|
| 1 | +''' |
| 2 | +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 |
| 3 | +Return True if its possible otherwise return False. |
| 4 | +
|
| 5 | + |
| 6 | +
|
| 7 | +Example 1: |
| 8 | +
|
| 9 | +Input: nums = [1,2,3,3,4,4,5,6], k = 4 |
| 10 | +Output: true |
| 11 | +Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. |
| 12 | +Example 2: |
| 13 | +
|
| 14 | +Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 |
| 15 | +Output: true |
| 16 | +Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. |
| 17 | +Example 3: |
| 18 | +
|
| 19 | +Input: nums = [3,3,2,2,1,1], k = 3 |
| 20 | +Output: true |
| 21 | +Example 4: |
| 22 | +
|
| 23 | +Input: nums = [1,2,3,4], k = 3 |
| 24 | +Output: false |
| 25 | +Explanation: Each array should be divided in subarrays of size 3. |
| 26 | +''' |
| 27 | +class Solution(object): |
| 28 | + def isPossibleDivide(self, nums, k): |
| 29 | + """ |
| 30 | + :type nums: List[int] |
| 31 | + :type k: int |
| 32 | + :rtype: bool |
| 33 | + """ |
| 34 | + from collections import Counter |
| 35 | + count_map = Counter(nums) |
| 36 | + for num in sorted(count_map.keys()): |
| 37 | + if count_map[num] <= 0: |
| 38 | + continue |
| 39 | + for index in range(1, k): |
| 40 | + count_map[num+index] -= count_map[num] |
| 41 | + if count_map[num+index] < 0: |
| 42 | + return False |
| 43 | + return True |
0 commit comments