【leetcode】1296. Divide Array in Sets of K Consecutive Numbers
题目如下:
Given an array of integers
numsand a positive integerk, find whether it's possible to divide this array into sets ofkconsecutive numbers
ReturnTrueif its possible otherwise returnFalse.Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: trueExample 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.Constraints:
1 <= nums.length <= 10^51 <= nums[i] <= 10^91 <= k <= nums.length
解题思路:从nums中最小的数字开始,依次往后找k-1个数字,找到一个就从nums删除掉对应的一个数字,直到nums为空,或者找不到符合条件的数字为止。
代码如下:
class Solution(object):
def isPossibleDivide(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
import bisect
nums.sort()
while len(nums) > 0:
head = nums.pop(0)
tk = k - 1
val = head + 1
while tk > 0:
inx = bisect.bisect_left(nums,val)
if inx >= 0 and inx < len(nums) and nums[inx] == val:
tk -= 1
val += 1
del nums[inx]
continue
return False
return tk == 0
【leetcode】1296. Divide Array in Sets of K Consecutive Numbers的更多相关文章
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- 【leetcode】1146. Snapshot Array
题目如下: Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) ini ...
- 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...
- 【LeetCode】932. Beautiful Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...
- 【LeetCode】922. Sort Array By Parity II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...
随机推荐
- Netty的学习
看了几天高并发和NIO 今晚终于要开始学习Netty http://ifeve.com/netty5-user-guide/ Netty实现通信的步骤 1.创建两个NIO线程组,一个专门用于网络事件的 ...
- fiddler笔记:主菜单栏
主菜单可以启动几乎所有的fiddler功能.菜单系统通过FiddlerScript或Extensions进行扩展和增强. 1.File菜单 File菜单主要是用来启动和停止web流量的捕获,也可以加载 ...
- 深度学习之卷积神经网络CNN及tensorflow代码实现示例
深度学习之卷积神经网络CNN及tensorflow代码实现示例 2017年05月01日 13:28:21 cxmscb 阅读数 151413更多 分类专栏: 机器学习 深度学习 机器学习 版权声明 ...
- tinymce富文本是在modal框中弹出显示的问题
记录一下,在用tinymce富文本的时候,由于是用在modal 上的,始终无法获取焦点,后来才发现问题出在tinymce在modal前创建了,所以导致这个问题,解决方案就是用 v-if="v ...
- VmWare 网络模式
VMware虚拟机三种联网方法及原理 一.Brigde--桥接:默认使用VMnet0 1.原理: Bridge 桥"就是一个主机,这个机器拥有两块网卡,分别处于两个局域网中,同时在" ...
- 从零开始使用mocha测试
mocha 需要在node环境下跑,请确保已经安装了node 1.新建一个文件夹 test 2.命令行切换到test目录下,执行命令:npm init ,出现选择按enter,一系列选择完成之 ...
- Java基础加强-注解
/*注解(Annotation)*/(注解相当于一个特殊的类,注解类@interface A) 了解注解及java提供的几个基本注解 1. @SuppressWarnings 指示应该在注释元素(以及 ...
- 【pycharm】pycharm断点调试
step into:单步执行,遇到子函数就进入并且继续单步执行(简而言之,进入子函数): step over:在单步执行时,在函数内遇到子函数时不会进入子函数内单步执行,而是将子函数整个执行完再停止, ...
- list 列表 函数的引用
方法 意义 L.index(v [, begin[, end]]) 返回对应元素的索引下标, begin为开始索引,end为结束索引,当 value 不存在时触发ValueError错误 L.inse ...
- 【leetcode】561. Array Partition I
原题: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...