Skip to main content Link Search Menu Expand Document (external link)

Split Array Into Consecutive Subsequences (Difficulty: Medium)

  • Will be back to write things up
class Solution:
    @staticmethod
    def check_appendable(list_subseq, n):
        i = 0
        while i < len(list_subseq):
            subseq = list_subseq[i]
            if subseq[-1] + 1 < n:
                if len(subseq) < 3:  # return false as answer
                    return "falseisanswer"
                list_subseq.pop(i)
                continue
                    
            if subseq[-1] + 1 == n:
                return i
            i += 1
            
        return None
        
    def isPossible(self, nums: List[int]) -> bool:
        list_subseq = []
        
        for n in nums:
            idx = self.check_appendable(list_subseq, n)
            if idx == "falseisanswer":
                return False
            elif idx != None:
                list_subseq[idx] += [n]
            else:
                list_subseq = [[n]] + list_subseq
        
        for subseq in list_subseq:
            if len(subseq) < 3:
                return False
            
        return True