'''
keys: 
Solutions:
Similar: 
T:
S:
'''
from typing import List
import collections


class Solution:
    '''
    leftis a hashmap, left[i] counts the number of i that I haven't placed yet.
    end is a hashmap, end[i] counts the number of consecutive subsequences that
    ends at number i. Then I tried to split the nums one by one.
    If I could neither add a number to the end of a existing consecutive 
    subsequence nor find two following number in the left, I return False
    '''
    def isPossible(self, nums: List[int]) -> bool:
        left = collections.Counter(nums)
        end = collections.Counter()
        for num in nums:
            if not left[num]: 
                continue
            left[num] -= 1 # marked as processed
            if end[num - 1] > 0: # there is some subseq ending with num-1
                end[num - 1] -= 1 # the subseq now ends with num -> end[num-1] -= 1
                end[num] += 1 # the subseq now ends with num -> end[num] += 1
            elif left[num + 1] and left[num + 2]: # two following ones exist
                left[num + 1] -= 1 # subseq ending with num+1 decrease by 1
                left[num + 2] -= 1 # subseq ending with num+2 decrease by 1
                end[num + 2] += 1 # this subseq now ends with num+2
            else:
                return False
        return True