id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
3,456 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 500</code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(nums.length, 25)</code></li>
</ul>
| 3 | {
"code": "\nclass Solution {\npublic:\n vector<int> nums;\n int k;\n int n;\n vector<vector<vector<int>>> dp;\n int solve(int previdx, int curridx, int left) {\n if (curridx == nums.size()) {\n return 0;\n }\n if (dp[previdx][curridx][left] != -1) {\n return dp[previdx][curridx][left];\n }\n int take = 0, notake = 0;\n\n notake = solve(previdx, curridx + 1, left);\n\n if (previdx == n) {\n take = 1 + solve(curridx, curridx + 1, left);\n } else {\n if (left) {\n int newleft = (nums[curridx] != nums[previdx]) ? left - 1 : left;\n take = 1 + solve(curridx, curridx + 1, newleft);\n } else {\n if (nums[previdx] == nums[curridx]) {\n take = 1 + solve(curridx, curridx + 1, left);\n }\n }\n }\n\n return dp[previdx][curridx][left] = max(take, notake);\n }\n\n int maximumLength(vector<int>& nums, int k) {\n this->nums = nums;\n this->k = k;\n this->n = nums.size();\n dp.resize(n + 1, vector<vector<int>>(n + 1, vector<int>(k + 1, -1)));\n return solve(n, 0, k);\n }\n};",
"memory": "422449"
} |
3,456 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 500</code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(nums.length, 25)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n \n vector<vector<vector<int>>> dp(n+1,vector<vector<int>>(n+1, vector<int> (k+1,0)));\n\n for(int index=n-1;index>=0;index--){\n for(int prevIndex=index-1;prevIndex>=-1;prevIndex--){\n for(int kk=0;kk<=k;kk++){\n \n int pick = 0;\n if(prevIndex==-1 || nums[prevIndex]==nums[index]){\n pick = 1 + dp[index+1][index+1][kk];\n }\n else if(nums[prevIndex]!=nums[index] && kk>0){\n pick = 1 + dp[index+1][index+1][kk-1];\n }\n\n int notPick = dp[index+1][prevIndex+1][kk];\n\n dp[index][prevIndex+1][kk] =max(pick,notPick);\n }\n }\n }\n return dp[0][0][k];\n }\n};",
"memory": "429945"
} |
3,456 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 500</code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(nums.length, 25)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int fun(vector<int>& nums, int k,int i,int prev,vector<vector<vector<int>>>&dp){\n if(i>=nums.size()){\n return 0;\n }\n if(dp[i][prev+1][k]!=-1){\n return dp[i][prev+1][k];\n }\n int a=0;\n if(prev==-1){\n a=max(fun(nums,k,i+1,i,dp)+1,fun(nums,k,i+1,prev,dp));\n }\n else if( nums[i]==nums[prev]){\n a=(fun(nums,k,i+1,i,dp)+1);\n }\n else {\n if(k>0){\n a=max(fun(nums,k-1,i+1,i,dp)+1,fun(nums,k,i+1,prev,dp));\n }\n else{\n a=fun(nums,k,i+1,prev,dp);\n }\n }\n return dp[i][prev+1][k]=a;\n }\n int maximumLength(vector<int>& nums, int k) {\n int n=nums.size();\n vector<vector<vector<int> > > dp(\n n+1, vector<vector<int> >(n+1, vector<int>(k+1,-1)));\n return fun(nums,k,0,-1,dp);\n }\n};",
"memory": "437441"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n \n int maximumLength(vector<int>& arr, int k) {\n int n=arr.size();\n int vis[5005][55];\n memset(vis,-1,sizeof(vis));\n unordered_map<int,int>last_seen;\n vector<int>longestSoFar(k+1,0);\n vis[0][0]=1;\n last_seen[arr[0]]=0;\n longestSoFar[0]=1;\n for(int i=1;i<n;i++){\n vis[i][0]=1;\n int maxp=min(i,k);\n if(last_seen.find(arr[i])!=last_seen.end()){\n for(int p=0;p<=maxp;p++){\n vis[i][p]=max(vis[i][p],1+vis[last_seen[arr[i]]][p]);\n }\n }\n last_seen[arr[i]]=i;\n for(int p=0;p<=maxp;p++){\n if(p>=k)break;\n vis[i][p+1]=max(vis[i][p+1],longestSoFar[p]+1);\n }\n for(int p=0;p<=maxp;p++){\n longestSoFar[p]=max(vis[i][p],longestSoFar[p]);\n }\n }\n int ans=1;\n for(int p=0;p<=k;p++){\n ans=max(ans,longestSoFar[p]);\n }\n return ans;\n }\n};",
"memory": "36691"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int result = 0, max_dp[51] = {}, dp[5000][51] = {};\n std::unordered_map<int, int> prev;\n for (int i = static_cast<int>(nums.size()) - 1; i >= 0; --i)\n {\n auto it = prev.find(nums[i]);\n int p = (it != prev.end())?it->second:i;\n for (int j = k; j >= 0; --j)\n {\n dp[i][j] = std::max(1 + ((i != p)?dp[p][j]:0),\n 1 + ((j > 0)?max_dp[j - 1]:0));\n max_dp[j] = std::max(max_dp[j], dp[i][j]); \n } \n prev[nums[i]] = i;\n result = std::max(result, dp[i][k]);\n } \n return result;\n }\n};",
"memory": "36691"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int dp[5001][51][2][2];\n \n int solve(int ind, bool knotreq, bool isst, int k, vector<int>& nums, vector<int>& nxtsame) {\n\n if(ind==nums.size())\n return 0;\n\n if(dp[ind][k][knotreq][isst]!=-1)\n return dp[ind][k][knotreq][isst];\n\n int maxi = 0;\n\n if(isst)\n {\n maxi = max(maxi, solve(ind+1, true, true, k, nums, nxtsame));\n maxi = max(maxi, 1 + solve(nxtsame[ind], true, false, k, nums, nxtsame));\n maxi = max(maxi, 1 + solve(ind+1, false, false, k, nums, nxtsame));\n }\n else if(knotreq)\n {\n maxi = max(maxi, 1 + solve(nxtsame[ind], true, false, k, nums, nxtsame));\n maxi = max(maxi, 1 + solve(ind+1, false, false, k,nums, nxtsame));\n }\n else\n {\n if(k>0)\n {\n maxi = max(maxi, 1 + solve(nxtsame[ind], true, false, k-1, nums, nxtsame));\n maxi = max(maxi, 1 + solve(ind+1, false, false, k-1, nums, nxtsame));\n }\n maxi = max(maxi, solve(ind+1, false, false, k, nums, nxtsame));\n }\n\n return dp[ind][k][knotreq][isst] = maxi;\n\n }\n \n int maximumLength(vector<int>& nums, int k) {\n memset(dp, -1, sizeof(dp));\n\n vector<int> nxtsame(nums.size(),nums.size());\n\n unordered_map<int,int> mp;\n\n for(int i=nums.size()-1; i>=0; i--)\n {\n if(mp.find(nums[i])!=mp.end())\n nxtsame[i] = mp[nums[i]];\n mp[nums[i]] = i;\n }\n\n return solve(0, true, true , k, nums, nxtsame);\n }\n};",
"memory": "39875"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n \n int n=nums.size(),ans=1;\n unordered_map<int,int>ele;\n vector<int>nuer(n,n),ner(n,n);\n vector<vector<int>>dp(2,vector<int>(n,1)),track(2,vector<int>(n));\n \n for(int i=n-1;i>=0;i--)\n {\n if(ele.find(nums[i])!=ele.end())\n ner[i]=ele[nums[i]];\n \n ele[nums[i]]=i;\n }\n \n for(int i=n-2;i>=0;i--)\n {\n if(nums[i]!=nums[i+1])\n nuer[i]=i+1;\n else\n nuer[i]=nuer[i+1];\n }\n \n for(int allowed=0;allowed<=k;allowed++)\n {\n track[1][n-1]=1;\n \n for(int i=n-2;i>=0;i--)\n {\n dp[1][i]=1+max((nuer[i]<n && allowed)?track[0][nuer[i]]:0,ner[i]<n?dp[1][ner[i]]:0);\n track[1][i]=max(track[1][i+1],dp[1][i]);\n ans=max(ans,dp[1][i]);\n }\n \n track[0]=track[1];\n dp[0]=dp[1];\n }\n \n return ans;\n }\n};",
"memory": "39875"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 0 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution\n{\npublic:\n int dp[5001][2][51];\n int solve(int i, int count, bool same, vector<int> &nums, unordered_map<int, vector<int>> &mp)\n {\n if (i >= nums.size())\n return 0;\n if (dp[i][same][count] != -1)\n return dp[i][same][count];\n\n int take = 0, notake = 0;\n vector<int> &v = mp[nums[i]];\n int ind = upper_bound(v.begin(), v.end(), i) - v.begin();\n\n if (ind == v.size())\n take = 1;\n else\n take = 1 + solve(v[ind], count, true, nums, mp);\n\n if (count > 0)\n take = max(take, 1 + solve(i + 1, count - 1, false, nums, mp));\n if (same == false)\n notake = solve(i + 1, count, false, nums, mp);\n\n return dp[i][same][count] = max(take, notake);\n }\n\n int maximumLength(vector<int> &nums, int k)\n {\n memset(dp, -1, sizeof(dp));\n unordered_map<int, vector<int>> mp;\n for (int i = 0; i < nums.size(); i++)\n {\n mp[nums[i]].push_back(i);\n }\n\n return solve(0, k, false, nums, mp);\n }\n};\n",
"memory": "43059"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 0 | {
"code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplate<typename T> istream & operator>>(istream & in, vector<T> & lst)\n{ for (auto & e : lst) in >> e; return in; }\n#define endl '\\n'\n#define F first\n#define pb push_back\n#define pf push_front\n#define pob pop_back\n#define pof pop_front\n#define S second\n#define MP make_pair\n// typedef long long ll;\ntypedef long double ld;\ntypedef pair<ll, ll> ii;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<ii> vii;\ntypedef set<ll> si;\ntypedef map<string, ll> msi;\n#define all(v) v.begin(),v.end()\n#define REP(i, a, b) \\\nfor (ll i = ll(a); i <= ll(b); i++)\n#define RREP(i, a, b) \\\nfor (ll i = ll(a); i >= ll(b); i--)\n#define ohyes cout<<\"YES\\n\"\n#define ohno cout<<\"NO\\n\"\n#define getvec(v,a,b) \\\nfor (ll i = a; i <=b; i++) \\\n cin >> v[i];\n#define printvec(v,a,b) {for (ll i = a; i <=b; i++){cout << v[i] << \" \";}cout<<'\\n';}\n#define get2dvec(v,a,b,c,d) \\\nfor (ll i = a; i <=b; i++) \\\n for (ll j = c; j <=d; j++) \\\n cin >> v[i][j];\n#define print2dvec(v,a,b,c,d) for (ll i = a; i <=b; i++){for (ll j = c; j <=d; j++){cout << v[i][j]<<\" \";}cout<<'\\n';}\n#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n// ll mod = 998244353;\n// ll mod = 1e9 + 7;\nll _t,cs;\n// cout<<\"Case \"<<cs-_t<<\": \"<<ans<<'\\n';\n// memset(dp,-1,sizeof(dp));\nll dp[5005][60];\nll maxx[60][5005];\nll maxx1[60];\nclass Solution {\npublic:\n vector<int> v;\n ll n,k;\n int maximumLength(vector<int>& nums, int kk) { \n v=nums;\n n=v.size();\n map<ll,ll> mp,mp1;\n for(auto i:v){\n mp[i]++;\n }\n ll numb=1;\n for(auto i:mp){\n mp1[i.F]=numb;\n numb++;\n }\n for(auto &i:v){\n i=mp1[i];\n }\n k=kk;\n REP(i,0,n+2){\n REP(j,0,51){\n dp[i][j]=0;\n maxx[j][i]=0;\n maxx1[j]=0;\n }\n }\n // map<ll,map<ll,ll>>maxx;\n // map<ll,ll>maxx1;\n // n-1-i\n ll mx=1;\n RREP(i,n-1,0){\n REP(j,0,k){\n dp[i][j]=max((ll)1,dp[i][j]);\n if(j)dp[i][j]=max((ll)maxx1[j-1]+1,dp[i][j]);\n dp[i][j]=max((ll)maxx[j][v[i]]+1,dp[i][j]); \n } \n REP(j,0,k){\n maxx1[j]=max(maxx1[j],dp[i][j]);\n maxx[j][v[i]]=max(maxx[j][v[i]],dp[i][j]);\n }\n mx=max(mx,dp[i][k]);\n }\n\n return mx;\n }\n};",
"memory": "43059"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n unordered_set<int> elements;\n\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n // encode\n int m = 0;\n unordered_map<int, int> mp;\n for(int x : nums){\n if(mp.find(x) == mp.end()){\n mp[x] = m;\n m++;\n }\n }\n\n // dp\n vector<vector<int>> dp(m, vector<int>(k+1));\n vector<pair<int, int>> max_len(k+1); // len, num\n int ans = 0;\n for(int num : nums){\n int x = mp[num];\n for(int j = 0; j <= k; j++){\n dp[x][j]++;\n if(j > 0 && max_len[j-1].second != x){\n dp[x][j] = max(dp[x][j], 1 + max_len[j-1].first);\n }\n if(dp[x][j] > max_len[j].first){\n max_len[j].first = dp[x][j];\n max_len[j].second = x;\n }\n ans = max(ans, dp[x][j]);\n }\n }\n return ans;\n }\n};\n\n/*\nfrom left to right, we try to include or exclude each element, and record the end\nof subsequence. and we also record the number of indices s.t. seq[i] != seq[i+1].\n\nfor each iteration, we first add every element in dp[num] by 1.\nthenm we compare it with other numbers.\n\n 1, 2, 1, 1, 3 , 3, 3\n\n 0, 1, 2\n1: 1 1 1\n2: 1 2 2\n3: \n\n 0, 1, 2\n1: 3 3 4\n2: 1 2 2\n3: \n\n 0, 1, 2\n1: 3 3 4\n2: 1 2 2\n3: 1 4 4\n\n\n 0, 1, 2\n1: 3 3 4\n2: 1 2\n3: 3 6 6\n\n\n\n 1, 2, 1, 1, 3, 3, 3\n\n*/",
"memory": "46243"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n unordered_set<int> elements;\n\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n // encode\n int m = 0;\n unordered_map<int, int> mp;\n for(int x : nums){\n if(mp.find(x) == mp.end()){\n mp[x] = m;\n m++;\n }\n }\n\n // dp\n vector<vector<int>> dp(m, vector<int>(k+1));\n vector<int> max_len(k+1);\n for(int num : nums){\n int x = mp[num];\n for(int j = k; j >= 0; j--){\n if(j == 0)\n dp[x][j]++;\n else{\n dp[x][j] = max(dp[x][j]+1, 1 + max_len[j-1]);\n }\n max_len[j] = max(max_len[j], dp[x][j]);\n }\n }\n return max_len[k];\n }\n};\n\n/*\nfrom left to right, we try to include or exclude each element, and record the end\nof subsequence. and we also record the number of indices s.t. seq[i] != seq[i+1].\n\nfor each iteration, we first add every element in dp[num] by 1.\nthenm we compare it with other numbers.\n\n 1, 2, 1, 1, 3 , 3, 3\n\n 0, 1, 2\n1: 1 1 1\n2: 1 2 2\n3: \n\n 0, 1, 2\n1: 3 3 4\n2: 1 2 2\n3: \n\n 0, 1, 2\n1: 3 3 4\n2: 1 2 2\n3: 1 4 4\n\n\n 0, 1, 2\n1: 3 3 4\n2: 1 2\n3: 3 6 6\n\n\n\n 1, 2, 1, 1, 3, 3, 3\n\n*/",
"memory": "46243"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 0 | {
"code": "namespace atcoder {\n\nnamespace internal {\n\n#if __cplusplus >= 202002L\n\nusing std::bit_ceil;\n\n#else\n\n// @return same with std::bit::bit_ceil\nunsigned int bit_ceil(unsigned int n) {\n unsigned int x = 1;\n while (x < (unsigned int)(n)) x *= 2;\n return x;\n}\n\n#endif\n\n// @param n `1 <= n`\n// @return same with std::bit::countr_zero\nint countr_zero(unsigned int n) {\n#ifdef _MSC_VER\n unsigned long index;\n _BitScanForward(&index, n);\n return index;\n#else\n return __builtin_ctz(n);\n#endif\n}\n\n// @param n `1 <= n`\n// @return same with std::bit::countr_zero\nconstexpr int countr_zero_constexpr(unsigned int n) {\n int x = 0;\n while (!(n & (1 << x))) x++;\n return x;\n}\n\n} // namespace internal\n\n\n#if __cplusplus >= 201703L\n\ntemplate <class S, auto op, auto e> struct segtree {\n static_assert(std::is_convertible_v<decltype(op), std::function<S(S, S)>>,\n \"op must work as S(S, S)\");\n static_assert(std::is_convertible_v<decltype(e), std::function<S()>>,\n \"e must work as S()\");\n\n#else\n\ntemplate <class S, S (*op)(S, S), S (*e)()> struct segtree {\n\n#endif\n\n public:\n segtree() : segtree(0) {}\n explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}\n explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {\n size = (int)internal::bit_ceil((unsigned int)(_n));\n log = internal::countr_zero((unsigned int)size);\n d = std::vector<S>(2 * size, e());\n for (int i = 0; i < _n; i++) d[size + i] = v[i];\n for (int i = size - 1; i >= 1; i--) {\n update(i);\n }\n }\n\n void set(int p, S x) {\n assert(0 <= p && p < _n);\n p += size;\n d[p] = x;\n for (int i = 1; i <= log; i++) update(p >> i);\n }\n\n S get(int p) const {\n assert(0 <= p && p < _n);\n return d[p + size];\n }\n\n S prod(int l, int r) const {\n assert(0 <= l && l <= r && r <= _n);\n S sml = e(), smr = e();\n l += size;\n r += size;\n\n while (l < r) {\n if (l & 1) sml = op(sml, d[l++]);\n if (r & 1) smr = op(d[--r], smr);\n l >>= 1;\n r >>= 1;\n }\n return op(sml, smr);\n }\n\n S all_prod() const { return d[1]; }\n\n template <bool (*f)(S)> int max_right(int l) const {\n return max_right(l, [](S x) { return f(x); });\n }\n template <class F> int max_right(int l, F f) const {\n assert(0 <= l && l <= _n);\n assert(f(e()));\n if (l == _n) return _n;\n l += size;\n S sm = e();\n do {\n while (l % 2 == 0) l >>= 1;\n if (!f(op(sm, d[l]))) {\n while (l < size) {\n l = (2 * l);\n if (f(op(sm, d[l]))) {\n sm = op(sm, d[l]);\n l++;\n }\n }\n return l - size;\n }\n sm = op(sm, d[l]);\n l++;\n } while ((l & -l) != l);\n return _n;\n }\n\n template <bool (*f)(S)> int min_left(int r) const {\n return min_left(r, [](S x) { return f(x); });\n }\n template <class F> int min_left(int r, F f) const {\n assert(0 <= r && r <= _n);\n assert(f(e()));\n if (r == 0) return 0;\n r += size;\n S sm = e();\n do {\n r--;\n while (r > 1 && (r % 2)) r >>= 1;\n if (!f(op(d[r], sm))) {\n while (r < size) {\n r = (2 * r + 1);\n if (f(op(d[r], sm))) {\n sm = op(d[r], sm);\n r--;\n }\n }\n return r + 1 - size;\n }\n sm = op(d[r], sm);\n } while ((r & -r) != r);\n return 0;\n }\n\n private:\n int _n, size, log;\n std::vector<S> d;\n\n void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }\n};\n} // namespace atcoder\n\nint e() {\n return 0;\n}\n\nint op(int a, int b) {\n return max(a, b);\n}\n\nclass Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n // dp[j][x]:以数x为结尾,seq[i]!=seq[i+1]下标个数为j个时的最长序列长度\n // 遍历到当前数x时,遍历j = 0~k-1:\n // j = 0时,dp[0][x] = 1; \n // 否则,dp[j][x] = max(dp[j][x](不接当前的x), 1 + max{dp[j - 1][y], 所有的 y != x}, 1 + dp[j][x](若能连续接上))\n vector<int> a(nums);\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n auto mapping = [&](int x) -> int {\n return lower_bound(a.begin(), a.end(), x) - a.begin();\n };\n int N = a.size();\n vector<atcoder::segtree<int, op, e>> dp(k + 1, atcoder::segtree<int, op, e>(N));\n for (int x : nums) {\n x = mapping(x);\n dp[0].set(x, 1 + dp[0].get(x));\n for (int j = 1; j <= k; j++) {\n int cur = dp[j].get(x);\n int v = dp[j - 1].prod(0, x);\n v = max(v, dp[j - 1].prod(x + 1, N));\n if (cur == 0) {\n // if (v + 1 > cur) {\n dp[j].set(x, 1 + v);\n // }\n } else {\n if (v > cur) {\n dp[j].set(x, 1 + v);\n } else {\n dp[j].set(x, 1 + cur);\n }\n }\n }\n }\n int ans = 0;\n for (int i = 0; i <= k; i++) {\n ans = max(ans, dp[i].prod(0, N));\n }\n return ans;\n }\n};",
"memory": "49426"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int idx = 0;\n unordered_map<int,int> mp;\n for (int &x : nums) {\n auto it = mp.find(x);\n if (it == mp.end()) {\n mp[x] = idx;\n x = idx++;\n } else {\n x = it->second;\n }\n }\n int n = nums.size();\n vector<vector<int>> dp(k + 1, vector<int>(n));\n vector<int> mx(k + 1);\n for (int i = 0; i < n; ++i) {\n int num = nums[i];\n for (int j = k; j >= 0; --j) {\n dp[j][num] = max(dp[j][num], j ? mx[j - 1] : 0) + 1;\n mx[j] = max(mx[j], dp[j][num]);\n }\n }\n return mx[k];\n }\n};",
"memory": "52610"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<int>> dp(k+1, vector<int>(n, 0));\n unordered_map<int, int> lastSeen;\n vector<int> longest(k+1, 0);\n\n dp[0][0] = 1;\n lastSeen[nums[0]] = 0;\n longest[0] = 1;\n for (int x = 1; x < n; x++) {\n dp[0][x] = 1;\n int minPossible = min(k, x);\n\n if (lastSeen.find(nums[x]) != lastSeen.end()) {\n int y = lastSeen[nums[x]];\n \n for (int i = 0; i <= minPossible; i++) \n dp[i][x] = max(dp[i][x], dp[i][y]+1);\n }\n lastSeen[nums[x]] = x;\n\n for (int i = 1; i <= minPossible; i++) {\n dp[i][x] = max(dp[i][x], longest[i-1]+1);\n }\n\n for (int i = 0; i <= minPossible; i++) {\n longest[i] = max(longest[i], dp[i][x]);\n }\n }\n\n int maxLen = 1;\n for (int i = 0; i <= k; i++) {\n maxLen = max(maxLen, longest[i]);\n }\n return maxLen;\n }\n};",
"memory": "55794"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> dp(n);\n unordered_map<int, int> mp;\n for (int i = n - 1; ~i; --i) {\n mp[nums[i]]++;\n dp[i] = mp[nums[i]];\n }\n\n for (int i = 1; i <= k; i++) {\n vector<int> ndp(n);\n if (i == 1)\n mp.clear();\n\n for (int j = n - 1; ~j; --j) {\n dp[j] = max(dp[j], j + 1 < n ? dp[j + 1] : 0);\n }\n for (int j = n - 1; ~j; --j) {\n if (mp.count(nums[j])) {\n ndp[j] = max(ndp[j], 1 + ndp[mp[nums[j]]]);\n }\n ndp[j] = max(ndp[j], 1 + (j + 1 < n ? dp[j + 1] : 0));\n mp[nums[j]] = j;\n }\n swap(ndp, dp);\n }\n\n return *max_element(dp.begin(), dp.end());\n }\n};",
"memory": "55794"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int ans=1,n=nums.size();\n vector<vector<int>>dp(n,vector<int>(k+1,1));\n vector<int>prevsame(n,-1);\n vector<int>prevdiff(n,-1);\n vector<int> dp1(k+1,1);\n for(int i=0;i<n;i++){\n for(int j=0;j<i;j++){\n if(nums[j]==nums[i]) prevsame[i]=j;\n else prevdiff[i]=j;\n }\n }\n for(int i=1;i<n;i++){\n for(int j=k;j>=0;j--){\n if(prevsame[i]!=-1) dp[i][j] = 1+dp[prevsame[i]][j];\n if(prevdiff[i]!=-1&&j>0) dp[i][j] = max(dp[i][j],1+dp1[j-1]);\n dp1[j] = max(dp1[j],dp[i][j]);\n ans = max(ans,dp[i][j]);\n }\n }\n return ans;\n }\n};",
"memory": "58978"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int ans=1,n=nums.size();\n vector<vector<int>>dp(n,vector<int>(k+1,1));\n vector<int>prevsame(n,-1);\n vector<int>prevdiff(n,-1);\n vector<int> dp1(k+1,1);\n for(int i=0;i<n;i++){\n for(int j=0;j<i;j++){\n if(nums[j]==nums[i]) prevsame[i]=j;\n else prevdiff[i]=j;\n }\n }\n for(int i=1;i<n;i++){\n for(int j=k;j>=0;j--){\n if(prevsame[i]!=-1) dp[i][j] = 1+dp[prevsame[i]][j];\n if(prevdiff[i]!=-1&&j>0) dp[i][j] = max(dp[i][j],1+dp1[j-1]);\n dp1[j] = max(dp1[j],dp[i][j]);\n ans = max(ans,dp[i][j]);\n }\n }\n return ans;\n }\n};",
"memory": "58978"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n if(!n)\n return 0;\n int ret = 1;\n unordered_map<int,int>index;\n vector<int>max_length(k+1, 0);\n vector<vector<int>>dp(n, vector<int>(k+1, 0));\n for(int i = 0;i<n;i++){\n for(int j = k;j>=0;j--){\n if(index.count(nums[i]))\n dp[i][j] = max(dp[i][j], dp[index[nums[i]]][j]+1);\n dp[i][j] = max(dp[i][j], j?max_length[j-1]+1:1);\n max_length[j] = max(max_length[j], dp[i][j]);\n ret = max(ret, dp[i][j]);\n }\n index[nums[i]] = i;\n }\n return ret;\n }\n};",
"memory": "62161"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) \n {\n\n\n vector<int>longest(k+1,0);\n\n unordered_map<int,int>whr;\n\n vector<vector<int>>dp(nums.size(),vector<int>(k+1,0));\n\n\n dp[0][0]=1;\n\n whr[nums[0]]=0;\n\n longest[0]=1;\n\n for(int i=1;i<nums.size();i++)\n {\n dp[i][0]=1;\n\n int p=min(i,k);\n\n if(whr.find(nums[i])!=whr.end())\n {\n for(int v=0;v<=p;v++)\n {\n dp[i][v]=max(dp[i][v],dp[whr[nums[i]]][v]+1);\n }\n\n \n }\n\n whr[nums[i]]=i;\n\n for(int v=0;v<=p;v++)\n {\n if(v+1>k)\n break;\n dp[i][v+1]=max(dp[i][v+1],longest[v]+1);\n }\n\n for(int v=0;v<=p;v++)\n {\n longest[v]=max(longest[v],dp[i][v]);\n }\n\n \n\n \n\n\n\n \n }\n\n\n int ans=1;\n for(int i=0;i<=k;i++)\n ans=max(ans,longest[i]);\n\n\n return ans;\n\n\n \n \n }\n};",
"memory": "65345"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int m = nums.size();\n vector<vector<int>> dp(m, vector<int>(k + 1, 1));\n vector<int> zk(k + 1, 0);\n int ans = 0;\n unordered_map<int, int> hm;\n for (int i = 0; i < m; ++i) {\n dp[i][0] = 1;\n for (int j = 0; j <= k; ++j) {\n // zk[j]记录当前这列前几行的最大值\n dp[i][j] = max(dp[i][j], j == 0 ? 1 : zk[j - 1] + 1);\n if (hm.count(nums[i])) {\n dp[i][j] = max(dp[i][j], dp[hm[nums[i]]][j] + 1);\n }\n ans = max(ans, dp[i][j]); \n }\n for (int j = 0; j <= k; ++j) { // 需要在后面进行更新zk[j]\n zk[j] = max(zk[j], dp[i][j]);\n }\n hm[nums[i]] = i;\n }\n return ans;\n }\n};",
"memory": "65345"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int n;\n int maximumLength(vector<int>& nums, int k) {\n n = nums.size() ;\n map<int, vector<int>> m ;\n for( int i = 0 ; i < n ; i++ ) m[nums[i]].push_back( i ) ;\n vector<vector<int>> dimpi ( n , vector<int> (k+1));\n vector<int> maxi( k+1 ) ;\n for( int i = 0 ; i <= k ; i++ ){\n dimpi[n-1][i] = 1 ;\n maxi[i] = 1 ;}\n int ans = 1;\n for( int i = n-2 ; i >= 0 ; i-- ){\n int ind = n ;\n auto it = upper_bound(m[nums[i]].begin() , m[nums[i]].end() , i );\n if( it != m[nums[i]].end() ) ind = *it ;\n for( int j = 0 ; j <= k ; j++ ){\n dimpi[i][j] = 1 ;\n if( ind != n ){ dimpi[i][j] = 1 + dimpi[ind][j]; }\n if( j ){ dimpi[i][j] = max( dimpi[i][j] , 1 + maxi[j-1]);}\n ans = max( ans , dimpi[i][j] );\n }\n for( int j = 0 ; j <= k ; j++ ) maxi[j] = max( dimpi[i][j] , maxi[j]);\n }\n return ans ;\n }\n};",
"memory": "68529"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n=nums.size();\n unordered_map<int,vector<int>>m;\n vector<int>v(k+1,0);\n for(int i=n-1;i>=0;i--){\n vector<int>c(k+1,0);\n c[0]=1;\n for(int j=k-1;j>=0;j--){\n if(v[j]!=0) v[j+1]=max(v[j+1],v[j]+1);\n c[j+1]=v[j]+1;\n }\n v[0]=max(v[0],1);\n if(m.find(nums[i])!=m.end()){\n for(int j=0;j<m[nums[i]].size();j++){\n if(m[nums[i]][j]!=0){ \n v[j]=max(v[j],m[nums[i]][j]+1);\n c[j]=max(c[j],m[nums[i]][j]+1);\n }\n }\n }\n m[nums[i]]=c;\n if(v[0]==3) cout<<i<<\" \";\n }\n int ans=1;\n for(int i=0;i<=k;i++){\n ans=max(ans,v[i]);\n }\n return ans;\n }\n};",
"memory": "68529"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n=nums.size();\n unordered_map<int,vector<int>>m;\n vector<int>v(k+1,0);\n // v[0]=1;\n for(int i=n-1;i>=0;i--){\n vector<int>c(k+1,0);\n c[0]=1;\n for(int j=k-1;j>=0;j--){\n if(v[j]!=0) v[j+1]=max(v[j+1],v[j]+1);\n // if(i==3) cout<<v[j]<<\" \";\n c[j+1]=v[j]+1;\n }\n v[0]=max(v[0],1);\n // if(i==4) for(int x:v) cout<<x<<\" \";\n if(m.find(nums[i])!=m.end()){\n for(int j=0;j<m[nums[i]].size();j++){\n if(m[nums[i]][j]!=0){ \n v[j]=max(v[j],m[nums[i]][j]+1);\n c[j]=max(c[j],m[nums[i]][j]+1);\n }\n // if(i==0) cout<<m[nums[i]][j]<<\" \";\n // if(i==0) cout<<\n }\n }\n m[nums[i]]=c;\n // if(i==3) for(int x:v) cout<<x<<\" \";\n // if(k>=2 and v[2]==5) cout<<i<<\" \";\n // if(i==0 and k>=2) cout<<v[2];\n // if(i==4)\n if(v[0]==3) cout<<i<<\" \";\n }\n int ans=1;\n for(int i=0;i<=k;i++){\n ans=max(ans,v[i]);\n // if(v[i]==5) cout<<i;\n }\n return ans;\n }\n};",
"memory": "71713"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n vector<vector<int>> dp(nums.size(), vector<int>(k+1, 1));\n vector<int> mx(k+1, 0);\n unordered_map<int, vector<int>> m;\n int ans = 1;\n\n for (int i = 0; i < nums.size(); i++) {\n if (m.count(nums[i]) == 0) {\n m[nums[i]] = vector<int>(k+1, 0);\n }\n\n for (int kk = k; kk >= 0; kk--) {\n if (kk > 0)\n dp[i][kk] = max(dp[i][kk], 1+mx[kk-1]);\n \n dp[i][kk] = max(dp[i][kk], 1+m[nums[i]][kk]);\n m[nums[i]][kk] = max(m[nums[i]][kk], dp[i][kk]);\n //cout << i << \" \" << kk << \" \" << dp[i][kk] << \"\\n\"; \n mx[kk] = max(mx[kk], dp[i][kk]);\n ans = max(ans, dp[i][kk]);\n }\n }\n\n return ans;\n }\n};",
"memory": "71713"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n= nums.size();\n int res=0;\n map<int,int>m;//to map the distinct number to indices which we can use latter\n int cnt=0;\n for(int i=0;i<nums.size();i++)\n {\n if(m.find(nums[i])==m.end())\n {\n //not assigned index yet\n m[nums[i]]=cnt;\n cnt++;\n }\n }\n vector<vector<int>>dp(n,vector<int>(k+1,1));\n // vector<pair<int,int>>mx(k+1,0);\n vector<vector<int>>mx(cnt+1,vector<int>(k+1,0));//to store max value available till now for each index\n vector<int>mx2(k+1,0);\n for(int i=n-1;i>=0;i--)\n {\n for(int j=k;j>=0;j--)\n {\n // for(int l=i+1;l<n;l++)\n // {\n // if(nums[i]==nums[l]) dp[i][j]=max(dp[i][j],1+dp[l][j]);\n // else if(j+1<=k) dp[i][j]=max(dp[i][j],1+dp[l][j+1]); \n // }\n if(i==n-1) dp[i][j]=1;\n else\n {\n dp[i][j]=max(dp[i][j],1+mx[m[nums[i]]][j]);\n if(j+1<=k) dp[i][j]=max(dp[i][j],1+mx2[j+1]);\n }\n res=max(res,dp[i][j]);\n }\n for(int j=k;j>=0;j--)\n {\n mx[m[nums[i]]][j]=max(mx[m[nums[i]]][j],dp[i][j]);\n mx2[j]=max(mx2[j],dp[i][j]);\n }\n }\n return res;\n }\n};",
"memory": "74896"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n// using namespace __gnu_pbds;\n #define vb vector<bool>\n #define ff first\n #define ss second\n #define pb push_back\n #define gout(tno) cout << \"Case #\" << tno++ <<\": \"\n #define ld long double\n #define ll long long\n #define f(i, a, b) for (int(i) = int(a); (i) < int(b); ++(i))\n #define vi vector<int>\n #define vb vector<bool>\n #define pb push_back\n #define ub upper_bound\n #define lb lower_bound\n #define rall(x) x.rbegin(), x.rend()\n #define uniq(v) v.resize(unique(v.begin(), v.end()) - v.begin())\n #define scanv(v) for (int i = 0; i < v.size(); ++i) cin >> v[i];\n // #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>\n //order_of_key (k) //find_by_order(k) \n #define prDouble(x) cout<<fixed<<setprecision(10)<<x\n #define pii pair<int, int>\n #define vpii vector<pair<int, int> >\n #define w(x) int x; cin >> x; while(x--)\n #define FIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n #define setbits(x) __builtin_popcountll(x) //count number of setbits in a number\n #define max3(a, b, c) max((a), max((b), (c)))\n #define min3(a, b, c) min((a), min((b), (c)))\n #define mx_all(c) *max_element((c).begin(), (c).end())\n #define mn_all(c) *min_element((c).begin(), (c).end())\n #define all(x) x.begin(),x.end()\n #define siz(x) ((int)(x).size())\n #define yes cout<<\"Yes\"<<endl\n #define no cout<<\"No\"<<endl\n #define alice cout<<\"Alice\"<<endl\n #define bob cout<<\"Bob\"<<endl\n #define takahashi cout<<\"Takahashi\"<<endl\n #define aoki cout<<\"Aoki\"<<endl\n #define pb push_back\n #define vi vector<int>\n #define vb vector<bool>\n #define vs vector<string>\n #define vvi vector<vector<int>>\n #define djikstra priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>>\n #define lld long double\n #define show(a) for (auto& (i) : (a)) cout << i<<\" \" ;\n #define itos to_string\n #define STOI stoi\n \n const lld pi = 3.1415926535897932;\n ll mul1(ll a,ll b,ll m){ll ans=0;while(b>0){if(b&1)ans=(ans+a)%m;b=b/2;a=(a+a)%m;}return ans;}\n ll mul(ll a,ll b,ll m){ return (a*b)%m;}\n ll accurateFloor(ll a, ll b) {ll val = a / b; while (val * b > a)val--;return val; }\n void yesno(bool xxx) {if(xxx) cout<<\"Yes\\n\"; else cout<<\"No\\n\";}\n ll nCr(ll n, ll r){if (n < r)return 0; if (r > n - r) r = n - r; ll ans = 1;ll i; for (i = 1; i <= r; i++) { ans *= n - r + i; ans /= i; } return ans;}\n ll binaryexp(ll a,ll b){ll ans=0;if(b<0)return 0; if(b==0)return 1;else if(b==1)return a; else if(b%2){ans=(a*(binaryexp((a*a),b/2)));}else ans=binaryexp((a*a),b/2);return ans;}\n int gcd(int x,int y){if(y==0)return x;else return gcd(y,x%y);}\n \n long long gcd(long long int a, long long int b) {if (b == 0) return a; return gcd(b, a % b);}\n \n // Function to return LCM of two numbers \n long long lcm(ll a, ll b){ return (a / gcd(a, b)) * b;}\n ll mod_add(ll a, ll b, ll m=1e9+7) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}\n ll expo(ll a, ll b, ll m) {ll res = 1; while (b > 0) {if (b & 1)res = mul(res , a,m) % m; a = mul(a , a,m) % m; b = b >> 1;} return res%m ;}\n ll modinv(ll a , ll m ) {return expo(a , m-2 , m)%m;} // m is prime /**\n// for questions involving segments, think of sweep line algorithm and binary search,\n// if segment tree with lazy prop^ does not seem to work .\n// think greedy wisely and not rush it over the algorithm\n// Use DSU for dynamically varying graphs,expanding compressing tree and cycles\n// for(int s=m;s;s=(s-1)&m) iterating through all subsets of mask m\n// Think about topological sortings whenever you see some sort of order or maybe independency\n// Think of Rolling Hash if Your solution is giving space or time limit exceeded for string matching\n// KMP algo for single pattern searching in text and Corasick algo for multiple pattern searching in Text.\n\nconst ll mod= 1e9+7;\n //1e9+7 //998244353\nconst ll mx=5e4+1,N=1e3+10;\nconst ll inf=1e17;\n\nclass Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n map<int,int> hsh;\n int n=nums.size();\n f(i,0,n) hsh[nums[i]]++;\n vector<int> v;\n int cnt=1;\n for(auto &it:hsh){\n it.ss=cnt++;\n }\n f(i,0,n) v.pb(hsh[nums[i]]);\n n=v.size();\n // show(v);\n // cout<<endl;\n // return 0;\n\n vector<vector<int>> row(n,vector<int>(k+1,0));\n vector<vector<int>> value(cnt+1,vector<int>(k+1,0));\n value[v[0]][0]=1;\n // return 0;\n\n\n int dp[n][k+1];//element placed at ith index of subsequence with j inversions-> max length\n f(i,0,n) f(j,0,k+1) dp[i][j]=0;\n f(i,0,n)dp[i][0]=1;\n row[0][0]=1;\n // f(i,1,n) row[i][0]=row[i-1][0];\n // return 0;\n // return 0;\n f(i,1,n){\n f(j,0,k+1){\n int ans=1;\n if(j) ans=max(ans,row[i-1][j-1]+1);\n ans=max(ans,value[v[i]][j]+1);\n dp[i][j]=ans;\n row[i][j]=max3(row[i][j],dp[i][j],row[i-1][j]);\n value[v[i]][j]=max(value[v[i]][j],dp[i][j]);\n\n // f(k,0,i){\n // if(v[i]==v[k])\n // dp[i][j]=max(dp[i][j],dp[k][j]+1);\n // if(j>=1) dp[i][j]=max(dp[i][j],dp[k][j-1]+1);\n // }\n }\n }\n // f(i,0,n){\n // f(j,0,k+1){\n // cout<<dp[i][j]<<\" \";\n // }\n // cout<<endl;\n // }cout<<endl;\n\n // f(i,0,n){\n // f(j,0,k+1){\n // cout<<row[i][j]<<\" \";\n // }\n // cout<<endl;\n // }\n // cout<<endl;\n\n // f(i,0,n){\n // f(j,0,k+1){\n // cout<<value[i][j]<<\" \";\n // }\n // cout<<endl;\n // }\n\n int ans=0;\n f(i,0,n) f(j,0,k+1) ans=max(ans,dp[i][j]); \n return ans;\n\n\n\n\n\n\n\n\n\n\n\n\n \n }\n};\n\n\n// int main(){\n// int n,k;\n// cin>>n>>k;\n// vector<int> v(n);\n// f(i,0,n) cin>>v[i];\n// Solution s;\n// cout<<s.maximumLength(v,k)<<endl;;\n\n// }",
"memory": "78080"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& a, int k) {\n int n=a.size() ;\n vector<vector<pair<int,int>>> dp(n , vector<pair<int,int>> (k+1)) ;\n for(int e=0 ;e<=k;e++){\n dp[n-1][e] ={1,1} ;\n }\n unordered_map<int, vector<int>> val_indx ;\n for(int e=0 ;e<n ;e++){\n val_indx[a[e]].push_back(e) ;\n }\n for(int e=n-2 ;e>=0 ;e--){\n int next_indx = upper_bound(val_indx[a[e]].begin() ,val_indx[a[e]].end(),e) - val_indx[a[e]].begin() ;\n dp[e][0].first = 1 ;\n if(next_indx!= val_indx[a[e]].size()) dp[e][0].first +=dp[val_indx[a[e]][next_indx]][0].first ;\n dp[e][0].second = max(dp[e][0].first,dp[e+1][0].second) ;\n for(int ki =1 ;ki<=k ;ki++){\n if(next_indx!=val_indx[a[e]].size()){\n dp[e][ki].first = max(dp[val_indx[a[e]][next_indx]][ki].first+1,dp[e+1][ki-1].second+1) ; \n }\n else {\n dp[e][ki].first = dp[e+1][ki-1].second+1 ;\n }\n dp[e][ki].second = max(dp[e][ki].first,dp[e+1][ki].second) ;\n }\n }\n return dp[0][k].second ;\n }\n};",
"memory": "78080"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<int>> dp(k + 1, vector<int>(n));\n vector<deque<pair<int,int>>> best(k + 1);\n unordered_map<int, vector<int>> mp;\n for(int i = 0 ; i < n; ++i){\n mp[nums[i]].push_back(i);\n }\n int longest = 0;\n for(int i = n - 1 ; i >= 0 ; --i) {\n int value = nums[i];\n for(int j = 0 ; j <= k ; j ++) {\n auto itr = upper_bound(mp[value].begin(), mp[value].end(), i);\n dp[j][i] = 1;\n if (itr != mp[value].end()) {\n dp[j][i] = dp[j][*itr] + 1;\n }\n if (j > 0) {\n for(auto pair : best[j - 1]) {\n if (pair.first != value) {\n dp[j][i] = max(dp[j][i], 1 + pair.second);\n break;\n }\n }\n }\n if (!best[j].empty()) {\n if (dp[j][i] >= best[j][0].second) {\n if (value == best[j][0].first){\n best[j][0].second = dp[j][i];\n } else {\n best[j].emplace_front(value, dp[j][i]);\n }\n } else if (best[j].size() == 2 && dp[j][i] >= best[j][1].second) {\n if (value == best[j][1].first){\n best[j][1].second = dp[j][i];\n } else {\n best[j].pop_back();\n best[j].emplace_back(value, dp[j][i]);\n }\n }\n } else {\n best[j].emplace_back(value, dp[j][i]);\n }\n longest = max(longest, dp[j][i]);\n }\n }\n // for(auto &u : dp){\n // for(auto &c : u) {\n // cout << c << \" \";\n // }\n // cout << endl;\n // }\n return longest;\n }\n};",
"memory": "81264"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n \n\n\n int maximumLength(vector<int>& nums, int k) {\n int n=nums.size();\n vector<vector<int>>dp(n,vector<int>(k+1,0));\n\n vector<int>prev(k+1,0);\n for(int i=0;i<=k;i++){\n dp[0][i]=1;\n prev[i]=1;\n }\n map<int,int>m;\n m[nums[0]]=0;\n for(int i=1;i<n;i++){\n vector<int>curr(k+1,0);\n for(int j=0;j<=k;j++){\n int val=0;\n if(m.find(nums[i])!=m.end()){\n val=dp[m[nums[i]]][j];\n\n }\n int val1=0;\n if(j-1>=0){\n val1=prev[j-1];\n }\n\n int faval=max(val+1,val1+1);\n dp[i][j]=faval;\n curr[j]=max(prev[j],faval); \n \n\n }\n prev=curr;\n\n m[nums[i]]=i;\n }\n\n int fa=0;\n for(int i=0;i<=k;i++){\n fa=max(fa,prev[i]);\n }\n\n return fa;\n\n }\n};",
"memory": "84448"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<int>> dp(n, vector<int>(k+1, 0));\n vector<vector<int>> maxDp(n, vector<int>(k+1, 0));\n\n // base case\n for (int j = 0; j < k+1; j++) {\n dp[0][j] = 1;\n maxDp[0][j] = max(maxDp[0][j], dp[0][j]);\n }\n\n unordered_map<int, int> prevIdxMap;\n prevIdxMap[nums[0]] = 0;\n\n for (int i = 1; i < n; i++) {\n for (int j = 0; j < k+1; j++) {\n if (j == 0) {\n dp[i][j] = 1;\n } else {\n dp[i][j] = maxDp[i-1][j-1]+1;\n }\n if (prevIdxMap.count(nums[i])) {\n auto idx = prevIdxMap[nums[i]];\n dp[i][j] = max(dp[i][j], dp[idx][j]+1);\n }\n maxDp[i][j] = max(dp[i][j], maxDp[i-1][j]);\n }\n prevIdxMap[nums[i]] = i;\n }\n\n return max(dp[n-1][k], maxDp[n-1][k]);\n }\n};",
"memory": "87631"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n\n vector<vector<int>> dp(n, vector<int>(k+1, 0));\n vector<vector<int>> maxLength(n, vector<int>(k+1, 0));\n\n // base case\n for (int j = 0; j < k+1; j++) {\n dp[0][j] = 1;\n maxLength[0][j] = 1;\n }\n\n unordered_map<int, int> m;\n m[nums[0]] = 0;\n\n for (int i = 1; i < n; i++) {\n for (int j = 0; j < k+1; j++) {\n if (j == 0) {\n dp[i][j] = 1;\n } else {\n dp[i][j] = maxLength[i-1][j-1] + 1;\n }\n if (m.count(nums[i])) {\n auto idx = m[nums[i]];\n dp[i][j] = max(dp[i][j], dp[idx][j] + 1);\n }\n maxLength[i][j] = max(dp[i][j], maxLength[i-1][j]);\n }\n m[nums[i]] = i;\n }\n\n return max(dp[n-1][k], maxLength[n-1][k]);\n }\n};",
"memory": "90815"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n\n vector<vector<int>> dp(n, vector<int>(k+1, 0));\n vector<vector<int>> maxVal(n, vector<int>(k+1, 0));\n\n // base case\n for (int j = 0; j < k+1; j++) {\n dp[0][j] = 1;\n maxVal[0][j] = 1;\n }\n unordered_map<int, int> m;\n m[nums[0]] = 0;\n\n for (int i = 1; i < n; i++) {\n for (int j = 0; j < k+1; j++) {\n if (j == 0) {\n dp[i][j] = 1;\n } else {\n dp[i][j] = maxVal[i-1][j-1] + 1;\n }\n if (m.count(nums[i])) {\n auto idx = m[nums[i]];\n dp[i][j] = max(dp[i][j], dp[idx][j]+1);\n }\n maxVal[i][j] = max(dp[i][j], maxVal[i-1][j]);\n }\n m[nums[i]] = i;\n }\n\n return max(maxVal[n-1][k], dp[n-1][k]);\n }\n};",
"memory": "90815"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n int ans = 0;\n vector <vector <int>> pickdp(n+1,vector <int> (k+1,0));\n vector <vector <int>> noPickdp(n+1,vector <int> (k+1,0));\n map <int,int> lastEqual;\n for(int i = 1; i <= n; ++i)\n {\n for(int j = 0; j <= k; ++j)\n {\n pickdp[i][j] = 1;\n \n if(lastEqual.find(nums[i-1])!=lastEqual.end())\n pickdp[i][j] = max(pickdp[i][j], pickdp[lastEqual[nums[i-1]]][j] + 1);\n \n if(j)\n {\n pickdp[i][j] = max(pickdp[i][j], pickdp[i-1][j-1] + 1);\n pickdp[i][j] = max(pickdp[i][j], noPickdp[i-1][j-1] + 1);\n }\n \n noPickdp[i][j] = max(pickdp[i-1][j] , noPickdp[i-1][j]);\n ans = max({ans,pickdp[i][j],noPickdp[i][j]});\n // cout << dp[i][j] << \" \";\n }\n // cout << endl;\n lastEqual[nums[i-1]] = i;\n }\n return ans;\n }\n};",
"memory": "93999"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n // If k is large, we pigeonhole the maximum length\n if (k >= nums.size()-1){ \n return nums.size();\n }\n\n // Create our variables to help us calculate\n // Map to store last location\n map<int, int> last{{}};\n last[nums[0]] = 0;\n\n // To store if we include current char\n vector<vector<int>> val(nums.size(), vector<int>(k+1, 1));\n // To store if we do not include current char\n vector<vector<int>> val_(nums.size(), vector<int>(k+1, 0));\n // Running maximum\n std::vector<int> maxval(k+1, 1);\n\n // Main code to populate val and val_\n for (int i = 1; i < nums.size(); i++){\n // Check to see if we've observed our item inside of map before\n if (last.contains(nums[i])){\n for (int j = 0; j < k+1; j++){\n val[i][j] = val[last[nums[i]]][j] + 1;\n }\n }\n\n // Update map\n last[nums[i]] = i;\n\n // Update the k=0 case for skipping current char\n val_[i][0] = max(val_[i-1][0],val[i-1][0]);\n\n // Update val_ and val\n for (int j = 1; j < k+1; j++){\n // Case where we skip should be the larger of skipping previous or including previous\n val_[i][j] = max(val_[i-1][j], val[i-1][j]);\n\n // Case where we do not skip should be larger of 3 things:\n // 1: k-1 case where previous letter was skipped\n // 2: k-1 case where previous letter was not skipped\n // 3: k case of the last occurrence of the same letter \n val[i][j] = max(val[i-1][j-1]+1, val[i][j]);\n val[i][j] = max(val_[i-1][j-1]+1, val[i][j]);\n }\n // Update running maximum\n for (int j = 0; j < k+1; j++){\n maxval[j] = max(maxval[j],val[i][j]);\n }\n }\n\n // // Code to print the array if you need help visualizing! \n // cout<<\"[\";\n // for (int j = 0; j < k+1; j++){\n // cout<<\"[\";\n // for (int i = 0; i < nums.size(); i++){\n // cout<<val[i][j];\n // if (i != nums.size()-1){\n // cout<<\", \";\n // }\n // }\n // cout<<\"]\";\n // if (j != k){\n // cout<<\",\\n\";\n // }\n // }\n // cout<<\"]\";\n\n // Get maximum of running maximum\n int max_val = 0;\n for (int i = 0; i < maxval.size(); i++){\n max_val = max(max_val, maxval[i]);\n }\n return max_val;\n }\n\n};",
"memory": "93999"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_map<int, int> dp;\n vector<int> prevMax(n);\n vector<int> curMax(n);\n\n for(int i=0; i<=k; i++) {\n for(int j=n-1; j>=0; j--) {\n dp[nums[j]] = max(\n dp[nums[j]] + 1,\n j!=n-1 ? prevMax[j+1] + 1 : 0\n );\n\n curMax[j] = max(\n j!=n-1 ? curMax[j+1] : 0, \n dp[nums[j]]\n );\n }\n dp.clear();\n prevMax = curMax;\n }\n\n return *max_element(curMax.begin(), curMax.end());\n }\n};",
"memory": "97183"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n unordered_map<int, int> pre; // the previous index of same number\n\t\tint n = nums.size();\n\t\tvector<vector<vector<int>>> dp(n, vector<vector<int>>(2, vector<int>(k+1, 0)));\n\t\tdp[0][1][0] = 1; pre[nums[0]] = 0;\n\t\t\n\t\tfor (int i = 1; i<n; ++i){\n\t\t\t// drop case\n\t\t\tfor (int j=0; j<=k; ++j){\n\t\t\t\tdp[i][0][j] = max(dp[i-1][0][j], dp[i-1][1][j]); // drop, not increase k, just take the max will do.\n\t\t\t}\n\t\t\t\n\t\t\t// keep case\n\t\t\tauto itr = pre.find(nums[i]);\n\t\t\tfor (int j=0; j<=k; ++j)\n\t\t\t\tdp[i][1][j] = max( (itr == pre.end() ? 0 : dp[itr->second][1][j]), (j==0 ? 0 : dp[i][0][j-1]) )+1;\n\t\t\tpre[nums[i]] = i;\n\n //cout << i << \" \" << dp[i][0][0] << \" \" << dp[i][1][0] << endl;\n\t\t}\n\t\treturn max(*max_element(dp[n-1][0].begin(), dp[n-1][0].end()), *max_element(dp[n-1][1].begin(), dp[n-1][1].end())); \n }\n};",
"memory": "97183"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n\n int fn(int i, int W, bool issame, vector<int>& nums, unordered_map<int, vector<int>>& mp, vector<vector<vector<int>>>& dp) {\n\n if (i < 0) return 0; // Base case: if we go before the first element, return 0\n if (dp[i][issame][W] != -1) return dp[i][issame][W];\n int p = 0, np = 0;\n\n vector<int>& v = mp[nums[i]];\n int ind = lower_bound(v.begin(), v.end(), i) - v.begin();\n\n // Option 1: Take the current element\n if (ind == 0) {\n // If there's no previous occurrence of nums[i], take it and end here\n p = 1;\n } else {\n // Take the current element and move to the previous occurrence\n p = 1 + fn(v[ind - 1], W, true, nums, mp, dp);\n }\n\n // Option 2: Use one switch (if available) and pick the previous different element\n if (W > 0) {\n // Allow switching (i.e., move to the previous index and reduce the count of allowed switches)\n p = max(p, 1 + fn(i - 1, W - 1, false, nums, mp, dp));\n }\n\n // Option 3: Don't take the current element and move on\n if (!issame) {\n np = fn(i - 1, W, false, nums, mp, dp);\n }\n\n return dp[i][issame][W] = max(p, np);\n }\n\n int maximumLength(vector<int>& nums, int W) {\n int n = nums.size();\n\n vector<vector<vector<int>>> dp(n, vector<vector<int>>(2, vector<int>(W + 1, -1)));\n\n unordered_map<int, vector<int>> mp;\n for (int i = 0; i < nums.size(); i++) {\n mp[nums[i]].push_back(i);\n }\n\n return fn(n - 1, W, false, nums, mp, dp); // Start from the last element\n }\n};\n",
"memory": "100366"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n // Recursive function with memoization\n int solve(int i, int W, bool issame, vector<int>& nums, unordered_map<int, vector<int>>& mp, vector<vector<vector<int>>>& dp) {\n // Base case: if we reach the end of nums, return 0\n if (i >= nums.size()) return 0;\n\n // Check if the result is already computed\n if (dp[i][issame][W] != -1) return dp[i][issame][W];\n\n int p = 0, np = 0;\n\n // Get the vector of indices where nums[i] appears\n vector<int>& v = mp[nums[i]];\n // Find the next index after 'i' where nums[i] appears\n int ind = upper_bound(v.begin(), v.end(), i) - v.begin();\n\n // Option 1: Take the current element\n if (ind == v.size()) {\n // If there's no next occurrence of nums[i], take it and end here\n p = 1;\n } else {\n // Take the current element and move to the next occurrence\n p = 1 + solve(v[ind], W, true, nums, mp, dp);\n }\n\n // Option 2: Use one switch (if available) and pick the next different element\n if (W > 0) {\n // Allow switching (i.e., move to the next index and reduce the count of allowed switches)\n p = max(p, 1 + solve(i + 1, W - 1, false, nums, mp, dp));\n }\n\n // Option 3: Don't take the current element and move on\n if (!issame) {\n np = solve(i + 1, W, false, nums, mp, dp);\n }\n\n // Store the result in the DP table and return the maximum value\n return dp[i][issame][W] = max(p, np);\n }\n\n // Main function to find the maximum length of subsequence with at most W switches\n int maximumLength(vector<int>& nums, int W) {\n int n = nums.size();\n\n // Initialize a 3D DP table with dimensions [n][2][W+1] filled with -1\n vector<vector<vector<int>>> dp(n, vector<vector<int>>(2, vector<int>(W + 1, -1)));\n\n // Build the unordered_map to store the indices of each element in nums\n unordered_map<int, vector<int>> mp;\n for (int i = 0; i < nums.size(); i++) {\n mp[nums[i]].push_back(i);\n }\n\n // Start the recursive solve function from the first index\n return solve(0, W, false, nums, mp, dp);\n }\n};\n",
"memory": "103550"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class segtree {\npublic:\n struct node {\n // don't forget to set default value (used for leaves)\n // not necessarily neutral element!\n int tag = 0, mx = 0;\n\n void apply(int l, int r, int v) {\n tag = v;\n mx = (r - l + 1) * v;\n }\n };\n\n node unite(const node &a, const node &b) const {\n node res;\n res.mx = max(a.mx, b.mx);\n return res;\n }\n\n inline void push(int x, int l, int r) {\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n // push from x into (x + 1) and z\n // if (tree[x].tag != 0) {\n // tree[x + 1].apply(l, y, tree[x].tag);\n // tree[z].apply(y + 1, r, tree[x].tag);\n // tree[x].tag = 0;\n // }\n }\n\n inline void pull(int x, int z) {\n tree[x] = unite(tree[x + 1], tree[z]);\n }\n\n int n;\n vector<node> tree;\n\n void build(int x, int l, int r) {\n if (l == r) {\n return;\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n build(x + 1, l, y);\n build(z, y + 1, r);\n pull(x, z);\n }\n\n template <typename M>\n void build(int x, int l, int r, const vector<M> &v) {\n if (l == r) {\n tree[x].apply(l, r, v[l]);\n return;\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n build(x + 1, l, y, v);\n build(z, y + 1, r, v);\n pull(x, z);\n }\n\n node get(int x, int l, int r, int ll, int rr) {\n if (ll <= l && r <= rr) {\n return tree[x];\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n push(x, l, r);\n node res{};\n if (rr <= y) {\n res = get(x + 1, l, y, ll, rr);\n } else {\n if (ll > y) {\n res = get(z, y + 1, r, ll, rr);\n } else {\n res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr));\n }\n }\n pull(x, z);\n return res;\n }\n\n template <typename... M>\n void modify(int x, int l, int r, int ll, int rr, const M&... v) {\n if (ll <= l && r <= rr) {\n tree[x].apply(l, r, v...);\n return;\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n push(x, l, r);\n if (ll <= y) {\n modify(x + 1, l, y, ll, rr, v...);\n }\n if (rr > y) {\n modify(z, y + 1, r, ll, rr, v...);\n }\n pull(x, z);\n }\n\n segtree(int _n) : n(_n) {\n assert(n > 0);\n tree.resize(2 * n - 1);\n build(0, 0, n - 1);\n }\n\n template <typename M>\n segtree(const vector<M> &v) {\n n = v.size();\n assert(n > 0);\n tree.resize(2 * n - 1);\n build(0, 0, n - 1, v);\n }\n\n node get(int ll, int rr) {\n // assert(0 <= ll && ll <= rr && rr <= n - 1);\n return get(0, 0, n - 1, ll, rr);\n }\n\n node get(int p) {\n // assert(0 <= p && p <= n - 1);\n return get(0, 0, n - 1, p, p);\n }\n\n template <typename... M>\n void modify(int ll, int rr, const M&... v) {\n // assert(0 <= ll && ll <= rr && rr <= n - 1);\n modify(0, 0, n - 1, ll, rr, v...);\n }\n};\n\nclass Solution {\n int pre[5002];\npublic:\n int maximumLength(vector<int>& nums, int k) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int n = nums.size();\n \n unordered_map<int, int> pos;\n for (int i = 0; i < n; i++) {\n if (pos.find(nums[i]) == pos.end()) {\n pre[i] = 0;\n } else {\n pre[i] = pos[nums[i]];\n }\n pos[nums[i]] = i + 1;\n }\n \n vector<segtree> trees(k + 1, segtree(n + 1));\n int ans = 0;\n for (int i = 1; i <= n; i++) {\n for (int c = 0; c <= k; c++) {\n trees[c].modify(i, i, \n max(trees[c].get(i, i).mx,\n trees[c].get(pre[i - 1], pre[i - 1]).mx + 1)\n );\n \n trees[c].modify(i, i,\n max(trees[c].get(i, i).mx,\n (c == 0 ? 0 : trees[c - 1].get(min(i - 1, pre[i - 1] + 1), i - 1).mx) + 1)\n );\n ans = max(ans, trees[c].get(i, i).mx);\n }\n }\n return ans;\n }\n};\n",
"memory": "106734"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n=nums.size();\n vector<vector<int>> dp(k+1,vector<int>(n,1));\n unordered_map<int,int> mp;\n for(int i=n-1;i>=0;i--){\n dp[0][i]=max(1,mp[nums[i]]+1);\n mp[nums[i]]++;\n }\n\n for(int t=1;t<=k;t++){\n dp[t][n-1]=1;\n }\n for(int t=1;t<=k;t++){\n mp.clear();\n mp[nums[n-1]]=n-1;\n int maxi=dp[t-1][n-1];\n for(int i=n-2;i>=0;i--){\n if(mp.find(nums[i])!=mp.end()){\n dp[t][i]=1+dp[t][mp[nums[i]]];\n }\n dp[t][i]=max(dp[t][i],1+maxi);\n maxi=max(maxi,dp[t-1][i]);\n mp[nums[i]]=i;\n }\n }\n int ans=0;\n for(int t=0;t<=k;t++){\n for(int i=0;i<n;i++){\n ans=max(ans,dp[t][i]);\n }\n }\n return ans;\n }\n};",
"memory": "109918"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n vector<vector<int>> dp(k+1,vector<int>(nums.size(),0));\n unordered_map<int,int> mp;\n vector<int> imax(nums.size(),0); // store the maximum value for even rows\n vector<int> imax2(nums.size(),0);\n int cur_max = 0;\n for(int i=0;i<nums.size();i++){\n int num = nums[i];\n mp[num]++;\n dp[0][i]=mp[num];\n cur_max = max(cur_max,dp[0][i]);\n imax[i]=cur_max;\n }\n for(int i=1;i<=k;i++){\n mp.clear();\n cur_max = 0;\n for(int j=0;j<nums.size();j++){\n dp[i][j]=1;\n int num = nums[j];\n if(mp.count(num)){\n dp[i][j] = mp[num]+1;\n }\n if(j>0){\n if(i%2){\n dp[i][j] = max(dp[i][j],imax[j-1]+1);\n }\n else{\n dp[i][j] = max(dp[i][j],imax2[j-1]+1);\n }\n \n }\n mp[num]=dp[i][j];\n cur_max = max(cur_max,dp[i][j]);\n if(i%2){\n imax2[j]=cur_max;\n }\n else{\n imax[j]=cur_max;\n }\n }\n }\n return cur_max;\n }\n};",
"memory": "113101"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n // int n=int(nums.size());\n // vector<vector<int>> dp(n,vector<int>(k+1,INT_MIN));\n // dp[0][0]=1;\n // int res=1;\n // for (int i=1; i<n; ++i) {\n // dp[i][0]=1;\n // for (int c=0; c<=k; ++c) {\n // for (int j=0; j<i; ++j) {\n // if (nums[j]==nums[i]) \n // dp[i][c]=max(dp[i][c],dp[j][c]+1);\n // else if (c>0)\n // dp[i][c]=max(dp[i][c],dp[j][c-1]+1);\n // }\n // res=max(res,dp[i][c]);\n // }\n // }\n // return res;\n int n=int(nums.size());\n unordered_map<int,vector<int>> dpx;\n vector<int> s0(k+1,INT_MIN);\n s0[0]=1;\n dpx[nums[0]]=s0;\n vector<map<int,int>> dpa(k+1);\n for (int c=0; c<=k; ++c)\n dpa[c][s0[c]]=nums[0];\n int res=1;\n for (int i=1; i<n; ++i) {\n int x=nums[i];\n vector<int> s(k+1,INT_MIN);\n s[0]=1;\n if (dpx.count(x)) {\n for (int c=0; c<=k; ++c)\n s[c]=max(s[c],dpx[x][c]+1);\n }\n for (int c=1; c<=k; ++c) {\n auto b=dpa[c-1].rbegin();\n while (b!=dpa[c-1].rend() and b->second==x) ++b;\n if (b!=dpa[c-1].rend())\n s[c]=max(s[c],b->first+1);\n }\n dpx[x]=s;\n for (int c=0; c<=k; ++c) {\n res=max(res,s[c]);\n dpa[c][s[c]]=x;\n }\n }\n return res;\n }\n};",
"memory": "116285"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n vector <pair<int,unordered_map<int,int>>> dp(k+1);int ans=0;\n for(int i=0;i<nums.size();i++){\n for(int j=min(k,i);j>=0;j--){\n int prev=-1;int curr=-1;\n if(j!=0)\n prev=dp[j-1].first+1;\n curr=dp[j].second[nums[i]]=max(dp[j].second[nums[i]]+1,prev);\n dp[j].first=max(dp[j].first,max(curr,prev));\n ans=max(ans,dp[j].first);\n }\n } \n return ans;\n }\n};",
"memory": "119469"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n vector <pair<int,unordered_map<int,int>>> dp(k+1);int ans=0;\n for(int i=0;i<nums.size();i++){\n for(int j=min(k,i);j>=0;j--){\n int prev=-1;int curr=-1;\n if(j)\n prev=dp[j-1].first+1;\n curr=dp[j].second[nums[i]]=max(dp[j].second[nums[i]]+1,prev);\n dp[j].first=max(dp[j].first,curr);\n ans=max(ans,dp[j].first);\n }\n } \n return ans;\n }\n};",
"memory": "122653"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n vector <pair<int,unordered_map<int,int>>> dp(k+1);int ans=0;\n for(int i=0;i<nums.size();i++){\n for(int j=min(k,i);j>=0;j--){\n int prev=-1;int curr=-1;\n if(j!=0)\n prev=dp[j-1].first+1;\n curr=dp[j].second[nums[i]]=max(dp[j].second[nums[i]]+1,prev);\n dp[j].first=max(dp[j].first,curr);\n ans=max(ans,dp[j].first);\n }\n } \n return ans;\n }\n};",
"memory": "125836"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 2 | {
"code": "#define ll long long\n#define pb push_back\nclass Solution {\npublic:\n ll f(ll index,ll val,vector<int>& nums,vector<vector<ll>>& dp,ll n,ll k, vector<ll>& next){\n\n if(dp[index][val]!=-1)return dp[index][val];\n if(index==n){\n return dp[index][val]=0;\n }\n if(next[index]!=-1){\n ll b = 0;\n ll a = f(next[index],val,nums,dp,n,k,next)+1;\n if(val!=k) b = f(index+1,val+1,nums,dp,n,k,next)+1;\n return dp[index][val] = max(a,b);\n }else{\n ll b = 0;\n if(val!=k) b = f(index+1,val+1,nums,dp,n,k,next)+1;\n return dp[index][val] = b;\n }\n \n }\n int maximumLength(vector<int>& nums, int k) {\n ll n = nums.size();\n vector<vector<ll>>dp(n+1,vector<ll>(k+1));\n vector<vector<ll>>max_dp(n+1,vector<ll>(k+1));\n ll maxi = 0;\n map<ll,ll>mp;\n vector<ll>next(n,-1);\n for(ll i=n-1;i>=0;i--){\n if(mp.find(nums[i])!=mp.end())next[i]=mp[nums[i]];\n mp[nums[i]]=i;\n }\n \n for(ll j=k;j>=0;j--){\n for(ll i=n-1;i>=0;i--){\n if(j==k){\n ll a = 1;\n if(next[i]!=-1)a = 1+dp[next[i]][j];\n dp[i][j]=a;\n }else{\n ll a = 1;\n if(next[i]!=-1)a = 1+dp[next[i]][j];\n ll b = 1+max_dp[i+1][j+1];\n dp[i][j]=max(a,b);\n }\n\n max_dp[i][j] = max(max_dp[i+1][j],dp[i][j]);\n }\n }\n for(ll i=0;i<n;i++){\n maxi=max(maxi,dp[i][0]);\n }\n return maxi;\n }\n};",
"memory": "132204"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 2 | {
"code": "//I should learn to use iterative dp as soon as possible\n//This was a very good question\n//Used xtra space but its fine\n\n#define ll long long\n#define pb push_back\nclass Solution {\npublic:\n\n int maximumLength(vector<int>& nums, int k) {\n ll n = nums.size();\n vector<vector<ll>>dp(n+1,vector<ll>(k+1));\n vector<vector<ll>>max_dp(n+1,vector<ll>(k+1));\n ll maxi = 0;\n map<ll,ll>mp;\n vector<ll>next(n,-1);\n for(ll i=n-1;i>=0;i--){\n if(mp.find(nums[i])!=mp.end())next[i]=mp[nums[i]];\n mp[nums[i]]=i;\n }\n \n for(ll j=k;j>=0;j--){\n for(ll i=n-1;i>=0;i--){\n if(j==k){\n ll a = 1;\n if(next[i]!=-1)a = 1+dp[next[i]][j];\n dp[i][j]=a;\n }else{\n ll a = 1;\n if(next[i]!=-1)a = 1+dp[next[i]][j];\n ll b = 1+max_dp[i+1][j+1];\n dp[i][j]=max(a,b);\n }\n\n max_dp[i][j] = max(max_dp[i+1][j],dp[i][j]);\n }\n }\n for(ll i=0;i<n;i++){\n maxi=max(maxi,dp[i][0]);\n }\n return maxi;\n }\n};",
"memory": "135388"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n= nums.size();\n unordered_map<int,unordered_map<int,int>>dp;\n vector<int>max_val(k+1,0);\n\n for(int i=0;i<n;i++){\n for(int rem=k;rem>=0;rem--){\n dp[nums[i]][rem] = max(dp[nums[i]][rem]+1,(rem>0?max_val[rem-1]+1:0));\n max_val[rem] = max(max_val[rem],dp[nums[i]][rem]);\n }\n }\n\n int max_len=0;\n for(int rem=k;rem>=0;rem--) max_len = max(max_len,max_val[rem]);\n return max_len;\n }\n};",
"memory": "135388"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n /*int f(int idx,int k,int prev,vector<int>& nums,vector<vector<vector<int>>>& dp)\n {\n if(k<0)return INT_MIN;\n if(idx==nums.size())return 0;\n\n if(dp[idx][k][prev+1]!=-1)return dp[idx][k][prev+1];\n\n int not_take = f(idx+1,k,prev,nums,dp);\n //take\n int take = 1;\n if(idx>0 && prev!=-1 && nums[idx]!=nums[prev])take += f(idx+1,k-1,idx,nums,dp);\n else take += f(idx+1,k,idx,nums,dp);\n \n return dp[idx][k][prev+1] = max(take,not_take);\n }*/\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n /*vector<vector<vector<int>>>dp(n,vector<vector<int>>(k+1,vector<int>(n+1,-1)));\n return f(0,k,-1,nums,dp); */\n\n //O(n.k.n) -> O(n.k)\n // total distinct values i can have is 5000 ; so instead of using dp vector, i'll\n //use dp map to store these\n unordered_map<int,unordered_map<int,int>>dp ; // nums[i] -> remk -> maxdp;\n vector<int>maxDpFork(k+1,0);\n\n for(int i=0;i<n;i++)\n {\n for(int remk = k;remk>=0 ; remk--)\n {\n dp[nums[i]][remk] = max(dp[nums[i]][remk]+1,(remk>0 ? maxDpFork[remk-1] +1 : 0));\n maxDpFork[remk] = max(dp[nums[i]][remk] , maxDpFork[remk]);\n }\n }\n int maxl = 0;\n for(int remk = k;remk>=0;remk--)\n {\n maxl = max(maxl,maxDpFork[remk]);\n }\n return maxl;\n \n } \n};",
"memory": "138571"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n unordered_map<int,unordered_map<int,int>> dp;\n vector<int> maxdp(k+1, 0);\n\n for(int i = 0 ;i< nums.size();i++){ \n for(int j = k ; j>=0 ;j--){\n dp[nums[i]][j] = max(dp[nums[i]][j]+1,(j>0 ? maxdp[j-1]+1 :0));\n maxdp[j] = max(maxdp[j],dp[nums[i]][j]);\n }\n }\n int maxi = 0 ;\n for(int i:maxdp)maxi = max(i,maxi);\n\n return maxi;\n }\n};",
"memory": "141755"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n unordered_set<int> elements;\n\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n for(int x : nums){\n elements.insert(x);\n }\n\n // dp\n unordered_map<int, unordered_map<int, int>> dp; // ends[x][k] = length\n vector<pair<int, int>> max_len(k+1); // len, num\n int ans = 0;\n for(int i = 0; i < n; i++){\n for(int j = 0; j <= k; j++){\n dp[nums[i]][j]++;\n if(j > 0 && max_len[j-1].second != nums[i]){\n dp[nums[i]][j] = max(dp[nums[i]][j], 1 + max_len[j-1].first);\n }\n if(dp[nums[i]][j] > max_len[j].first){\n max_len[j].first = dp[nums[i]][j];\n max_len[j].second = nums[i];\n }\n ans = max(ans, dp[nums[i]][j]);\n }\n }\n return ans;\n }\n};\n\n/*\nfrom left to right, we try to include or exclude each element, and record the end\nof subsequence. and we also record the number of indices s.t. seq[i] != seq[i+1].\n\nfor each iteration, we first add every element in dp[num] by 1.\nthenm we compare it with other numbers.\n\n 1, 2, 1, 1, 3 , 3, 3\n\n 0, 1, 2\n1: 1 1 1\n2: 1 2 2\n3: \n\n 0, 1, 2\n1: 3 3 4\n2: 1 2 2\n3: \n\n 0, 1, 2\n1: 3 3 4\n2: 1 2 2\n3: 1 4 4\n\n\n 0, 1, 2\n1: 3 3 4\n2: 1 2\n3: 3 6 6\n\n\n\n 1, 2, 1, 1, 3, 3, 3\n\n*/",
"memory": "141755"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n map<int,int>mp[5005];\n int prev[5005];\n int maximumLength(vector<int>& nums, int k) {\n int n=nums.size(),ans=1;\n for(int i=0;i<n;i++){\n for(int j=0;j<=k;j++){\n mp[j][nums[i]]+=1;\n if(j)mp[j][nums[i]]=max(mp[j][nums[i]],prev[j-1]+1);\n }\n for(int j=0;j<=k;j++)prev[j]=max(prev[j],mp[j][nums[i]]);\n }\n for(int i=0;i<=k;i++)ans=max(ans,prev[i]);\n return ans;\n }\n};",
"memory": "144939"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\tint maximumLength(vector<int>& nums, int k) {\n\t\tvector<pair<int, int>> fixedNums{ make_pair(nums[0],1) };\n\t\tfor (int i = 1; i < nums.size(); ++i)\n\t\t{\n\n\t\t\tif (nums[i] == fixedNums.back().first)\n\t\t\t{\n\t\t\t\tfixedNums.back().second++;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tfixedNums.push_back(make_pair(nums[i], 1));\n\t\t\t}\n\t\t}\n\t\tint m = fixedNums.size();\n\t\tvector<vector<int>> dp(k + 1, vector<int>(m, 0));\n\t\tdp[0][0] = fixedNums[0].second;\n\t\tunordered_map<int, int> mapping0;\n\t\tmapping0[fixedNums[0].first] = fixedNums[0].second;\n\t\tfor (int i = 1; i < m; ++i)\n\t\t{\n\t\t\tdp[0][i] = mapping0[fixedNums[i].first] + fixedNums[i].second;\n\t\t\tmapping0[fixedNums[i].first] = dp[0][i];\n\t\t\tdp[0][i] = max(dp[0][i], dp[0][i - 1]);\n\t\t}\n\n\t\tfor (int i = 1; i <= k; ++i)\n\t\t{\n\t\t\tunordered_map<int, int> mapping;\n\t\t\tdp[i][0] = dp[i - 1][0];\n\t\t\tmapping[fixedNums[0].first] = dp[i - 1][0];\n\t\t\tfor (int j = 1; j < m; ++j)\n\t\t\t{\n\t\t\t\tdp[i][j] = dp[i - 1][j - 1] + fixedNums[j].second;\n\t\t\t\tif (mapping.find(fixedNums[j].first) != mapping.end())\n\t\t\t\t{\n\t\t\t\t\tdp[i][j] = max(dp[i][j], mapping[fixedNums[j].first]+ fixedNums[j].second);\n\n\t\t\t\t}\n\t\t\t\tmapping[fixedNums[j].first] = dp[i][j];\n\t\t\t\tdp[i][j] = max(dp[i][j], dp[i][j - 1]);\n\t\t\t}\n\t\t}\n\n\t\treturn dp[k][m - 1];\n\t}\n};",
"memory": "148123"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 2 | {
"code": "using i32 = int;\nusing i64 = long long;\nclass Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n i32 n = nums.size();\n vector <map <i32, i32>> dp (k + 1);\n vector <i32> maxi (k + 1);\n i32 res = 0;\n for (i32 i = 0; i < n; ++i) {\n for (i32 cnt = k; cnt >= 0; --cnt) {\n dp[cnt][nums[i]] = max(dp[cnt][nums[i]] + 1, (cnt - 1 >= 0 ? maxi[cnt - 1] + 1 : 0));\n maxi[cnt] = max(maxi[cnt], dp[cnt][nums[i]]);\n res = max(res, maxi[cnt]);\n }\n }\n return res;\n }\n};",
"memory": "148123"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplate<typename T> istream & operator>>(istream & in, vector<T> & lst)\n{ for (auto & e : lst) in >> e; return in; }\n#define endl '\\n'\n#define F first\n#define pb push_back\n#define pf push_front\n#define pob pop_back\n#define pof pop_front\n#define S second\n#define MP make_pair\n// typedef long long ll;\ntypedef long double ld;\ntypedef pair<ll, ll> ii;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<ii> vii;\ntypedef set<ll> si;\ntypedef map<string, ll> msi;\n#define all(v) v.begin(),v.end()\n#define REP(i, a, b) \\\nfor (ll i = ll(a); i <= ll(b); i++)\n#define RREP(i, a, b) \\\nfor (ll i = ll(a); i >= ll(b); i--)\n#define ohyes cout<<\"YES\\n\"\n#define ohno cout<<\"NO\\n\"\n#define getvec(v,a,b) \\\nfor (ll i = a; i <=b; i++) \\\n cin >> v[i];\n#define printvec(v,a,b) {for (ll i = a; i <=b; i++){cout << v[i] << \" \";}cout<<'\\n';}\n#define get2dvec(v,a,b,c,d) \\\nfor (ll i = a; i <=b; i++) \\\n for (ll j = c; j <=d; j++) \\\n cin >> v[i][j];\n#define print2dvec(v,a,b,c,d) for (ll i = a; i <=b; i++){for (ll j = c; j <=d; j++){cout << v[i][j]<<\" \";}cout<<'\\n';}\n#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n// ll mod = 998244353;\n// ll mod = 1e9 + 7;\nll _t,cs;\n// cout<<\"Case \"<<cs-_t<<\": \"<<ans<<'\\n';\n// memset(dp,-1,sizeof(dp));\nll dp[5005][60];\nclass Solution {\npublic:\n vector<int> v;\n ll n,k;\n int maximumLength(vector<int>& nums, int kk) {\n v=nums;\n n=v.size();\n k=kk;\n REP(i,0,n+2){\n REP(j,0,51){\n dp[i][j]=0;\n }\n }\n map<ll,map<ll,ll>>maxx;\n map<ll,ll>maxx1;\n // n-1-i\n ll mx=1;\n RREP(i,n-1,0){\n REP(j,0,k){\n dp[i][j]=max((ll)1,dp[i][j]);\n if(j)dp[i][j]=max((ll)maxx1[j-1]+1,dp[i][j]);\n dp[i][j]=max((ll)maxx[j][v[i]]+1,dp[i][j]); \n } \n REP(j,0,k){\n maxx1[j]=max(maxx1[j],dp[i][j]);\n maxx[j][v[i]]=max(maxx[j][v[i]],dp[i][j]);\n }\n mx=max(mx,dp[i][k]);\n }\n\n return mx;\n }\n};",
"memory": "151306"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n map <int,map<int,int>> dp;\n vector<int> rem_k(k+1,0);\n int ans = 1;\n for(int i=n-1;i>=0;i--){\n for(int j=k;j>=0;j--){\n dp[nums[i]][j] = max(1+dp[nums[i]][j],(j>0)?1+rem_k[j-1]:1);\n rem_k[j] = max(rem_k[j],dp[nums[i]][j]);\n ans = max(ans,rem_k[j]);\n }\n } \n\n return ans;\n }\n};",
"memory": "154490"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n vector<unordered_map<int, int>> dp(k + 1);\n vector<int> res(k + 1);\n for (auto x : nums) {\n vector<int> tmp = res;\n for (int i = 0; i <= k; i++) {\n dp[i][x] = max(dp[i][x] + 1, (i > 0 ? res[i - 1] + 1 : 0));\n tmp[i] = max(tmp[i], dp[i][x]);\n }\n res = tmp;\n }\n return res[k];\n }\n};",
"memory": "154490"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(std::vector<int>& nums, int k) {\n int n = nums.size();\n if (n == 0) \n return 0;\n\n vector<vector<int>> dp(n, vector<int>(k + 1, 0));\n for (int i = 0; i < n; ++i) \n dp[i][0] = 1;\n\n int res = 1;\n for (int j = 0; j <= k; ++j) {\n int max1 = 1;\n std::unordered_map<int, int> numMap;\n numMap[nums[0]] = 0;\n\n for (int i = 1; i < n; ++i) {\n\n if (i > 0 && j > 0 && nums[i]!=nums[i-1]) \n max1 = max(max1, dp[i - 1][j - 1] + 1);\n dp[i][j] = max(dp[i][j], max1);\n\n if (numMap.find(nums[i]) != numMap.end()) {\n dp[i][j] = max(dp[i][j], dp[numMap[nums[i]]][j] + 1);\n }\n\n numMap[nums[i]] = i;\n res = max(res, dp[i][j]);\n }\n }\n\n return res;\n }\n};",
"memory": "157674"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(std::vector<int>& nums, int k) {\n int n = nums.size();\n if (n == 0) return 0;\n\n std::vector<std::vector<int>> dp(n, std::vector<int>(k + 1, 0));\n for (int i = 0; i < n; ++i) \n dp[i][0] = 1;\n\n int res = 1;\n for (int j = 0; j <= k; ++j) {\n int max1 = 1;\n std::unordered_map<int, int> numMap;\n numMap[nums[0]] = 0;\n\n for (int i = 1; i < n; ++i) {\n // dp[i][j] = 1;\n if (i > 0 && j > 0) \n max1 = max(max1, dp[i - 1][j - 1] + 1);\n dp[i][j] = max(dp[i][j], max1);\n\n if (numMap.find(nums[i]) != numMap.end()) {\n dp[i][j] = max(dp[i][j], dp[numMap[nums[i]]][j] + 1);\n }\n\n numMap[nums[i]] = i;\n res = max(res, dp[i][j]);\n }\n }\n\n return res;\n }\n};",
"memory": "157674"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution\n{\npublic:\n int maximumLength(vector<int>& nums, int k)\n {\n int n = nums.size();\n std::vector<std::vector<int>> dp(n + 1, std::vector<int>(k + 1, 0));\n\n /*\n [x x x x x x] i\n store the max value before i\n */\n int result = 1;\n std::vector<int> maxValues1(55, 0);\n std::vector<std::unordered_map<int, int>> maxValues2(55);\n\n nums.insert(nums.begin(), 0);\n for (int i = 1; i <= n; ++i)\n\t\t{\n\t\t\tfor (int t = 0; t <= k; ++t)\n\t\t\t{\n int currMax = 1;\n\n // old\n\t\t\t\t// for (int j = i - 1; j >= 0; --j)\n\t\t\t\t// {\n // if (nums[j] == nums[i])\n // currMax = std::max(currMax, dp[j][t] + 1);\n // else if (t >= 1)\n // currMax = std::max(currMax, dp[j][t - 1] + 1);\n\t\t\t\t// }\n\n // new, improvement\n if (t >= 1)\n currMax = std::max(currMax, maxValues1[t - 1] + 1);\n\n if (maxValues2[t].find(nums[i]) != maxValues2[t].end())\n currMax = std::max(currMax, maxValues2[t][nums[i]] + 1);\n\n dp[i][t] = currMax;\n result = std::max(result, currMax);\n\t\t\t}\n\n for (int t = 0; t <= k; ++t)\n {\n maxValues1[t] = std::max(maxValues1[t], dp[i][t]);\n maxValues2[t][nums[i]] = std::max(maxValues2[t][nums[i]], dp[i][t]);\n }\n\n }\n return result;\n }\n};",
"memory": "160858"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n \n vector<pair<int, int>> vec;\n for(int i = 0; i < n;) {\n int curr = nums[i], cnt = 0;\n while(i < n && curr == nums[i]) {\n i++, cnt++;\n }\n vec.push_back({curr, cnt});\n }\n \n vector<int> prev(k + 1, 0);\n unordered_map<int, unordered_map<int, int>> same;\n \n for(int i = 0; i <= k; ++i) {\n prev[i] = vec[0].second;\n same[vec[0].first][i] = vec[0].second;\n }\n \n for(int i = 1; i < vec.size(); ++i) {\n vector<int> curr(k + 1, 0);\n for(int j = 0; j <= k; ++j) {\n int mx = 0;\n if(same.find(vec[i].first) != same.end()) {\n mx = max(mx, same[vec[i].first][j] + vec[i].second);\n same[vec[i].first][j] += vec[i].second;\n }\n \n mx = max(mx, ((j > 0) ? prev[j - 1] : 0) + vec[i].second);\n same[vec[i].first][j] = max(same[vec[i].first][j], ((j > 0) ? prev[j - 1] : 0) + vec[i].second);\n \n curr[j] = max(mx, prev[j]);\n }\n prev = curr;\n }\n \n int ans = 0;\n for(int ele : prev) {\n ans = max(ans, ele);\n }\n return ans;\n }\n};",
"memory": "160858"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int N = nums.size();\n vector<vector<int>> dp(k+1, vector<int>(N, 0));\n \n map<int, int> prev;\n int change = 0;\n \n for (int i = 0; i <= k; i++) {\n prev.clear();\n change = 0;\n for (int j = N-1; j >= 0; j--) {\n int num = nums[j];\n int reuse = 0;\n if (prev[num] != NULL) {\n reuse = prev[num];\n }\n dp[i][j] = 1 + max(change, reuse);\n\n if (i > 0) {\n change = max(change, dp[i-1][j]);\n }\n prev[num] = dp[i][j];\n \n }\n }\n \n int best = 0;\n for (int num: dp[k]) {\n best = max(best, num);\n }\n return best;\n }\n};",
"memory": "164041"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\n int dfs(vector<int>& nums, int idx, int k) {\n int cnt = 0;\n if(k < 0) return 0;\n if(idx == nums.size()) return 1;\n \n for(int j=idx+1; j<nums.size(); ++j) {\n if(nums[idx] == nums[j]) {\n cnt = max(cnt, dfs(nums, j, k));\n } else {\n cnt = max(cnt, dfs(nums, j, k-1));\n }\n }\n\n return cnt+1;\n }\npublic:\n int maximumLength(vector<int>& nums, int k) {\n // int cnt = 0;\n // for(int idx = 0; idx<nums.size(); ++idx) {\n // cnt = max(cnt, dfs(nums, idx, k));\n // }\n // return cnt;\n int N = nums.size();\n vector<vector<int>> dp(k+1, vector<int>(N, 0));\n \n map<int, int> prev;\n int change = 0;\n \n for (int i = 0; i <= k; i++) {\n prev.clear();\n change = 0;\n for (int j = N-1; j >= 0; j--) {\n int num = nums[j];\n int reuse = 0;\n if (prev[num] != NULL) {\n reuse = prev[num];\n }\n dp[i][j] = 1 + max(change, reuse);\n\n if (i > 0) {\n change = max(change, dp[i-1][j]);\n }\n prev[num] = dp[i][j];\n \n }\n }\n \n int best = 0;\n for (int num: dp[k]) {\n best = max(best, num);\n }\n return best;\n }\n};",
"memory": "167225"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "/*\n\n* observations:\n* f[i][j]: max len of subsequence where there is j difference\n in nums[:i], with nums[i] being the last element.\nf[0][0]: 1.\nf[i][0]: go through previous numbers and add 1 to the largest.\n\n* f[i][j]: \neither it does not add a difference.\nor\nfind the longest in f[:(i-1)][j-1] and add 1.\n* use a priortity queue and a hash table to speed things up.\n* at nums[i], there can be at most i differences.\nso f[i][j] = 0 if j<i.\n\n\n===\n* f[i][j]: max len subseq with at most i difference in nums[:j].\n* f[i][j] can be dervied from f[i-1][:(j-1)] and f[i][:(j-1)].\n* just need to modify the previous code slightly.\n\n*/\n\nclass Solution {\n\npublic:\n int maximumLength(vector<int>& nums, int k) {\n\n\n int n = nums.size();\n vector<vector<int>> f(k+1,vector<int>(n,0));\n \n // prepare diff\n vector<int> diff(n,0);\n for(int j=1; j<n; j++)\n diff[j] = diff[j-1] + (nums[j]!=nums[j-1]);\n\n f[0][0] = 1;\n // max len keyed by value\n unordered_map<int,int> g;\n if(f[0][0]>0)\n g[nums[0]] = f[0][0];\n for(int j=1; j<n; j++){\n // f[0][j]>=1\n f[0][j] = 1;\n if(g.find(nums[j])!=nullptr)\n f[0][j] = max(f[0][j],g[nums[j]]+1);\n g[nums[j]] = max(f[0][j],g[nums[j]]);\n }\n\n for(int i=1; i<=min(k,diff[n-1]); i++){\n if(i+1>n)\n break;\n\n // initialize f[i][i]\n f[i][i] = diff[i]<i? 0:(i+1);\n // max len keyed by value\n unordered_map<int,int> g;\n if(f[i][i]>0)\n g[nums[i]] = f[i][i];\n\n // prepare prev: max len with at most i-1 difference.\n vector<int> prev(n,0);\n prev[0] = f[i-1][0];\n for(int j=1; j<n; j++)\n prev[j] = max(prev[j-1],f[i-1][j]);\n\n for(int j=i+1; j<n; j++){\n f[i][j] = prev[j-1]+1;\n // does not add a difference.\n if(g.find(nums[j])!=nullptr)\n f[i][j] = max(f[i][j],g[nums[j]]+1);\n // g[nums[j]] = max(f[i][j],g[nums[j]]);\n g[nums[j]] = f[i][j];\n }\n\n if(ranges::max(f[i])==0)\n break;\n }\n\n int ans = 0;\n for(int i=0; i<=k; i++)\n ans = max(ans,ranges::max(f[i]));\n return ans;\n\n }\n};",
"memory": "170409"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "/*\n\n* observations:\n* f[i][j]: max len of subsequence where there is j difference\n in nums[:i], with nums[i] being the last element.\nf[0][0]: 1.\nf[i][0]: go through previous numbers and add 1 to the largest.\n\n* f[i][j]: \neither it does not add a difference.\nor\nfind the longest in f[:(i-1)][j-1] and add 1.\n* use a priortity queue and a hash table to speed things up.\n* at nums[i], there can be at most i differences.\nso f[i][j] = 0 if j<i.\n\n\n===\n* f[i][j]: max len subseq with at most i difference in nums[:j].\n* f[i][j] can be dervied from f[i-1][:(j-1)] and f[i][:(j-1)].\n* just need to modify the previous code slightly.\n\n*/\n\nclass Solution {\n\npublic:\n int maximumLength(vector<int>& nums, int k) {\n\n\n int n = nums.size();\n vector<vector<int>> f(k+1,vector<int>(n,0));\n \n // prepare diff\n vector<int> diff(n,0);\n for(int j=1; j<n; j++)\n diff[j] = diff[j-1] + (nums[j]!=nums[j-1]);\n\n f[0][0] = 1;\n // max len keyed by value\n unordered_map<int,int> g;\n if(f[0][0]>0)\n g[nums[0]] = f[0][0];\n for(int j=1; j<n; j++){\n // f[0][j]>=1\n f[0][j] = 1;\n if(g.find(nums[j])!=nullptr)\n f[0][j] = max(f[0][j],g[nums[j]]+1);\n g[nums[j]] = max(f[0][j],g[nums[j]]);\n }\n\n for(int i=1; i<=min(k,diff[n-1]); i++){\n if(i+1>n)\n break;\n\n // initialize f[i][i]\n f[i][i] = diff[i]<i? 0:(i+1);\n // max len keyed by value\n unordered_map<int,int> g;\n if(f[i][i]>0)\n g[nums[i]] = f[i][i];\n\n // prepare prev: max len with at most i-1 difference.\n vector<int> prev(n,0);\n prev[0] = f[i-1][0];\n for(int j=1; j<n; j++)\n prev[j] = max(prev[j-1],f[i-1][j]);\n\n for(int j=i+1; j<n; j++){\n f[i][j] = prev[j-1]+1;\n // does not add a difference.\n if(g.find(nums[j])!=nullptr)\n f[i][j] = max(f[i][j],g[nums[j]]+1);\n g[nums[j]] = f[i][j];\n }\n\n if(ranges::max(f[i])==0)\n break;\n }\n\n int ans = 0;\n for(int i=0; i<=k; i++)\n ans = max(ans,ranges::max(f[i]));\n return ans;\n\n }\n};",
"memory": "170409"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\n\npublic:\n \n int maximumLength(vector<int>& nums, int k) {\n \n map<pair<int, int>, int> dp;\n \n vector<int> dpOthers(k+1, 0);\n \n for (int i=0; i<nums.size(); i++) {\n vector<int> copy(dpOthers.begin(), dpOthers.end());\n for (int j=0; j<=k; j++) {\n\n if (dp.find({nums[i], j}) != dp.end()) {\n dp[{nums[i], j}] = 1 + dp[{nums[i], j}];\n if (j != 0)\n dp[{nums[i], j}] = max(dp[{nums[i], j}], 1 + copy[j-1]);\n }\n else {\n if (j != 0)\n dp[{nums[i], j}] = 1 + copy[j-1];\n else\n dp[{nums[i], j}] = 1;\n }\n\n dpOthers[j] = max(dpOthers[j], dp[{nums[i], j}]);\n\n // dpOthers[j] = max(dpOthers[j], )\n }\n \n }\n \n int maxAns = 0;\n \n for (int i=0; i<=k; i++) {\n std::cout<<dpOthers[i]<<\" \";\n maxAns = max(maxAns, dpOthers[i]);\n }\n \n return maxAns;\n \n \n }\n};",
"memory": "173593"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>>dp;\n int maximumLength(vector<int>& nums, int k) {\n dp.clear();\n dp.assign((int)nums.size(), vector<int>(k+1, -1));\n\n for(int j=0; j<=k; j++){\n map<int, int>same_val_best;\n int max_dp_x_j_1 = 0; \n for(int i=0; i<nums.size(); i++){\n int ans = 1;\n ans=max(ans, 1+same_val_best[nums[i]]);\n if(j) ans = max(ans, 1+max_dp_x_j_1);\n dp[i][j] = ans;\n same_val_best[nums[i]] = max(same_val_best[nums[i]], ans);\n if(j) max_dp_x_j_1 = max(max_dp_x_j_1, dp[i][j-1]);\n }\n }\n int final_ans = 0;\n for(int i=0; i<nums.size(); i++){\n final_ans = max(final_ans, dp[i][k]);\n }\n return final_ans;\n }\n};",
"memory": "176776"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n vector<vector<int>>dp(nums.size(),vector<int>(k+1,-1));\n //dp[i][j]\n int finalMaxLength=0;\n for(int j=0;j<=k;j++){\n map<int,int>same;\n int prevj=0;\n for(int i=0;i<nums.size();i++){\n int ans=1;\n if(same.find(nums[i])!=same.end())ans=same[nums[i]]+1;\n if(j) ans=max(ans,prevj+1);\n dp[i][j]=ans;\n if(same.find(nums[i])!=same.end())same[nums[i]]=max(ans,same[nums[i]]);\n else same[nums[i]]=ans;\n if(j) prevj=max(prevj,dp[i][j-1]);\n if(j==k) finalMaxLength=max(finalMaxLength,dp[i][j]);\n }\n\n }\n return finalMaxLength;\n \n }\n};",
"memory": "176776"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int inf = -1e9;\n int n = nums.size();\n vector<vector<int>> dp(n + 1, vector<int>(k + 1, inf));\n map<pair<int, int>, int> prev;\n for (auto i : nums) {\n for (int kk = 0; kk <= k; kk++) {\n prev[{i, kk}] = inf;\n }\n }\n vector<int> val(k + 1, inf);\n for (int i = 0; i < n; i++) {\n for (int kk = 0; kk <= k; kk++) {\n if (kk == 0) {\n dp[i][kk] = 1;\n }\n dp[i][kk] = max(dp[i][kk], 1 + prev[{nums[i], kk}]);\n if (kk > 0)\n dp[i][kk] = max(dp[i][kk], 1 + val[kk - 1]);\n }\n for (int kk = 0; kk <= k; kk++) {\n val[kk] = max(val[kk], dp[i][kk]);\n prev[{nums[i], kk}] = max(prev[{nums[i], kk}], dp[i][kk]);\n }\n }\n int ans = 1;\n for (int i = 0; i < n; i++) {\n for (int kk = 0; kk <= k; kk++) {\n ans = max(ans, dp[i][kk]);\n // cout << i << \" \" << kk << \" \" << dp[i][kk] << endl;\n }\n }\n return ans;\n }\n};",
"memory": "179960"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size(), res = 0;\n vector<vector<int>> dp(n, vector<int>(k+1, 0));\n vector<int> prev(n, 0);\n for(int i=0;i<=k;i++)\n {\n map<int, int> mp;\n int maxy = 0;\n for(int j=0;j<n;j++)\n {\n if(i-1 >= 0) dp[j][i] = max(dp[j][i], prev[j] + 1);\n if(mp.find(nums[j]) != mp.end()) dp[j][i] = max(dp[j][i], dp[mp[nums[j]]][i] + 1);\n else dp[j][i] = max(dp[j][i], 1);\n mp[nums[j]] = j;\n res = max(res, dp[j][i]);\n prev[j] = maxy;\n maxy = max(maxy, dp[j][i]);\n } \n }\n return res;\n }\n};",
"memory": "179960"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "typedef long long ll;\nconst ll INF = LONG_LONG_MAX - 1;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\ntypedef pair<int, int> pi;\n\n\nclass Solution {\npublic:\ntemplate <class T> class Segtree {\n\nprivate:\n\n const T DEFAULT = -INF/3; // Will overflow if T is an int\n\n\n vector<T> segtree;\n\n vector<T> lazy; // add lazy prop later\n\n int len;\n\n\npublic:\n\n Segtree(int len) : len(len), segtree(len * 2, DEFAULT), lazy(len * 2, 0) {\n\n }\n\n\n /** Sets the value at ind to val. */\n\n void set(int ind, T val) {\n\n ind += len;\n\n segtree[ind] = val;\n\n for (; ind > 1; ind /= 2) {\n\n segtree[ind / 2] = std::max(segtree[ind], segtree[ind ^ 1]);\n\n }\n\n }\n\n\n /** @return the max element in the range [start, end] */\n\n T rmax(int start, int end) {\n end++;\n T sum = DEFAULT;\n\n for (start += len, end += len; start < end; start /= 2, end /= 2) {\n\n if (start % 2 == 1) { sum = std::max(sum, segtree[start++]); }\n\n if (end % 2 == 1) { sum = std::max(sum, segtree[--end]); }\n\n }\n\n return sum;\n\n }\n\n};\n\n ll dp[(int) 1e4 + 3][51];\n int maximumLength(vector<int>& b, int k) {\n // [(89,2),(90,1),(88,4),(90,2)]\n\n // dp[i][k] = min(dp[i+1][k-1] + a[i][1] .. or dp[j][k-1] + a[i][1] + a[j][1])\n\n vector<pair<int,int>> a;\n int n = b.size();\n a.emplace_back(b[0],1);\n for (int i = 1; i < n; ++i){\n if (b[i] == b[i-1])\n a.back().second++;\n else\n a.emplace_back(b[i],1);\n }\n\n n = a.size();\n a.emplace_back(0,0);\n ll ans = 0;\n //map<int,int> sk[w];\n map<int,int> nxt;\n map<int,int> tr;\n for (int i = n-1; i >= 0; --i){\n if (tr.count(a[i].first))\n nxt[i] = tr[a[i].first];\n else\n nxt[i] = n;\n tr[a[i].first] = i;\n }\n vector<Segtree<ll>> st(k+1,Segtree<ll>(n+1));\n\n\n for (int i = n-1; i >= 0; --i){\n dp[i][0] = a[i].second + dp[nxt[i]][0];\n st[0].set(i,dp[i][0]);\n ans = max(ans,dp[i][0]);\n }\n\n\n for (int w = 1; w <= k; ++w){\n for (int i = n-1; i >= 0; --i){\n // find min on range(i,nxt[i]-1) (ST?)\n dp[i][w] = a[i].second + dp[nxt[i]][w];\n dp[i][w] = max(dp[i][w],a[i].second + st[w-1].rmax(i+1,nxt[i]));\n st[w].set(i,dp[i][w]);\n ans = max(ans,dp[i][w]);\n }\n }\n return ans;\n\n }\n};",
"memory": "183144"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n vector<vector<int>> dp(nums.size(), vector<int>(k + 1, 0));\n\n for (int i = 0; i < k; i++)\n {\n dp[0][i] = 1;\n }\n\n unordered_map<int, int> count;\n\n for (int i = 0; i < nums.size(); i++)\n {\n int num = nums[i];\n count[num]++;\n\n dp[i][0] = count[num];\n }\n\n vector<int> kMax(k+1, 1);\n vector<unordered_map<int, int>> knMax(k+1);\n for (int i = 0; i <= k; i++) knMax[i][nums[0]] = 1;\n\n for (int i = 1; i < nums.size(); i++)\n {\n vector<int> kMaxBuffer(k+1);\n kMaxBuffer[0] = dp[i][0];\n\n for (int j = 1; j <= k; j++)\n {\n dp[i][j] = dp[i][j-1];\n dp[i][j] = max(dp[i][j], kMax[j-1]+1);\n dp[i][j] = max(dp[i][j], knMax[j][nums[i]]+1);\n\n kMaxBuffer[j] = dp[i][j];\n knMax[j][nums[i]] = max(knMax[j][nums[i]], dp[i][j]);\n }\n\n for (int i = 0; i < kMax.size(); i++) kMax[i] = max(kMax[i], kMaxBuffer[i]);\n }\n\n int ans = 0;\n\n for (auto &vec : dp)for (auto i : vec) ans = max(ans, i);\n\n return ans; \n }\n};",
"memory": "183144"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "int dp[5005][55], v[5005];\n\nclass ST {\n\tpublic:\n\tint s, e, v;\n\tST *l, *r;\n\tST(int s, int e) {\n\t\tthis->s = s;\n\t\tthis->e = e;\n\t\t\n\t\t//or min/max value\n\t\tv = 0;\n\t\tl = r = NULL;\n\t}\n};\n\nST *build(int s, int e) {\n\tif(s > e) {\n return NULL;\n }\n \n ST *cur = new ST(s, e);\n if(s == e) {\n return cur;\n }\n \n int mid = (e - s) / 2 + s;\n cur->l = build(s, mid);\n cur->r = build(mid + 1, e);\n \n return cur;\n}\n\nint query(ST *root, int s, int e) {\n\t\n\t//or min/max(useless) value\n\tif(!root) {\n return 0;\n }\n if(root->e < s || root->s > e) {\n return 0;\n }\n \n if(root->s >= s && root->e <= e) {\n return root->v;\n }\n return max(query(root->l, s, e), query(root->r, s, e));\n}\n\n\nvoid modify(ST *root, int i, int v) {\n\tif(!root) {\n return;\n }\n if(root->s > i || root->e < i) {\n return;\n }\n if(root->s == i && root->e == i) {\n root->v = v;\n return;\n }\n modify(root->l, i, v);\n modify(root->r, i, v);\n root->v = max(root->l->v, root->r->v);\n}\n\nST *st[55];\n\nclass Solution {\npublic:\n int maximumLength(vector<int>& a, int k) {\n int n = a.size(), c = 0, res = 1;\n unordered_map<int, int> m;\n for(int i = 0; i < n; i++) {\n if(!m.count(a[i])) {\n m[a[i]] = c++;\n }\n v[i] = m[a[i]];\n }\n for(int i = 0; i <= k; i++) {\n st[i] = build(0, c);\n }\n memset(dp, 0, (n + 1) * 55 * 4);\n for(int i = 0; i < n; i++) {\n dp[i][0] = query(st[0], v[i], v[i]) + 1;\n res = max(res, dp[i][0]);\n modify(st[0], v[i], dp[i][0]);\n for(int j = 1; j <= i && j <= k; j++) {\n dp[i][j] = max(dp[i][j], query(st[j], v[i], v[i]) + 1);\n dp[i][j] = max(dp[i][j], query(st[j - 1], 0, v[i] - 1) + 1);\n dp[i][j] = max(dp[i][j], query(st[j - 1], v[i] + 1, c) + 1);\n modify(st[j], v[i], dp[i][j]);\n res = max(res, dp[i][j]);\n }\n }\n return res;\n }\n};",
"memory": "186328"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int K) {\n // goal : find the longest subseq s.t. there at most k indices i make \n // subseq[i] != subseq[i+1].\n \n // 1. dp[i][k] : maximum length subseq end at i with k indices \n // s.t. subseq[j] != subseq[j+1], 0 <= j < i\n // 2. dp[i][k] = max(dp[i][k], \n // dp[j][k] + 1 if j is the closed j s.t. nums[i] == nums[j],\n // dp[j][k-1] + 1 if dp[j][k-1] is the maximum len s.t. nums[i] != nums[j])\n // => we only need to find the max length of above two cases.\n // => maintain a orderd set s.t. we can find the max length in\n // O(logn) and check whether it is same with nums[i] or not.\n \n // 3. base case is 1 (only 1 element)\n // 4. answer is the max(dp[i][0:k])\n\n int n = nums.size(), ret = 1;\n vector<vector<int>> dp(n, vector<int>(K+1, 1));\n unordered_map<int, unordered_map<int,int>> lastKLen;\n vector<int> prevMxLen(K+1, 1);\n \n lastKLen[nums[0]][0] = 1;\n\n for (int i = 1; i < n; ++i) {\n auto curMxLen = prevMxLen;\n for (int k = K; k >= 0; --k) {\n if (k > 0)\n dp[i][k] = max(dp[i][k], prevMxLen[k-1] + 1);\n \n dp[i][k] = max(dp[i][k], lastKLen[nums[i]][k] + 1); \n\n ret = max(ret, dp[i][k]);\n lastKLen[nums[i]][k] = dp[i][k];\n curMxLen[k] = max(curMxLen[k], dp[i][k]);\n }\n swap(prevMxLen, curMxLen);\n }\n \n\n return ret;\n }\n};",
"memory": "189511"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class SEG{\npublic:\n int n;\n vector<int>t,a;\n void init(int sz,vector<int>b){\n a=b;\n n=sz;\n t.resize(4*n);\n built(0,0,n-1);\n }\n void built(int v,int tl,int tr){\n if(tl==tr){\n t[v]=a[tl];\n }\n else{\n int tm=(tl+tr)/2;\n built(2*v+1,tl,tm);\n built(2*v+2,tm+1,tr);\n t[v]=max(t[2*v+1],t[2*v+2]);\n }\n }\n void upd(int v,int tl,int tr,int pos,int val){\n if(tl==tr){\n t[v]=val;\n }\n else{\n int tm=(tl+tr)/2;\n if(pos<=tm)upd(2*v+1,tl,tm,pos,val);\n else upd(2*v+2,tm+1,tr,pos,val);\n t[v]=max(t[2*v+1],t[2*v+2]);\n }\n }\n int get(int v,int tl,int tr,int l,int r){\n if(l>r) return 0;\n else if(tl==l && tr==r){\n return t[v];\n }\n else{\n int tm=(tl+tr)/2;\n return max(get(2*v+1,tl,tm,l,min(tm,r)),get(2*v+2,tm+1,tr,max(tm+1,l),r));\n }\n }\n};\nclass Solution {\npublic:\n const int inf =1e9+5;\n int maximumLength(vector<int>&a, int k) {\n int n=a.size();\n vector<int>b=a;\n sort(b.begin(),b.end());\n for(int i=0;i<n;i++){\n auto idx=lower_bound(b.begin(),b.end(),a[i])-b.begin();\n a[i]=idx;\n }\n // vector<int>v(n,inf);\n vector<vector<int>>v(k+1,vector<int>(n,-inf));\n vector<vector<int>>dp(n+1,vector<int>(k+1,-inf));\n SEG seg[k+1];\n dp[0][0]=1;\n v[0][a[0]]=1;\n // for(int i=0;i<n;i++) v[0][a[i]]=1;\n for(int i=0;i<k+1;i++){\n seg[i].init(v[i].size(),v[i]);\n }\n // seg.init(v.size(),v);\n \n int ans=0;\n for(int i=1;i<n;i++){\n for(int j=0;j<k+1;j++){\n dp[i][j]=max(dp[i][j],v[j][a[i]]+1);\n if(j){\n int mx=max(seg[j-1].get(0,0,n-1,0,a[i]-1),seg[j-1].get(0,0,n-1,a[i]+1,n-1));\n dp[i][j]=max(dp[i][j],mx+1);\n }\n // else{\n // dp[i][j]=max(dp[i][j],1);\n // }\n if(!j){\n dp[i][j]=max(dp[i][j],1);\n }\n v[j][a[i]]=max(v[j][a[i]],dp[i][j]);\n seg[j].upd(0,0,n-1,a[i],v[j][a[i]]);\n }\n }\n for(int i=0;i<n;i++){\n for(int j=0;j<=k;j++) ans=max(ans,dp[i][j]);\n }\n return ans;\n\n }\n};",
"memory": "192695"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // int fun(int i, int k, vector<int>& nums, vector<vector<int>> &dp){\n // if(dp[i][k] != -1){\n // return dp[i][k];\n // }\n\n // int maxLen=1;\n // for(int j=0; j<i; j++){\n // if(nums[j] == nums[i]){\n // int len= 1 + fun(j, k, nums, dp);\n // maxLen= max(maxLen, len);\n // }\n // else if(k>0){\n // int len= 1+ fun(j, k-1, nums, dp);\n // maxLen= max(maxLen, len);\n // }\n // }\n\n // return dp[i][k]= maxLen;\n // }\n\n int maximumLength(vector<int>& nums, int k) {\n int n= nums.size();\n vector<vector<int>> dp(k+1, vector<int>(n, 1));\n int ans=1;\n // ind[nums[n-1]]= n-1;\n\n for(int i= 0; i<= k; i++){\n unordered_map<int, int> ind;\n priority_queue<int> pq;\n\n for(int j=n-1; j>=0; j--){\n int next= 0;\n if(ind.count(nums[j])){\n next= 1+ dp[i][ind[nums[j]]];\n }\n\n int ops= 0;\n if(i>0 && !pq.empty()){\n int val= pq.top();\n // pq.pop();\n ops= 1+val;\n }\n if(i>0) pq.push(dp[i-1][j]);\n\n dp[i][j]= max({dp[i][j], ops, next});\n ind[nums[j]]= j;\n\n ans= max(ans, dp[i][j]);\n }\n }\n return ans;\n\n\n\n\n // for(int i=0; i<n; i++){\n // ans= max(ans, fun(i, k, nums, dp));\n // }\n // return ans;\n // return fun(0, -1, nums, k, dp);\n }\n};",
"memory": "195879"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n=nums.size();\n vector<vector<int>> dp(n,vector<int> (k+1,1));\n vector<map<int,int>> v(k+1);\n vector<int> mx(k+1);\n for(int i=n-1;i>=0;i--){\n vector<int> temp=mx;\n for(int j=0;j<=k;j++){\n // for(int p=i+1;p<n;p++){\n // if(nums[i]!=nums[p]){\n // if(j)\n // dp[i][j]=max(dp[i][j],dp[p][j-1]+1);\n // }\n // else dp[i][j]=max(dp[i][j],dp[p][j]+1);\n // }\n dp[i][j]=max(dp[i][j], (v[j].count(nums[i]) ? v[j][nums[i]] : 0)+1);\n if(j)\n dp[i][j]=max(temp[j-1]+1,dp[i][j]);\n v[j][nums[i]]=max(v[j][nums[i]], dp[i][j]);\n mx[j]=max(mx[j],dp[i][j]);\n }\n }\n // for(auto &x : dp){\n // for(auto &y : x)\n // cout<<y<<\" \";\n // cout<<endl;\n // }\n int ans=0;\n for(int i=0;i<n;i++)\n ans=max(dp[i][k],ans);\n return ans;\n }\n};",
"memory": "199063"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_map<int,int> vcnt;\n int icnt[n];\n for (int i = n-1; i >= 0; i--) {\n icnt[i] = vcnt[nums[i]]++;\n }\n \n int dp[5000] = {};\n int ans = 0;\n while (k >= 0) {\n unordered_map<int,pair<int,int>> vmax; // vmax[v] = {res, vcnt}\n vcnt.clear(); \n int runmax = 0;\n for (int i = 0; i < n; i++) {\n int mem = dp[i];\n auto it = vmax.find(nums[i]);\n dp[i] = max(runmax+1, (it != vmax.end() ? it->second.first + (++vcnt[nums[i]])-it->second.second : 0));\n vmax[nums[i]] = {dp[i], vcnt[nums[i]]};\n \n ans = max(ans, dp[i]+icnt[i]);\n runmax= max(runmax, mem);\n }\n k--;\n }\n return ans;\n }\n};",
"memory": "202246"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int K) {\n // goal : find the longest subseq s.t. there at most k indices i make \n // subseq[i] != subseq[i+1].\n \n // 1. dp[i][k] : maximum length subseq end at i with k indices \n // s.t. subseq[j] != subseq[j+1], 0 <= j < i\n // 2. dp[i][k] = max(dp[i][k], \n // dp[j][k] + 1 if j is the closed j s.t. nums[i] == nums[j],\n // dp[j][k-1] + 1 if dp[j][k-1] is the maximum len s.t. nums[i] != nums[j])\n // => we only need to find the max length of above two cases.\n // => maintain a orderd set s.t. we can find the max length in\n // O(logn) and check whether it is same with nums[i] or not.\n \n // 3. base case is 1 (only 1 element)\n // 4. answer is the max(dp[i][0:k])\n\n int n = nums.size(), ret = 1;\n vector<vector<int>> dp(n, vector<int>(K+1, 1));\n unordered_map<int, set<int>> maxKLen; // (k, {len, ...}))\n unordered_map<int, unordered_map<int,int>> lastKLen; // (nums[i], (k, len))\n \n\n maxKLen[0].insert(1);\n lastKLen[nums[0]][0] = 1;\n\n for (int i = 1; i < n; ++i) {\n for (int k = K; k >= 0; --k) {\n // find the nums[i] != nums[j] in maxLen[k-1]\n if (k > 0 and !maxKLen[k-1].empty()) {\n int len = *maxKLen[k-1].rbegin(); \n dp[i][k] = max(dp[i][k], len + 1);\n }\n\n for (int kk = k; kk >= 0; --kk) {\n dp[i][k] = max(dp[i][k], lastKLen[nums[i]][kk] + 1);\n break;\n }\n\n ret = max(ret, dp[i][k]);\n maxKLen[k].insert(dp[i][k]);\n lastKLen[nums[i]][k] = dp[i][k];\n }\n }\n \n\n return ret;\n }\n};",
"memory": "205430"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int K) {\n // goal : find the longest subseq s.t. there at most k indices i make \n // subseq[i] != subseq[i+1].\n \n // 1. dp[i][k] : maximum length subseq end at i with k indices \n // s.t. subseq[j] != subseq[j+1], 0 <= j < i\n // 2. dp[i][k] = max(dp[i][k], \n // dp[j][k] + 1 if j is the closed j s.t. nums[i] == nums[j],\n // dp[j][k-1] + 1 if dp[j][k-1] is the maximum len s.t. nums[i] != nums[j])\n // => we only need to find the max length of above two cases.\n // => maintain a orderd set s.t. we can find the max length in\n // O(logn) and check whether it is same with nums[i] or not.\n \n // 3. base case is 1 (only 1 element)\n // 4. answer is the max(dp[i][0:k])\n\n int n = nums.size(), ret = 1;\n vector<vector<int>> dp(n, vector<int>(K+1, 1));\n unordered_map<int, set<int>> maxKLen; // (k, {len, ...}))\n unordered_map<int, unordered_map<int,int>> lastKLen; // (nums[i], (k, len))\n \n\n maxKLen[0].insert(1);\n lastKLen[nums[0]][0] = 1;\n\n for (int i = 1; i < n; ++i) {\n for (int k = K; k >= 0; --k) {\n // find the nums[i] != nums[j] in maxLen[k-1]\n if (k > 0 and !maxKLen[k-1].empty()) {\n int len = *maxKLen[k-1].rbegin(); \n dp[i][k] = max(dp[i][k], len + 1);\n }\n\n for (int kk = k; kk >= 0; --kk)\n if (lastKLen[nums[i]].count(kk)) {\n dp[i][k] = max(dp[i][k], lastKLen[nums[i]][kk] + 1);\n break;\n }\n\n ret = max(ret, dp[i][k]);\n maxKLen[k].insert(dp[i][k]);\n lastKLen[nums[i]][k] = dp[i][k];\n }\n }\n \n\n return ret;\n }\n};",
"memory": "208614"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int K) {\n // goal : find the longest subseq s.t. there at most k indices i make \n // subseq[i] != subseq[i+1].\n \n // 1. dp[i][k] : maximum length subseq end at i with k indices \n // s.t. subseq[j] != subseq[j+1], 0 <= j < i\n // 2. dp[i][k] = max(dp[i][k], \n // dp[j][k] + 1 if j is the closed j s.t. nums[i] == nums[j],\n // dp[j][k-1] + 1 if dp[j][k-1] is the maximum len s.t. nums[i] != nums[j])\n // => we only need to find the max length of above two cases.\n // => maintain a orderd set s.t. we can find the max length in\n // O(logn) and check whether it is same with nums[i] or not.\n \n // 3. base case is 1 (only 1 element)\n // 4. answer is the max(dp[i][0:k])\n\n int n = nums.size(), ret = 1;\n vector<vector<int>> dp(n, vector<int>(K+1, 1));\n unordered_map<int, set<int>> maxKLen; // (k, {len, ...}))\n unordered_map<int, unordered_map<int,int>> lastKLen; // (nums[i], (k, len))\n \n\n maxKLen[0].insert(1);\n lastKLen[nums[0]][0] = 1;\n\n for (int i = 1; i < n; ++i) {\n for (int k = K; k >= 0; --k) {\n // find the nums[i] != nums[j] in maxLen[k-1]\n if (k > 0 and !maxKLen[k-1].empty()) {\n int len = *maxKLen[k-1].rbegin(); \n dp[i][k] = max(dp[i][k], len + 1);\n }\n\n dp[i][k] = max(dp[i][k], lastKLen[nums[i]][k] + 1); \n\n ret = max(ret, dp[i][k]);\n maxKLen[k].insert(dp[i][k]);\n lastKLen[nums[i]][k] = dp[i][k];\n }\n }\n \n\n return ret;\n }\n};",
"memory": "208614"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& v, int k) \n { \n int n=v.size();\n vector<vector<int>>dp(k+1,vector<int>(n,1));\n map<int,int>mp;\n for(int i=n-1;i>=0;i--)\n { dp[0][i]=max(1,1+mp[v[i]]);\n mp[v[i]]++;\n }\n for(int i=1;i<=k;i++) dp[i][n-1]=1;\n for(int i=1;i<=k;i++)\n { \n mp.clear();\n mp[v[n-1]]=n-1;\n priority_queue<int>pq;\n pq.push(dp[i-1][n-1]);\n for(int j=n-2;j>=0;j--)\n {\n if(mp.find(v[j])!=mp.end()) dp[i][j]=dp[i][mp[v[j]]]+1;\n dp[i][j]=max(dp[i][j],1+pq.top());\n pq.push(dp[i-1][j]);\n mp[v[j]]=j;\n } \n }\n int ans=0;\n for(int i=0;i<=k;i++)\n {\n for(int j=0;j<n;j++)\n { \n ans=max(ans,dp[i][j]);\n }\n }\n return ans;\n }\n};",
"memory": "211798"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n=nums.size();\n vector<vector<int>>dp(k+1,vector<int>(n,1));//\n\n //dp[i][j]=length of the good subsequence starting from j and having i mismatches.\n map<int,int>mp;\n for(int i=n-1;i>=0;i--){\n dp[0][i]=1+mp[nums[i]];\n mp[nums[i]]++;\n }\n for(int i=1;i<=k;i++){\n dp[i][n-1]=1;\n }\n for(int i=1;i<=k;i++){\n mp.clear();\n priority_queue<int>pq;\n pq.push(dp[i-1][n-1]);\n mp[nums[n-1]]=n-1;\n for(int j=n-2;j>=0;j--){\n if(mp.find(nums[j])!=mp.end()){\n dp[i][j]=1+dp[i][mp[nums[j]]];\n }\n dp[i][j]=max(dp[i][j],pq.top()+1);\n pq.push(dp[i-1][j]);\n mp[nums[j]]=j;\n }\n }\n int ans=0;\n for(int i=0;i<=k;i++){\n for(int j=0;j<n;j++){\n ans=max(ans,dp[i][j]);\n }\n }\n return ans;\n }\n};",
"memory": "214981"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_map<int,int> vcnt;\n unordered_map<int,int> icnt;\n for (int i = n-1; i >= 0; i--) {\n icnt[i] = vcnt[nums[i]]++;\n }\n \n int dp[5000] = {};\n int ans = 0;\n while (k >= 0) {\n unordered_map<int,pair<int,int>> vmax; // vmax[v] = {res, vcnt}\n vcnt.clear(); \n int runmax = 0;\n for (int i = 0; i < n; i++) {\n int mem = dp[i];\n auto it = vmax.find(nums[i]);\n dp[i] = max(runmax+1, (it != vmax.end() ? it->second.first + (++vcnt[nums[i]])-it->second.second : 0));\n vmax[nums[i]] = {dp[i], vcnt[nums[i]]};\n \n ans = max(ans, dp[i]+icnt[i]);\n runmax= max(runmax, mem);\n }\n k--;\n }\n return ans;\n }\n};",
"memory": "218165"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_map<int,int> vcnt;\n unordered_map<int,int> icnt;\n unordered_map<int,vector<int>> vis;\n for (int i = n-1; i >= 0; i--) {\n icnt[i] = vcnt[nums[i]]++;\n vis[nums[i]].push_back(i);\n }\n vcnt.clear();\n \n for (auto& [_, is] : vis) {\n for (int l = 0, r = is.size()-1; l < r; l++, r--) \n swap(is[l], is[r]);\n }\n \n \n int dp[5000] = {};\n int ans = 0;\n \n for (int i = 0; i < n; i++) {\n ans = max(ans, dp[i] = ++vcnt[nums[i]]);\n }\n \n while (k) {\n unordered_map<int,pair<int,int>> vmax; // vmax[v] = {res, vcnt}\n vcnt.clear(); \n int runmax = 0;\n for (int i = 0; i < n; i++) {\n int mem = dp[i];\n auto it = vmax.find(nums[i]);\n dp[i] = max(runmax+1, (it != vmax.end() ? it->second.first + (++vcnt[nums[i]])-it->second.second : 0));\n vmax[nums[i]] = {dp[i], vcnt[nums[i]]};\n \n ans = max(ans, dp[i]+icnt[i]);\n runmax= max(runmax, mem);\n }\n k--;\n }\n return ans;\n }\n};",
"memory": "221349"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_map<int,int> vcnt;\n unordered_map<int,int> icnt;\n unordered_map<int,vector<int>> vis;\n for (int i = n-1; i >= 0; i--) {\n icnt[i] = vcnt[nums[i]]++;\n vis[nums[i]].push_back(i);\n }\n vcnt.clear();\n \n for (auto& [_, is] : vis) {\n for (int l = 0, r = is.size()-1; l < r; l++, r--) \n swap(is[l], is[r]);\n }\n \n \n int dp[5000] = {};\n int ans = 0;\n \n while (k >= 0) {\n unordered_map<int,pair<int,int>> vmax; // vmax[v] = {res, vcnt}\n vcnt.clear(); \n int runmax = 0;\n for (int i = 0; i < n; i++) {\n int mem = dp[i];\n auto it = vmax.find(nums[i]);\n dp[i] = max(runmax+1, (it != vmax.end() ? it->second.first + (++vcnt[nums[i]])-it->second.second : 0));\n vmax[nums[i]] = {dp[i], vcnt[nums[i]]};\n \n ans = max(ans, dp[i]+icnt[i]);\n runmax= max(runmax, mem);\n }\n k--;\n }\n return ans;\n }\n};",
"memory": "224533"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "#define ll long long int\nclass Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n //dp[i][x] -> ith index par end hone wala max length ka subsequence such that we can perform atmost x operations(remember we will always take the ith element)\n int n=nums.size();\n vector<vector<int>> dp(n,vector<int>(k+1,1)); \n map<ll,ll> freq;\n int ans=1;\n vector<vector<ll>> pre(n,vector<ll> (k+1,1));\n map<pair<ll,ll>,ll> mp;\n for(ll j=0;j<n;j++)\n {\n if(j==0) pre[0][0]=1;\n else pre[j][0]=max(pre[j-1][0],freq[nums[j]]+1);\n freq[nums[j]]++;\n dp[j][0]=freq[nums[j]];\n ans=max(ans,dp[j][0]);\n }\n for(ll x=1;x<=k;x++) mp[{nums[0],x}]=1;\n for(ll i=1;i<n;i++)\n {\n for(ll x=1;x<=k;x++)\n {\n\n dp[i][x]=pre[i-1][x-1]+1;\n dp[i][x]=fmax(dp[i][x],mp[{nums[i],x}]+1);\n // for(ll prev=0;prev<i;prev++)\n // {\n // if(nums[prev]!=nums[i]) dp[i][x]=max(dp[i][x],dp[prev][x-1]+1);\n // else dp[i][x]=max(dp[i][x],dp[prev][x]+1);\n // }\n ans=max(ans,dp[i][x]);\n\n }\n for(ll x=1;x<=k;x++) pre[i][x]=fmax(pre[i-1][x],dp[i][x]);\n for(ll x=1;x<=k;x++) mp[{nums[i],x}]=fmax(mp[{nums[i],x}],dp[i][x]);\n }\n return ans;\n }\n};",
"memory": "227716"
} |
3,452 | <p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>3</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= min(50, nums.length)</code></li>
</ul>
| 3 | {
"code": "const int N = 5010, INF = 0x3f3f3f3f;\n\nclass SegmentTree {\npublic:\n struct Info {\n int l, r, v;\n Info() {}\n Info(int left, int right, int val): l(left), r(right), v(val) {}\n } seg[N<<2];\n\n explicit SegmentTree() {}\n\n void build(int u, int l, int r) {\n if(l == r) {\n seg[u] = Info(l, r, 0);\n }else {\n int mid = l + r >> 1;\n build(u<<1, l, mid);\n build(u<<1|1, mid + 1, r);\n pushup(u);\n }\n }\n\n void modify(int pos, int val) {\n modify(1, pos, val);\n }\n\n Info query(int l, int r) {\n if(l > r) return Info(0, 0, 0);\n return query(1, l, r);\n }\n\nprivate:\n void modify(int u, int pos, int val) {\n if(seg[u].l == pos && seg[u].r == pos) {\n seg[u] = Info(pos, pos, val);\n }else {\n int mid = seg[u].l + seg[u].r >> 1;\n if(pos <= mid) modify(u<<1, pos, val);\n else modify(u<<1|1, pos, val);\n pushup(u);\n }\n }\n\n Info query(int u, int l, int r) {\n if(l <= seg[u].l && seg[u].r <= r) return seg[u];\n int mid = seg[u].l + seg[u].r >> 1;\n if(r <= mid) {\n return query(u<<1, l, r);\n }else if(mid < l) {\n return query(u<<1|1, l, r);\n }else {\n return merge(query(u<<1, l, r), query(u<<1|1, l, r));\n }\n }\n\n void pushup(int u) {\n seg[u] = merge(seg[u<<1], seg[u<<1|1]);\n }\n\n Info merge(const Info& lchild, const Info& rchild) {\n Info info;\n info.l = lchild.l;\n info.r = rchild.r;\n info.v = max(lchild.v, rchild.v);\n return info;\n }\n};\n\nclass Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> vals;\n for(int i = 0; i < n; i++) {\n vals.push_back(nums[i]);\n }\n sort(vals.begin(), vals.end());\n vals.erase(unique(vals.begin(), vals.end()), vals.end());\n unordered_map<int, int> val2pos;\n int sz = vals.size();\n for(int i = 0; i < sz; i++) {\n val2pos[vals[i]] = i + 1;\n }\n vector<SegmentTree> seg;\n for(int i = 0; i <= k; i++) {\n SegmentTree tree;\n tree.build(1, 1, sz);\n seg.push_back(tree);\n }\n int ans = 1;\n for(int i = 0; i < n; i++) {\n int index = val2pos[nums[i]];\n for(int rest = 0; rest <= min(k, i); rest++) {\n if(i == 0) {\n seg[rest].modify(index, 1);\n ans = max(ans, seg[rest].query(index, index).v);\n }else {\n int dp = 0;\n if(rest > 0) {\n int left = index > 1? seg[rest - 1].query(1, index - 1).v: 0;\n int right = index + 1 <= sz? seg[rest - 1].query(index + 1, sz).v: 0;\n dp = max(dp, max(left, right) + 1);\n }\n dp = max(dp, seg[rest].query(index, index).v + 1);\n seg[rest].modify(index, dp);\n ans = max(ans, dp);\n }\n }\n }\n return ans;\n }\n};",
"memory": "230900"
} |
3,541 | <p>You are given an array of strings <code>message</code> and an array of strings <code>bannedWords</code>.</p>
<p>An array of words is considered <strong>spam</strong> if there are <strong>at least</strong> two words in it that <b>exactly</b> match any word in <code>bannedWords</code>.</p>
<p>Return <code>true</code> if the array <code>message</code> is spam, and <code>false</code> otherwise.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","world","leetcode"], bannedWords = ["world","hello"]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The words <code>"hello"</code> and <code>"world"</code> from the <code>message</code> array both appear in the <code>bannedWords</code> array.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>Only one word from the <code>message</code> array (<code>"programming"</code>) appears in the <code>bannedWords</code> array.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code></li>
<li><code>1 <= message[i].length, bannedWords[i].length <= 15</code></li>
<li><code>message[i]</code> and <code>bannedWords[i]</code> consist only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n const int W = 11;\n unsigned long long hash(const string &s) {\n unsigned long long r = 0;\n for (char c : s) {\n r = r * W + c;\n }\n return r;\n }\npublic:\n bool reportSpam(vector<string>& message, vector<string>& bannedWords) {\n unordered_set<unsigned long long> banned;\n for (const auto& s : bannedWords) {\n banned.insert(hash(s));\n }\n int r = 0;\n for (const auto& s : message) {\n if ((r += banned.count(hash(s))) == 2) return true;\n }\n return false;\n }\n};",
"memory": "144600"
} |
3,541 | <p>You are given an array of strings <code>message</code> and an array of strings <code>bannedWords</code>.</p>
<p>An array of words is considered <strong>spam</strong> if there are <strong>at least</strong> two words in it that <b>exactly</b> match any word in <code>bannedWords</code>.</p>
<p>Return <code>true</code> if the array <code>message</code> is spam, and <code>false</code> otherwise.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","world","leetcode"], bannedWords = ["world","hello"]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The words <code>"hello"</code> and <code>"world"</code> from the <code>message</code> array both appear in the <code>bannedWords</code> array.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>Only one word from the <code>message</code> array (<code>"programming"</code>) appears in the <code>bannedWords</code> array.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code></li>
<li><code>1 <= message[i].length, bannedWords[i].length <= 15</code></li>
<li><code>message[i]</code> and <code>bannedWords[i]</code> consist only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n const int W = 11;\n unsigned int hash(const string &s) {\n unsigned int r = 0;\n for (char c : s) {\n r = r * W + c;\n }\n return r;\n }\npublic:\n bool reportSpam(vector<string>& message, vector<string>& bannedWords) {\n unordered_set<unsigned int> banned;\n for (const auto& s : bannedWords) {\n banned.insert(hash(s));\n }\n int r = 0;\n for (const auto& s : message) {\n if ((r += banned.count(hash(s))) == 2) return true;\n }\n return false;\n }\n};",
"memory": "144800"
} |
3,541 | <p>You are given an array of strings <code>message</code> and an array of strings <code>bannedWords</code>.</p>
<p>An array of words is considered <strong>spam</strong> if there are <strong>at least</strong> two words in it that <b>exactly</b> match any word in <code>bannedWords</code>.</p>
<p>Return <code>true</code> if the array <code>message</code> is spam, and <code>false</code> otherwise.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","world","leetcode"], bannedWords = ["world","hello"]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The words <code>"hello"</code> and <code>"world"</code> from the <code>message</code> array both appear in the <code>bannedWords</code> array.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>Only one word from the <code>message</code> array (<code>"programming"</code>) appears in the <code>bannedWords</code> array.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code></li>
<li><code>1 <= message[i].length, bannedWords[i].length <= 15</code></li>
<li><code>message[i]</code> and <code>bannedWords[i]</code> consist only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n const int W = 17;\n unsigned int hash(const string &s) {\n unsigned int r = 0;\n for (char c : s) {\n r = r * W + c;\n }\n return r;\n }\npublic:\n bool reportSpam(vector<string>& message, vector<string>& bannedWords) {\n unordered_set<unsigned int> banned;\n for (const auto& s : bannedWords) {\n banned.insert(hash(s));\n }\n int r = 0;\n for (const auto& s : message) {\n if ((r += banned.count(hash(s))) == 2) return true;\n }\n return false;\n }\n};",
"memory": "145200"
} |
3,541 | <p>You are given an array of strings <code>message</code> and an array of strings <code>bannedWords</code>.</p>
<p>An array of words is considered <strong>spam</strong> if there are <strong>at least</strong> two words in it that <b>exactly</b> match any word in <code>bannedWords</code>.</p>
<p>Return <code>true</code> if the array <code>message</code> is spam, and <code>false</code> otherwise.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","world","leetcode"], bannedWords = ["world","hello"]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The words <code>"hello"</code> and <code>"world"</code> from the <code>message</code> array both appear in the <code>bannedWords</code> array.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>Only one word from the <code>message</code> array (<code>"programming"</code>) appears in the <code>bannedWords</code> array.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code></li>
<li><code>1 <= message[i].length, bannedWords[i].length <= 15</code></li>
<li><code>message[i]</code> and <code>bannedWords[i]</code> consist only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n const int W = 101;\n unsigned long long hash(const string &s) {\n unsigned long long r = 0;\n for (char c : s) {\n r = r * W + c;\n }\n return r;\n }\npublic:\n bool reportSpam(vector<string>& message, vector<string>& bannedWords) {\n unordered_set<unsigned long long> banned;\n for (const auto& s : bannedWords) {\n banned.insert(hash(s));\n }\n int r = 0;\n for (const auto& s : message) {\n if ((r += banned.count(hash(s))) == 2) return true;\n }\n return false;\n }\n};",
"memory": "146200"
} |
3,541 | <p>You are given an array of strings <code>message</code> and an array of strings <code>bannedWords</code>.</p>
<p>An array of words is considered <strong>spam</strong> if there are <strong>at least</strong> two words in it that <b>exactly</b> match any word in <code>bannedWords</code>.</p>
<p>Return <code>true</code> if the array <code>message</code> is spam, and <code>false</code> otherwise.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","world","leetcode"], bannedWords = ["world","hello"]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The words <code>"hello"</code> and <code>"world"</code> from the <code>message</code> array both appear in the <code>bannedWords</code> array.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>Only one word from the <code>message</code> array (<code>"programming"</code>) appears in the <code>bannedWords</code> array.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code></li>
<li><code>1 <= message[i].length, bannedWords[i].length <= 15</code></li>
<li><code>message[i]</code> and <code>bannedWords[i]</code> consist only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\n const int W = 101;\n unsigned long long hash(const string &s) {\n unsigned long long r = 0;\n for (char c : s) {\n r = r * W + c;\n }\n return r;\n }\npublic:\n bool reportSpam(vector<string>& message, vector<string>& bannedWords) {\n unordered_set<unsigned long long> banned;\n for (const auto& s : bannedWords) {\n banned.insert(hash(s));\n }\n int r = 0;\n for (const auto& s : message) {\n if ((r += banned.count(hash(s))) == 2) return true;\n }\n return false;\n }\n};",
"memory": "146300"
} |
3,541 | <p>You are given an array of strings <code>message</code> and an array of strings <code>bannedWords</code>.</p>
<p>An array of words is considered <strong>spam</strong> if there are <strong>at least</strong> two words in it that <b>exactly</b> match any word in <code>bannedWords</code>.</p>
<p>Return <code>true</code> if the array <code>message</code> is spam, and <code>false</code> otherwise.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","world","leetcode"], bannedWords = ["world","hello"]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The words <code>"hello"</code> and <code>"world"</code> from the <code>message</code> array both appear in the <code>bannedWords</code> array.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>Only one word from the <code>message</code> array (<code>"programming"</code>) appears in the <code>bannedWords</code> array.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code></li>
<li><code>1 <= message[i].length, bannedWords[i].length <= 15</code></li>
<li><code>message[i]</code> and <code>bannedWords[i]</code> consist only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\n const int W = 59;\n unsigned int hash(const string &s) {\n unsigned int r = 0;\n for (char c : s) {\n r = r * W + c;\n }\n return r;\n }\npublic:\n bool reportSpam(vector<string>& message, vector<string>& bannedWords) {\n unordered_set<unsigned int> banned;\n for (const auto& s : bannedWords) {\n banned.insert(hash(s));\n }\n int r = 0;\n for (const auto& s : message) {\n if ((r += banned.count(hash(s))) == 2) return true;\n }\n return false;\n }\n};",
"memory": "146400"
} |
3,541 | <p>You are given an array of strings <code>message</code> and an array of strings <code>bannedWords</code>.</p>
<p>An array of words is considered <strong>spam</strong> if there are <strong>at least</strong> two words in it that <b>exactly</b> match any word in <code>bannedWords</code>.</p>
<p>Return <code>true</code> if the array <code>message</code> is spam, and <code>false</code> otherwise.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","world","leetcode"], bannedWords = ["world","hello"]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The words <code>"hello"</code> and <code>"world"</code> from the <code>message</code> array both appear in the <code>bannedWords</code> array.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>Only one word from the <code>message</code> array (<code>"programming"</code>) appears in the <code>bannedWords</code> array.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code></li>
<li><code>1 <= message[i].length, bannedWords[i].length <= 15</code></li>
<li><code>message[i]</code> and <code>bannedWords[i]</code> consist only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool reportSpam(vector<string>& message, vector<string>& bannedWords) {\n unordered_set<string> b(begin(bannedWords), end(bannedWords));\n int cnt = 0;\n for(auto& m : message) {\n if(b.count(m)) {\n if(++cnt >= 2) break;\n }\n }\n return cnt >= 2;\n }\n};\n",
"memory": "153500"
} |
3,541 | <p>You are given an array of strings <code>message</code> and an array of strings <code>bannedWords</code>.</p>
<p>An array of words is considered <strong>spam</strong> if there are <strong>at least</strong> two words in it that <b>exactly</b> match any word in <code>bannedWords</code>.</p>
<p>Return <code>true</code> if the array <code>message</code> is spam, and <code>false</code> otherwise.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","world","leetcode"], bannedWords = ["world","hello"]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The words <code>"hello"</code> and <code>"world"</code> from the <code>message</code> array both appear in the <code>bannedWords</code> array.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>Only one word from the <code>message</code> array (<code>"programming"</code>) appears in the <code>bannedWords</code> array.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code></li>
<li><code>1 <= message[i].length, bannedWords[i].length <= 15</code></li>
<li><code>message[i]</code> and <code>bannedWords[i]</code> consist only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool reportSpam(vector<string>& message, vector<string>& bannedWords) {\n assert(message.size() > 0 && message.size() <= 100000);\n assert(bannedWords.size() > 0 && bannedWords.size() <= 100000);\n for (const auto& s : bannedWords) {\n assert(s.length() > 0 && s.length() <= 15);\n for (char c : s) {\n assert(islower(c));\n }\n }\n const unordered_set<string> banned(bannedWords.begin(), bannedWords.end());\n int r = 0;\n for (const auto& s : message) {\n for (char c : s) {\n assert(islower(c));\n }\n assert(s.length() > 0 && s.length() <= 15);\n if ((r += banned.count(s)) == 2) return true;\n }\n return false;\n }\n};",
"memory": "153600"
} |
3,541 | <p>You are given an array of strings <code>message</code> and an array of strings <code>bannedWords</code>.</p>
<p>An array of words is considered <strong>spam</strong> if there are <strong>at least</strong> two words in it that <b>exactly</b> match any word in <code>bannedWords</code>.</p>
<p>Return <code>true</code> if the array <code>message</code> is spam, and <code>false</code> otherwise.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","world","leetcode"], bannedWords = ["world","hello"]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The words <code>"hello"</code> and <code>"world"</code> from the <code>message</code> array both appear in the <code>bannedWords</code> array.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>Only one word from the <code>message</code> array (<code>"programming"</code>) appears in the <code>bannedWords</code> array.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code></li>
<li><code>1 <= message[i].length, bannedWords[i].length <= 15</code></li>
<li><code>message[i]</code> and <code>bannedWords[i]</code> consist only of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool reportSpam(vector<string>& message, vector<string>& bannedWords) {\n \n unordered_set<string> s;\n \n for(auto x:bannedWords)\n {\n s.insert(x);\n }\n \n int cnt=0;\n \n for(auto x:message)\n {\n cnt+=(s.find(x)!=s.end());\n }\n \n return cnt>1;\n }\n};",
"memory": "153700"
} |
3,541 | <p>You are given an array of strings <code>message</code> and an array of strings <code>bannedWords</code>.</p>
<p>An array of words is considered <strong>spam</strong> if there are <strong>at least</strong> two words in it that <b>exactly</b> match any word in <code>bannedWords</code>.</p>
<p>Return <code>true</code> if the array <code>message</code> is spam, and <code>false</code> otherwise.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","world","leetcode"], bannedWords = ["world","hello"]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The words <code>"hello"</code> and <code>"world"</code> from the <code>message</code> array both appear in the <code>bannedWords</code> array.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>Only one word from the <code>message</code> array (<code>"programming"</code>) appears in the <code>bannedWords</code> array.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code></li>
<li><code>1 <= message[i].length, bannedWords[i].length <= 15</code></li>
<li><code>message[i]</code> and <code>bannedWords[i]</code> consist only of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool reportSpam(std::vector<std::string>& message, std::vector<std::string>& bannedWords) {\n std::unordered_set<std::string> bannedSet(bannedWords.begin(), bannedWords.end());\n int count = 0;\n \n for (const std::string& word : message) {\n if (bannedSet.find(word) != bannedSet.end()) {\n count++;\n if (count >= 2) {\n return true;\n }\n }\n }\n \n return false;\n }\n};",
"memory": "153800"
} |
3,541 | <p>You are given an array of strings <code>message</code> and an array of strings <code>bannedWords</code>.</p>
<p>An array of words is considered <strong>spam</strong> if there are <strong>at least</strong> two words in it that <b>exactly</b> match any word in <code>bannedWords</code>.</p>
<p>Return <code>true</code> if the array <code>message</code> is spam, and <code>false</code> otherwise.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","world","leetcode"], bannedWords = ["world","hello"]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The words <code>"hello"</code> and <code>"world"</code> from the <code>message</code> array both appear in the <code>bannedWords</code> array.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>Only one word from the <code>message</code> array (<code>"programming"</code>) appears in the <code>bannedWords</code> array.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code></li>
<li><code>1 <= message[i].length, bannedWords[i].length <= 15</code></li>
<li><code>message[i]</code> and <code>bannedWords[i]</code> consist only of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool reportSpam(vector<string>& message, vector<string>& bw) {\n set <string> st(begin(bw),end(bw));\n int ct=0;\n for(auto &s:message) {\n if(st.count(s)) ++ct;\n }\n return ct>=2?true:false;\n }\n};",
"memory": "154400"
} |
3,573 | <p>You are given two strings <code>word1</code> and <code>word2</code>.</p>
<p>A string <code>x</code> is called <strong>valid</strong> if <code>x</code> can be rearranged to have <code>word2</code> as a <span data-keyword="string-prefix">prefix</span>.</p>
<p>Return the total number of <strong>valid</strong> <span data-keyword="substring-nonempty">substrings</span> of <code>word1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "bcca", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only valid substring is <code>"bcca"</code> which can be rearranged to <code>"abcc"</code> having <code>"abc"</code> as a prefix.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<p>All the substrings except substrings of size 1 and size 2 are valid.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "aaabc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word1.length <= 10<sup>5</sup></code></li>
<li><code>1 <= word2.length <= 10<sup>4</sup></code></li>
<li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long validSubstringCount(string word1, string word2) {\n \n vector<int> a(26);\n vector<int> target(26);\n \n for(auto x:word2)\n {\n target[x-'a']+=1;\n }\n \n long long ans=0;\n \n int i=0,j=0;\n \n while(j<word1.length()||i<word1.length())\n {\n int flag=1;\n \n for(int k=0;k<26;k++)\n {\n if(a[k]<target[k])\n {\n flag=0;\n }\n }\n \n if(flag)\n {\n ans+=word1.length()-j+1;\n a[word1[i]-'a']-=1;\n i++;\n }\n else\n {\n if(j==word1.length())\n {\n break;\n }\n \n a[word1[j]-'a']+=1;\n j++;\n }\n }\n \n return ans;\n }\n};",
"memory": "13500"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.