当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Idea new UI usage
- D - How Many Answers Are Wrong
- 「 WEB测试工程师 」岗位一面总结
- 黑猫带你学UFS协议第18篇:UFS如何配置逻辑单元(LU Management)
- Reading notes of effective managers
- What are the test sites for tunnel engineering?
- 二维码的前世今生 与 六大测试点梳理
- [leetcode] day96 - the first unique character & ransom letter & letter ectopic word
- Basic knowledge of error
- JDBC Requset 对应内容及功能介绍
猜你喜欢

Manhattan distance and Manhattan rectangle - print back font matrix

【Postman】Collections-运行配置之导入数据文件
![[eolink] PC client installation](/img/91/8b3c4264e544b14f926e91edddf18d.png)
[eolink] PC client installation

nodejs实现微博第三方登录

Hypothesis testing learning notes

G - Supermarket

LeetCode 729. 我的日程安排表 I

The latest 2022 review of "graph classification research"

(中)苹果有开源,但又怎样呢?

把el-tree选中的数组转换为数组对象
随机推荐
Aike AI frontier promotion (2.13)
Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误
[C language] string left rotation
JDBC Requset 对应内容及功能介绍
数据库-当前读与快照读
【C语言】字符串左旋
假设检验学习笔记
[postman] collections configuration running process
php使用redis实现分布式锁
A complete collection of necessary learning websites for office programmers
D - How Many Answers Are Wrong
[postman] dynamic variable (also known as mock function)
Still worrying about how to write web automation test cases? Senior test engineers teach you selenium test case writing hand in hand
[web security] nodejs prototype chain pollution analysis
全链路压测:构建三大模型
Web界面元素的测试
Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete
Gtest之TEST宏的用法
PAT(乙级)2022年夏季考试
E - food chain