leetcode

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

2369.cpp (843B)


      1 class Solution {
      2     int8_t dp[100001];
      3 
      4   public:
      5     Solution() { memset(dp, 0xFF, sizeof(dp)); }
      6     bool validPartition(const vector<int> &nums, int idx = 0) {
      7         if (idx == nums.size()) return true;
      8         if (idx == nums.size() - 1) return false;
      9 
     10         if (dp[idx] != -1) return dp[idx];
     11 
     12         if (nums[idx] == nums[idx + 1]) {
     13             if (validPartition(nums, idx + 2)) return dp[idx] = true;
     14         }
     15 
     16         if (idx == nums.size() - 2) return dp[idx] = false;
     17 
     18         if (nums[idx] == nums[idx + 1] && nums[idx] == nums[idx + 2]) {
     19             if (validPartition(nums, idx + 3)) return dp[idx] = true;
     20         }
     21 
     22         if (nums[idx] + 1 == nums[idx + 1] && nums[idx] + 2 == nums[idx + 2]) {
     23             if (validPartition(nums, idx + 3)) return dp[idx] = true;
     24         }
     25 
     26         return dp[idx] = false;
     27     }
     28 };