leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0659.cpp (608B)


0 class Solution { 1 public: 2 bool isPossible(const vector<int> &nums) const { 3 unordered_map<int, int> left, end; 4 for (const int n : nums) 5 left[n]++; 6 7 for (const int n : nums) { 8 if (!left[n]) continue; 9 left[n]--; 10 11 if (end[n - 1] > 0) { 12 end[n - 1]--; 13 end[n]++; 14 } else if (left[n + 1] > 0 && left[n + 2] > 0) { 15 left[n + 1]--; 16 left[n + 2]--; 17 end[n + 2]++; 18 } else 19 return false; 20 } 21 22 return true; 23 } 24 };