LeetCode 1296. 划分数组为连续数字的集合

简介: LeetCode 1296. 划分数组为连续数字的集合

LeetCode 1296. 划分数组为连续数字的集合


Table of Contents

中文版:

英文版:

My answer:

解题报告:

中文版:

给你一个整数数组 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 <= nums.length <= 10^5

1 <= nums[i] <= 10^9

1 <= k <= nums.length

 

英文版:

1296. Divide Array in Sets of K Consecutive Numbers

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

My answer:

class Solution:
    def isPossibleDivide(self, nums: List[int], k: int) -> bool:
        nums.sort()
        _dict = {}
        for num in nums:
            if num in _dict:
                _dict[num] += 1
            else:
                _dict[num] = 1
        for num in nums:
            if _dict[num] != 0:
                for i in range(k):
                    if num + i in _dict and _dict[num+i] != 0:
                        _dict[num+i] -= 1
                    else:
                        return False                           
        return True

解题报告:

1、将数组升序排序,这样相同的数会排在一起

2、生成字典,key 是数组 nums 中的数,value 是数出现的个数

3、遍历数组中的数,判断其个数是否为 0:如果不为 0,则再检查从 num 到 num + k - 1 的数是否都存在,若不存在则返回 false。若遍历能进行到最后,返回 true。

相关文章
|
2月前
|
算法
【数组相关面试题】LeetCode试题
【数组相关面试题】LeetCode试题
|
20天前
【Leetcode】两数之和,给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
【Leetcode】两数之和,给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
|
15天前
【力扣】238. 除自身以外数组的乘积
【力扣】238. 除自身以外数组的乘积
|
15天前
|
C++
【力扣】2562. 找出数组的串联值
【力扣】2562. 找出数组的串联值
|
28天前
|
算法 C++ 索引
【力扣经典面试题】238. 除自身以外数组的乘积
【力扣经典面试题】238. 除自身以外数组的乘积
|
2月前
|
存储 算法
《LeetCode 热题 HOT 100》——寻找两个正序数组的中位数
《LeetCode 热题 HOT 100》——寻找两个正序数组的中位数
|
2月前
|
存储
力扣刷题-最大子数组和
力扣刷题-最大子数组和
10 1
|
2月前
leetcode2967. 使数组成为等数数组的最小代价
leetcode2967. 使数组成为等数数组的最小代价
21 0
|
2月前
|
算法 搜索推荐
LeetCode刷题---215. 数组中的第K个最大元素(双指针,快速选择)
LeetCode刷题---215. 数组中的第K个最大元素(双指针,快速选择)
|
2月前
|
Go C++
【力扣】2645. 构造有效字符串的最小插入数(动态规划 贪心 滚动数组优化 C++ Go)
【2月更文挑战第17天】2645. 构造有效字符串的最小插入数(动态规划 贪心 滚动数组优化 C++ Go)
31 8

热门文章

最新文章