LeetCode 1296. Divide Array in Sets of K Consecutive Numbers Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
1296. Divide Array in Sets of K Consecutive Numbers

Description

Given an array of integers nums and a positive integer k, check whether it is possible to divide this array into sets of k consecutive numbers.

Return true if it is 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 = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.

 

Constraints:

  • 1 <= k <= nums.length <= 105
  • 1 <= nums[i] <= 109

 

Note: This question is the same as 846: https://leetcode.com/problems/hand-of-straights/

Solutions

Solution 1: Hash Table + Sorting

First, we check if the length of the array nums is divisible by k. If it is not divisible, it means the array cannot be divided into subarrays of length k, and we return false directly.

Next, we use a hash table cnt to count the occurrences of each number in the array nums, and then we sort the array nums.

After sorting, we iterate through the array nums, and for each number x, if cnt[x] is not 0, we enumerate each number y from x to x + k - 1. If cnt[y] is 0, it means we cannot divide the array into subarrays of length k, and we return false directly. Otherwise, we decrement cnt[y] by 1.

After the loop, if no issues were encountered, it means we can divide the array into subarrays of length k, and we return true.

The time complexity is O(n × log n), and the space complexity is O(n). Here, n is the length of the array nums.

PythonJavaC++GoTypeScript
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: if len(nums) % k: return False cnt = Counter(nums) for x in sorted(nums): if cnt[x]: for y in range(x, x + k): if cnt[y] == 0: return False cnt[y] -= 1 return True(code-box)

Solution 2: Ordered Set

Similar to Solution 1, we first check if the length of the array nums is divisible by k. If it is not divisible, it means the array cannot be divided into subarrays of length k, and we return false directly.

Next, we use an ordered set sd to count the occurrences of each number in the array nums.

Then, we repeatedly extract the smallest value x from the ordered set and enumerate each number y from x to x + k - 1. If these numbers all appear in the ordered set with non-zero occurrences, we decrement their occurrence count by 1. If the occurrence count becomes 0 after the decrement, we remove the number from the ordered set; otherwise, it means we cannot divide the array into subarrays of length k, and we return false.

If we can successfully divide the array into subarrays of length k, we return true after completing the traversal.

The time complexity is O(n × log n), and the space complexity is O(n). Here, n is the length of the array nums.

PythonJavaC++GoTypeScript
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: if len(nums) % k: return False cnt = Counter(nums) sd = SortedDict(cnt) while sd: x = next(iter(sd)) for y in range(x, x + k): if y not in sd: return False if sd[y] == 1: del sd[y] else: sd[y] -= 1 return True(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !