Problem statement

https://binarysearch.com/problems/List-Consecutive-Split/

Solution

Equal to Leetcode 0846 Hand of Straights.

Complexity

Time complexity is O(m log m + n). Space complexity is O(n).

Code

class Solution:
    def solve(self, hand, W):
        stack = sorted(Counter(hand).items())
        
        while stack:
            num, freq = stack.pop()
            addon = []
            for i in range(1, W):
                if not stack: return False
                num_n, freq_n = stack.pop()
                if num_n != num - i or freq_n < freq: return False
                if freq_n != freq: addon += [(num_n, freq_n - freq)]
            stack += addon[::-1]
    
        return True