1296. Divide Array in Sets of K Consecutive Numbers
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 59% Topics: Array, Hash Table, Greedy, Sorting
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(nlogn)
# Space: O(n)
import collections
class Solution(object):
def isPossibleDivide(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
count = collections.Counter(nums)
for num in sorted(count.keys()):
c = count[num]
if not c:
continue
for i in xrange(num, num+k):
if count[i] < c:
return False
count[i] -= c
return True
Solution from kamyu104/LeetCode-Solutions · MIT