Split Array into Consecutive Subsequences

medium greedy hash map

Problem

Can the array be split into one or more subsequences of consecutive integers each of length ≥ 3?

Inputnums = [1,2,3,3,4,5]
OutputTrue
[1,2,3] and [3,4,5].

def is_possible(nums):
    from collections import Counter
    left = Counter(nums); ends = Counter()
    for x in nums:
        if left[x] == 0: continue
        left[x] -= 1
        if ends[x - 1] > 0: ends[x - 1] -= 1; ends[x] += 1
        elif left[x + 1] > 0 and left[x + 2] > 0:
            left[x + 1] -= 1; left[x + 2] -= 1; ends[x + 2] += 1
        else: return False
    return True
function isPossible(nums) {
  const left = new Map(), ends = new Map();
  const inc = (m, k, d=1) => m.set(k, (m.get(k)||0) + d);
  nums.forEach(x => inc(left, x));
  for (const x of nums) {
    if (!left.get(x)) continue;
    left.set(x, left.get(x) - 1);
    if (ends.get(x - 1) > 0) { ends.set(x - 1, ends.get(x - 1) - 1); inc(ends, x); }
    else if (left.get(x + 1) > 0 && left.get(x + 2) > 0) {
      left.set(x + 1, left.get(x + 1) - 1); left.set(x + 2, left.get(x + 2) - 1); inc(ends, x + 2);
    } else return false;
  }
  return true;
}
boolean isPossible(int[] nums) {
    Map left = new HashMap<>(), ends = new HashMap<>();
    for (int x : nums) left.merge(x, 1, Integer::sum);
    for (int x : nums) {
        if (left.getOrDefault(x, 0) == 0) continue;
        left.merge(x, -1, Integer::sum);
        if (ends.getOrDefault(x - 1, 0) > 0) { ends.merge(x - 1, -1, Integer::sum); ends.merge(x, 1, Integer::sum); }
        else if (left.getOrDefault(x + 1, 0) > 0 && left.getOrDefault(x + 2, 0) > 0) {
            left.merge(x + 1, -1, Integer::sum); left.merge(x + 2, -1, Integer::sum); ends.merge(x + 2, 1, Integer::sum);
        } else return false;
    }
    return true;
}
bool isPossible(vector& nums) {
    unordered_map left, ends;
    for (int x : nums) left[x]++;
    for (int x : nums) {
        if (left[x] == 0) continue;
        left[x]--;
        if (ends[x - 1] > 0) { ends[x - 1]--; ends[x]++; }
        else if (left[x + 1] > 0 && left[x + 2] > 0) { left[x + 1]--; left[x + 2]--; ends[x + 2]++; }
        else return false;
    }
    return true;
}
Time: O(n) Space: O(n)