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)


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