当前位置:网站首页>模拟卷Leetcode【普通】1296. 划分数组为连续数字的集合
模拟卷Leetcode【普通】1296. 划分数组为连续数字的集合
2022-07-06 06:15:00 【邂逅模拟卷】
1296. 划分数组为连续数字的集合
给你一个整数数组 nums 和一个正整数 k,请你判断是否可以把这个数组划分成一些由 k 个连续数字组成的集合。
如果可以,请返回 true;否则,返回 false。
示例 1:
输入:nums = [1,2,3,3,4,4,5,6], k = 4
输出:true
解释:数组可以分成 [1,2,3,4] 和 [3,4,5,6]。
示例 2:
输入:nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
输出:true
解释:数组可以分成 [1,2,3] , [2,3,4] , [3,4,5] 和 [9,10,11]。
示例 3:
输入:nums = [3,3,2,2,1,1], k = 3
输出:true
示例 4:
输入:nums = [1,2,3,4], k = 3
输出:false
解释:数组不能分成几个大小为 3 的子数组。
提示:
1 <= k <= nums.length <= 105
1 <= nums[i] <= 109
注意:此题目与 846 重复:https://leetcode-cn.com/problems/hand-of-straights/
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/divide-array-in-sets-of-k-consecutive-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
class Solution:
def __init__(self):
pass
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums)%k>0:return False
cnt = Counter(nums)
while cnt:
x = min(cnt)
for n in range(x,x+k):
if cnt[n]==1:cnt.pop(n)
elif cnt[n]>1:cnt[n]-=1
else:return False
return True
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list转node
return s.getResult(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总_卷子的博客-CSDN博客
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
猜你喜欢
【Postman】测试(Tests)脚本编写和断言详解
Redis 核心技术与实战之 基本架构:一个键值数据库包含什么?
[postman] test script writing and assertion details
Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)
[wechat applet] build a development tool environment
P问题、NP问题、NPC问题、NP-hard问题详解
Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
LeetCode 729. 我的日程安排表 I
JWT-JSON WEB TOKEN
Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete
随机推荐
[wechat applet] build a development tool environment
Introduction to promql of # yyds dry goods inventory # Prometheus
Postman核心功能解析-参数化和测试报告
【API接口工具】postman-界面使用介绍
LeetCode 729. 我的日程安排表 I
Interface test: what are the components of the URL in fiddler
Win10 cannot operate (delete, cut) files
多线程应用的测试与调试
A complete collection of necessary learning websites for office programmers
联合索引的左匹配原则
Software test interview questions - Test Type
「 WEB测试工程师 」岗位一面总结
[postman] dynamic variable (also known as mock function)
黑猫带你学UFS协议第18篇:UFS如何配置逻辑单元(LU Management)
P问题、NP问题、NPC问题、NP-hard问题详解
MySQL之数据类型
Thoughts on data security (Reprint)
【Postman】Collections配置运行过程
Accélération de la lecture vidéo de l'entreprise
Seven imperceptible truths in software testing