题目如下:

Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.

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: true

Example 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^5
  • 1 <= nums[i] <= 10^9
  • 1 <= 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的更多相关文章

  1. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  2. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  3. 【leetcode】1146. Snapshot Array

    题目如下: Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) ini ...

  4. 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...

  6. 【LeetCode】932. Beautiful Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...

  7. 【LeetCode】922. Sort Array By Parity II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...

  8. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  9. 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...

随机推荐

  1. Nginx03---重装

    1.先执行一下命令 1.1 删除nginx,–purge包括配置文件 sudo apt-get --purge remove nginx 1.2 自动移除全部不使用的软件包 sudo apt-get ...

  2. Httpwatch教程

    启动Httpwatch 从IE的“查看”—“浏览器栏”—“HttpWatch”启动HttpWatch.如下图所示: 以下是HttpWatch程序界面 以下用登录我的邮箱mail.163.com例子来展 ...

  3. kettle下载地址

    kettle 4.4和4.2 版本是好的,版本6.6和8.2 版本有bug Kettle下载和安装: 1.官网各个版本下载地址:https://sourceforge.net/projects/pen ...

  4. dev linechart动态加载数据(像股票一样的波动)

    图片地址:https://blog.csdn.net/qq_33459369/article/details/80060196:(盗图) 接下来是封装的代码 #region 动态折线图 public ...

  5. QT开发小技巧-窗口处理(一)

    this->setWindowFlags(Qt::WindowCloseButtonHint); // 仅保留关闭按钮 this->setAttribute(Qt::WA_DeleteOn ...

  6. c# 如何把一个同步方法变成异步方法

    1 例如有同步方法如下: private static void GenerateFile(DataTable dt) { } 2 变为异步方法 private static void Generat ...

  7. centeros7安装mysql

    转载自:https://www.linuxidc.com/Linux/2016-09/135288.htm 安装之前先安装基本环境:yum install -y perl perl-Module-Bu ...

  8. mysql启动失败“MySQL Daemon failed to start”

    CentOS上,用命令:service mysqld restart 启动mysql报错: # service mysqld restart Stopping mysqld: [ OK ] MySQL ...

  9. gulp 实现sass自动化 ,监听同步

    实现功能 监听scss文件 sass自动化 准备条件 1 .安装gulp npm init   ---->一直enter,会在当前目录下生成一个package.json文件,记录安装的依赖模块 ...

  10. CentOS7 PHP增加连接Sqlserver扩展

    扩展插件下载地址 https://github.com/Microsoft/msphpsql/tags 本机PHP版本7.2,非线程安全 https://github.com/microsoft/ms ...