当前位置:网站首页>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;
}
};
边栏推荐
- DIY personal server (DIY storage server)
- Automated testing ----- selenium (I)
- Apache MINA框架「建议收藏」
- 毕业从事弱电3个月,我为什么会选择转行网络工程师
- [matlab] download originality documents based on oil monkey script and MATLAB
- RF, gbdt, xgboost feature selection methods "recommended collection"
- qml 结合 QSqlTableModel 动态加载数据 MVC「建议收藏」
- Card link
- [today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference
- 预处理指令
猜你喜欢

智能电子界桩自然保护区远程监控解决方案
![[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

【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)

Prescan quick start to master Lesson 19: prescan actuator configuration, track synchronization and non configuration of multiple tracks

火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布

Struct, enum type and union

Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework

How much memory does bitmap occupy in the development of IM instant messaging?

网络协议:TCP Part2
![[MCU] 51 MCU burning those things](/img/fa/8f11ef64a18114365c084fff7d39f6.png)
[MCU] 51 MCU burning those things
随机推荐
Compilation and operation of program
[tensorrt] trtexec tool to engine
【高等数学】【4】不定积分
CarSim simulation quick start (XV) - ADAS sensor objects of CarSim sensor simulation (1)
Difference Between Accuracy and Precision
【高等数学】【8】微分方程
"Chain" connects infinite possibilities: digital asset chain, wonderful coming soon!
CarSim simulation quick start (16) - ADAS sensor objects of CarSim sensor simulation (2)
文件操作详解
[advanced mathematics] [3] Application of differential mean value theorem and derivative
[advanced drawing of single cell] 07. Display of KEGG enrichment results
Link list of sword finger offer question bank summary (III) (C language version)
雷达水位计的工作原理及安装维护注意事项
Socket error Event: 32 Error: 10053. Connection closing...Socket close
[cloud native] use of Nacos taskmanager task management
Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework
Key network protocols in tcp/ip four layer model
Chapter VI modified specification (SPEC) class
[today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace
[today in history] July 13: the father of database passed away; Apple buys cups code; IBM chip Alliance