当前位置:网站首页>Leetcode-6131: the shortest dice sequence impossible to get
Leetcode-6131: the shortest dice sequence impossible to get
2022-07-25 20:38:00 【Chrysanthemum headed bat】
leetcode-6131: The shortest dice sequence that cannot be obtained
subject
Topic linking
Give you a length of n Array of integers for rolls And an integer k . You throw one k Face dice n Time , Each side of the dice is 1 To k , Among them the first i The number of times thrown is rolls[i] .
Please return unable from rolls Obtained in The shortest The length of the dice sequence .
Throw one k Face dice len The result is a length of len Of Dice sequence .
Be careful , Subsequences only need to maintain the order in the original array , There is no need for continuity .
Example 1:
Input :rolls = [4,2,1,2,3,3,2,4,1], k = 4
Output :3
explain : All lengths are 1 Dice sequence [1] ,[2] ,[3] ,[4] Can be obtained from the original array .
All lengths are 2 Dice sequence [1, 1] ,[1, 2] ,... ,[4, 4] Can be obtained from the original array .
Subsequence [1, 4, 2] Cannot get from the original array , So we go back to 3 .
There are other subsequences that cannot be obtained from the original array .
Example 2:
Input :rolls = [1,1,2,2], k = 2
Output :2
explain : All lengths are 1 The subsequence [1] ,[2] Can be obtained from the original array .
Subsequence [2, 1] Cannot get from the original array , So we go back to 2 .
There are other subsequences that cannot be obtained from the original array , but [2, 1] Is the shortest subsequence .
Example 3:
Input :rolls = [1,1,3,2,2,2,3,3], k = 4
Output :1
explain : Subsequence [4] Cannot get from the original array , So we go back to 1 .
There are other subsequences that cannot be obtained from the original array , but [4] Is the shortest subsequence .

Problem solving
Method 1 : brain twists
These numbers can be divided into A few paragraphs , Each paragraph contains 1 To K Value , The last value of each paragraph , It must only appear once in the current paragraph .
So the final result , Is the number of segments that can be divided +1
class Solution {
public:
int shortestSequence(vector<int>& rolls, int k) {
unordered_set<int> set;
int res=1;
for(int roll:rolls){
if(set.count(roll)==0){
set.insert(roll);
if(set.size()==k){
res++;
set.clear();
}
}
}
return res;
}
};
边栏推荐
- Recommended books | essentials of industrial digital transformation: methods and Practice
- Vulnhub | dc: 6 | [actual combat]
- Implementation of simple registration and login
- Mobile web layout method
- [advanced mathematics] [8] differential equation
- [MCU] 51 MCU burning those things
- matlab----EEGLab查看脑电信号
- 预处理指令
- Open source SPL enhances mangodb computing
- 【高等数学】【1】函数、极限、连续
猜你喜欢

Online random coin tossing positive and negative statistical tool
![[advanced mathematics] [6] differential calculus of multivariate functions](/img/9e/84fe6f74b58cbaabab1b6eed0df556.png)
[advanced mathematics] [6] differential calculus of multivariate functions

FanoutExchange交换机代码教程
![[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference](/img/0f/8ce2d5487b16d38a152cfd3ab454bb.png)
[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference

Leetcode customs clearance: hash table six, this is really a little simple
![[today in history] July 7: release of C; Chrome OS came out;](/img/a6/3170080268a836f2e0973916d737dc.png)
[today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued

leetcode-6127:优质数对的数目

程序的编译和运行

【高等数学】【1】函数、极限、连续

leetcode-6129:全 0 子数组的数目
随机推荐
Link list of sword finger offer question bank summary (III) (C language version)
"Chain" connects infinite possibilities: digital asset chain, wonderful coming soon!
Yolov7 training error indexerror: list index out of range
Mobile web layout method
Technology cloud report: more than zero trust, the wild hope of Parra's "Digital Security Cloud strategy"
During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
Success factors of software R & D effectiveness measurement
Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released
QML combines qsqltablemodel to dynamically load data MVC "recommended collection"
leetcode-155:最小栈
毕业从事弱电3个月,我为什么会选择转行网络工程师
leetcode-6131:不可能得到的最短骰子序列
JS作用域与作用域链
【单细胞高级绘图】07.KEGG富集结果展示
Dataframe first performs grouping operation and then combines output
Arrow 之 Parquet
【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入
JMeter - interface test
Scan delete folder problems
Vulnhub | dc: 6 | [actual combat]