Advertisement
pb_jiang

leetcode contest 304 T3

Aug 6th, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     bool validPartition(vector<int>& ns) {
  4.         int n = ns.size();
  5.         vector<int> dp(n + 1);
  6.         dp[0] = 1;
  7.         dp[1] = 0;
  8.         dp[2] = ns[0] == ns[1] ? 1 : 0;
  9.         for (int i = 3; i <= n; ++i) {
  10.             if (ns[i - 1] == ns[i - 2] && ns[i - 2] == ns[i - 3]) dp[i] = max(dp[i], dp[i - 3]);
  11.             if (ns[i - 1] == ns[i - 2]) dp[i] = max(dp[i], dp[i - 2]);
  12.             if (ns[i - 1] == ns[i - 2] + 1 && ns[i - 2] == ns[i - 3] + 1) dp[i] = max(dp[i], dp[i - 3]);            
  13.         }
  14.         return dp[n] == 1;
  15.     }
  16. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement