当前位置:网站首页>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;
}
};
边栏推荐
- During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
- leetcode-6130:设计数字容器系统
- Unity VS—— VS中默认调试为启动而不是附加到Unity调试
- Vulnhub | dc: 5 | [actual combat]
- preprocessor directives
- 文件操作详解
- MySQL 日期【加号/+】条件筛选问题
- Clickhouse notes 02 -- installation test clickvisual
- Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important
- [advanced mathematics] [1] function, limit, continuity
猜你喜欢
![[today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
![[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born](/img/bf/09ccf36caec099098a22f0e8b670bd.png)
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born

Fanoutexchange switch code tutorial

Online random coin tossing positive and negative statistical tool

Network protocol: TCP part2
![[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging](/img/e9/5751dc435cfbbefc22d84fd9ebbaea.png)
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging

leetcode-6131:不可能得到的最短骰子序列
![MySQL date [plus sign / +] condition filtering problem](/img/86/aed048e27b3e0b0baa919204bc067c.png)
MySQL date [plus sign / +] condition filtering problem

leetcode-919:完全二叉树插入器

Recommended books | essentials of industrial digital transformation: methods and Practice
随机推荐
Remote—基本原理介绍
Leetcode customs clearance: hash table six, this is really a little simple
[workplace rules] it workplace rules | poor performance
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
leetcode-6125:相等行列对
Fanoutexchange switch code tutorial
Difference Between Accuracy and Precision
Yolov7 training error indexerror: list index out of range
Why did I choose to become a network engineer after graduating from weak current for 3 months
Clickhouse notes 02 -- installation test clickvisual
Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
LeetCode通关:哈希表六连,这个还真有点简单
[today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET
[leetcode] 28. Implement strstr ()
Unity VS—— VS中默认调试为启动而不是附加到Unity调试
Online random coin tossing positive and negative statistical tool
Today's sleep quality record 75 points
How to choose a microservice registration center?
[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
leetcode-79:单词搜索