当前位置:网站首页>leetcode-6131:不可能得到的最短骰子序列
leetcode-6131:不可能得到的最短骰子序列
2022-07-25 20:36:00 【菊头蝙蝠】
题目
题目连接
给你一个长度为 n 的整数数组 rolls 和一个整数 k 。你扔一个 k 面的骰子 n 次,骰子的每个面分别是 1 到 k ,其中第 i 次扔得到的数字是 rolls[i] 。
请你返回 无法 从 rolls 中得到的 最短 骰子子序列的长度。
扔一个 k 面的骰子 len 次得到的是一个长度为 len 的 骰子子序列 。
注意 ,子序列只需要保持在原数组中的顺序,不需要连续。
示例 1:
输入:rolls = [4,2,1,2,3,3,2,4,1], k = 4
输出:3
解释:所有长度为 1 的骰子子序列 [1] ,[2] ,[3] ,[4] 都可以从原数组中得到。
所有长度为 2 的骰子子序列 [1, 1] ,[1, 2] ,... ,[4, 4] 都可以从原数组中得到。
子序列 [1, 4, 2] 无法从原数组中得到,所以我们返回 3 。
还有别的子序列也无法从原数组中得到。
示例 2:
输入:rolls = [1,1,2,2], k = 2
输出:2
解释:所有长度为 1 的子序列 [1] ,[2] 都可以从原数组中得到。
子序列 [2, 1] 无法从原数组中得到,所以我们返回 2 。
还有别的子序列也无法从原数组中得到,但 [2, 1] 是最短的子序列。
示例 3:
输入:rolls = [1,1,3,2,2,2,3,3], k = 4
输出:1
解释:子序列 [4] 无法从原数组中得到,所以我们返回 1 。
还有别的子序列也无法从原数组中得到,但 [4] 是最短的子序列。

解题
方法一:脑筋急转弯
可以把这些数分为 几段,每一段里面都包含了1到K的值,每一段的最后一个值,一定是当前段中只出现一次的。
因此最后的结果,就是可以划分的段数+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;
}
};
边栏推荐
- Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important
- Rand1 generates rand9
- [today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
- Detailed explanation of document operation
- DIY个人服务器(diy存储服务器)
- leetcode-79:单词搜索
- Formatdatetime explanation [easy to understand]
- Docker builds redis cluster
- Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released
- How to choose a microservice registration center?
猜你喜欢
![[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
![[advanced mathematics] [5] definite integral and its application](/img/b2/62748b7533982f2b864148e0857490.png)
[advanced mathematics] [5] definite integral and its application
![[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

Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!
![[today in history] July 13: the father of database passed away; Apple buys cups code; IBM chip Alliance](/img/2d/c23a367c9e8e2806ffd5384de273d2.png)
[today in history] July 13: the father of database passed away; Apple buys cups code; IBM chip Alliance

Difference Between Accuracy and Precision
![[paper reading] unpaired image to image translation using cycle consistent advantageous networks](/img/73/69651dd8ecfdddd1cae13a1d223d51.png)
[paper reading] unpaired image to image translation using cycle consistent advantageous networks

JVM (XXIII) -- JVM runtime parameters

Leetcode customs clearance: hash table six, this is really a little simple
![[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
随机推荐
Fanoutexchange switch code tutorial
[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference
飞行器pid控制(旋翼飞控)
Scan delete folder problems
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
Card link
使用cookie登录百度网盘(网站使用cookie)
【高等数学】【6】多元函数微分学
[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
[advanced mathematics] [3] Application of differential mean value theorem and derivative
[cloud native] use of Nacos taskmanager task management
leetcode-79:单词搜索
4everland storage node portal network design
Prescan quick start to master Lesson 19: prescan actuator configuration, track synchronization and non configuration of multiple tracks
QML combines qsqltablemodel to dynamically load data MVC "recommended collection"
程序的编译和运行
第六章 修改规范(SPEC)类
网络协议:TCP Part2
数据库清空表数据并让主键从1开始
Timing analysis and constraints based on xlinx (1) -- what is timing analysis? What are temporal constraints? What is temporal convergence?