id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n\n int n = nums.size();\n vector<bool> dp(n, false); // valid arr: is nums arr valid from start to curr i\n\n dp[0]=false; // single num must be invalid\n\n for(int i=1; i<n; i++){\n\n //case1: \n if(nums[i]==nums[i-1]){\n if(i-2<0){\n dp[i]=true;\n }\n else if(dp[i-2]){\n dp[i]=true;\n }\n }\n\n //case2:\n if(i-2>=0 && nums[i]==nums[i-1] && nums[i-1]==nums[i-2]){\n if(i-3<0){\n dp[i]=true;\n }\n else if(dp[i-3]){\n dp[i]=true;\n }\n }\n\n //case3:\n if(i-2>=0 && nums[i]==nums[i-1]+1 && nums[i-1]==nums[i-2]+1){\n if(i-3<0){\n dp[i]=true;\n }\n else if(dp[i-3]){\n dp[i]=true;\n }\n }\n\n }\n\n return dp[n-1];\n\n }\n};", "memory": "86900" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n int dp[n];\n if(n==1)return 0;\n dp[0] = 0;\n dp[1] = (nums[0]==nums[1])?1:0;\n if(n==2)return dp[1];\n if(dp[1]==1 and nums[0]==nums[2]){\n dp[2] = 1;\n }\n else if(nums[0] + 2 == nums[2] and nums[1] + 1 == nums[2]){\n dp[2] = 1;\n }\n else{\n dp[2]=0;\n }\n for(int i=3;i<n;i++){\n if(dp[i-3]==1 and ((nums[i-1]==nums[i] and \n nums[i-2]==nums[i]) or (nums[i-2]+2==nums[i] and nums[i-1]+1==nums[i]))){\n dp[i]=1;\n }\n else if(dp[i-2]==1 and nums[i-1]==nums[i]){\n dp[i] = 1;\n }\n else{\n dp[i] = 0;\n }\n }\n // for(int i=0;i<n;i++){\n // cout<<dp[i]<<\" \";\n // }\n return dp[n-1];\n }\n};", "memory": "87000" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) \n {\n int n=nums.size();\n vector<bool> dp1(n,false); \n vector<bool> dp2(n,false); \n vector<bool> dp3(n,false);\n for(int i=0;i<n;i++)\n {\n if(i>=2)\n {\n if(nums[i]-nums[i-1]==1 and nums[i-1]-nums[i-2]==1)\n {\n dp1[i-2]=true;\n }\n if(nums[i]==nums[i-1] and nums[i]==nums[i-2])\n {\n dp2[i-2]=true;\n }\n }\n if(i>=1)\n {\n if(nums[i]==nums[i-1])\n {\n dp3[i-1]=true;\n }\n }\n } \n vector<bool> dp4(n+1,false);\n dp4[0]=true;\n for(int i=0;i<n;i++)\n {\n if(dp3[i] and dp4[i])dp4[i+2]=true;\n if((dp2[i] or dp1[i]) and dp4[i])dp4[i+3]=true;\n }\n // cout<<\"dp2 dp3 dp4 \"<<endl;\n // for(int i=0;i<n;i++)\n // {\n // cout<<dp2[i]<<\" \"<<dp3[i]<<\" \"<<dp4[i]<<endl;\n // }\n return dp4[n];\n }\n};", "memory": "87300" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool solve(vector<int>& nums,int i){\n if(i <= -1){\n return true;\n }\n if(i >=2 && nums[i] == nums[i-1] && nums[i-1] == nums[i-2]){\n if(solve(nums,i-3)){\n return true;\n }\n }\n if(i >=1 && nums[i] == nums[i-1]){\n if(solve(nums,i-2)){\n return true;\n }\n }\n if(i >=2 && nums[i] == nums[i-1]+1 && nums[i-1] == nums[i-2]+1){\n if(solve(nums,i-3)){\n return true;\n }\n }\n return false;\n }\n bool validPartition(vector<int>& nums) {\n return solve(nums, nums.size()-1);\n }\n};", "memory": "88900" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\nint t[100005];\nbool isValid1(vector<int>& nums, int idx){\n\n if(idx+1 < nums.size() && nums[idx] == nums[idx+1]){\n return true;\n }\n\n return false;\n\n}\n\nbool isValid2(vector<int>& nums, int idx){\n\n if(idx+1 < nums.size() && idx+2 < nums.size() && nums[idx] == nums[idx+1] && nums[idx+1] == nums[idx+2]){\n return true;\n }else if(idx+1 < nums.size() && idx+2 < nums.size() && nums[idx] + 1 == nums[idx+1] && nums[idx+1] + 1 == nums[idx+2]){\n return true;\n }\n\n return false;\n\n}\n\n\nbool solve(vector<int>& nums, int idx){\n\n if(idx >= nums.size()){\n return true;\n }\n\n if(t[idx] != -1){\n return t[idx];\n }\n\n bool ans1 = false;\n bool ans2 = false;\n\n if(isValid1(nums, idx)){\n ans1 = solve(nums, idx + 2);\n }\n if(isValid2(nums, idx)){\n ans2 = solve(nums, idx + 3);\n }\n\n return t[idx] = ans1 || ans2;\n\n}\n\n bool validPartition(vector<int>& nums) {\n memset(t, -1, sizeof(t));\n return solve(nums, 0);\n }\n};", "memory": "89700" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int x[100010];\n bool dp(int i,vector<int> &nums){\n int n=nums.size();\n if(i==n) return true;\n if(n-i<2) return false;\n if(x[i]!=-1) return x[i];\n bool s1=false,s2=false;\n if(n-i>2){\n s1=(nums[i]==nums[i+1] && nums[i+1]==nums[i+2] && dp(i+3,nums));\n s2=(nums[i+1]-nums[i]==1 && nums[i+2]-nums[i+1]==1 && dp(i+3,nums));\n }\n bool s3=(nums[i]==nums[i+1]) && dp(i+2,nums);\n return x[i]=s3|s2|s1;\n\n }\n bool validPartition(vector<int>& nums) {\n memset(x,-1,sizeof(x));\n return dp(0,nums);\n }\n};", "memory": "89800" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int dp[100001];\n bool two_con(int i, vector<int> &nums) {\n if (i+1 >= nums.size()) return false;\n if (nums[i] == nums[i+1]) return true;\n return false;\n }\n bool three_con(int i, vector<int> &nums) {\n if (i+2 >= nums.size()) return false;\n\n if (nums[i] == nums[i+1] && nums[i] == nums[i+2]) return true;\n return false;\n }\n bool three_inc(int i, vector<int> &nums) {\n if (i+2 >= nums.size()) return false;\n\n if (nums[i] == nums[i+1] - 1 && nums[i] == nums[i+2] - 2) return true;\n return false;\n }\n bool util(int i, vector<int> &nums) {\n if (i >= nums.size()) return true;\n\n if (dp[i] != -1) return dp[i];\n bool ans = 0;\n if (two_con(i, nums)) ans |= util(i+2, nums);\n if (ans) return ans;\n if (three_con(i, nums)) ans |= util(i+3, nums);\n if (ans) return ans;\n if (three_inc(i, nums)) ans |= util(i+3, nums);\n\n return dp[i] = ans;\n }\n bool validPartition(vector<int>& nums) {\n memset(dp, -1, sizeof(dp));\n return util(0, nums);\n }\n};", "memory": "89900" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n // int rec(int i, int &n, vector<int>& nums, vector<int> &dp)\n // {\n // if(i==n) return 1;\n\n // if(dp[i] != -1) return dp[i];\n\n // if(i < n-1)\n // {\n // if(nums[i] == nums[i+1])\n // {\n // if(rec(i+2, n, nums, dp)==1) return dp[i] = 1;\n\n // if(i<n-2 && nums[i+1]==nums[i+2])\n // {\n // return dp[i] = rec(i+3, n, nums, dp);\n // }\n // }\n // }\n // if(i<n-2 && nums[i]+1 == nums[i+1] && nums[i+1]+1== nums[i+2])\n // {\n // return dp[i] = rec(i+3, n, nums, dp);\n // }\n // return dp[i] = 0;\n // }\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n vector<int> dp(n+1, 0);\n dp[n]=1;\n for(int i = n-2; i>=0; i--)\n {\n if(nums[i]==nums[i+1])\n {\n dp[i] = max(dp[i], dp[i+2]);\n if(dp[i]) continue;\n if(i<n-2 && nums[i+1]==nums[i+2])\n {\n dp[i] = dp[i+3];\n continue;\n } \n }\n if(i<n-2 && nums[i]+1 == nums[i+1] && nums[i+1]+1== nums[i+2])\n {\n dp[i] = dp[i+3];\n }\n }\n return dp[0];\n }\n};", "memory": "90000" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\nprivate:\n bool solve(int ind, int n, vector<int>&nums, vector<int>&dp){\n // base case\n if(ind==n){\n return true;\n }\n\n if(dp[ind]!=-1) return dp[ind];\n\n // nums[i] == nums[i+1]\n if(ind+1<n && nums[ind]==nums[ind+1]){\n bool res = solve(ind+2,n,nums,dp);\n if(res){\n return dp[ind] = res;\n }\n }\n\n // nums[i] == nums[i+1] == nums[i+2]\n if(ind+2<n && nums[ind]==nums[ind+1] && nums[ind+1]==nums[ind+2]){\n bool res = solve(ind+3,n,nums,dp);\n if(res){\n return dp[ind] = res;\n }\n }\n\n // nums[i+1] - nums[i] == nums[i+2] - nums[i+1] == 1\n if(ind+2<n && nums[ind+1]-nums[ind] == 1 && nums[ind+2] - nums[ind+1] == 1){\n bool res = solve(ind+3,n,nums,dp);\n if(res){\n return dp[ind] = res;\n }\n }\n\n return dp[ind] = false; \n }\npublic:\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n vector<int> dp(n+1,-1);\n // base case\n dp[n] = true;\n\n for(int ind = n-1; ind>=0; ind--){\n // nums[i] == nums[i+1]\n if(ind+1<n && nums[ind]==nums[ind+1]){\n bool res = dp[ind+2];\n if(res){\n dp[ind] = res;\n continue;\n }\n }\n\n // nums[i] == nums[i+1] == nums[i+2]\n if(ind+2<n && nums[ind]==nums[ind+1] && nums[ind+1]==nums[ind+2]){\n bool res = dp[ind+3];\n if(res){\n dp[ind] = res;\n continue;\n }\n }\n\n // nums[i+1] - nums[i] == nums[i+2] - nums[i+1] == 1\n if(ind+2<n && nums[ind+1]-nums[ind] == 1 && nums[ind+2] - nums[ind+1] == 1){\n bool res = dp[ind+3];\n if(res){\n dp[ind] = res;\n continue;\n }\n }\n\n dp[ind] = false;\n }\n\n return dp[0];\n }\n};", "memory": "90100" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool validPartition(vector<int>& arr) {\n int n=arr.size();\n if(n<2)\n return false;\n vector<int> dp(n,0);\n if(arr[0]==arr[1])\n dp[1]=1;\n if(n>2){\n if(arr[0]==arr[1] && arr[1]==arr[2]){\n dp[2]=1;\n }\n if(arr[0]==arr[1]-1 && arr[0]==arr[2]-2){\n dp[2]=1;\n }\n }\n \n // cout<<dp[0]<<\" \"<<dp[1]<<\" \";\n for(int i=2;i<n;i++){\n // cout<<i<<endl;\n if(arr[i]==arr[i-1]){\n dp[i]|=dp[i-2];\n }\n // cout<<\"yes\"<<endl;\n if(i>=3 && ((arr[i]==arr[i-1] && arr[i]==arr[i-2]) || (arr[i]==arr[i-1]+1 && arr[i]==arr[i-2]+2))){\n dp[i]|=dp[i-3];\n // cout<<i<<\"yes\"<<endl;\n }\n // cout<<\"yes\"<<endl;\n // cout<<dp[i]<<\" \";\n }\n return dp[n-1]==1;\n }\n};", "memory": "90200" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n vector<int>dp(n, 0);\n int curlen = 1;\n int consecutive = 1;\n int i = 1;\n while(i < n){\n if(nums[i] == nums[i - 1]){\n curlen++;\n }\n else{\n curlen = 1;\n }\n if(nums[i] - nums[i - 1] == 1){\n consecutive++;\n }\n else{\n consecutive = 1;\n }\n if(curlen>= 2){\n if((i - 2 < 0) || (dp[i - 2] == true)){\n dp[i] = true;\n }\n }\n if(curlen>=3){\n if((i - 3 < 0) || (dp[i - 3] == true)){\n dp[i] = true;\n }\n }\n if(consecutive >= 3){\n if((i - 3 < 0) || (dp[i - 3] == true)){\n dp[i] = true;\n // consecutive = 1;\n // i++;\n }\n }\n // cout<<curlen<<endl;\n i++;\n }\n // for(auto it : dp){\n // cout<<it<<\" \";\n // }\n // cout<<endl;\n return dp[n - 1];\n }\n};", "memory": "90300" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n int n=nums.size();\n vector<int>dp(n,0);\n int len=1, consecutive=1;\n for(int i=1; i<n; i++){\n if(nums[i]==nums[i-1]) len++;\n else len=1;\n if(nums[i]-nums[i-1]==1) consecutive++;\n else consecutive=1;\n\n if(len>=2 && (i-2<0 || dp[i-2]==1)){\n dp[i]=1;\n // len=1;\n }\n if(len>=3 && (i-3<0 || dp[i-3]==1)){\n dp[i]=1;\n }\n if(consecutive>=3 && (i-3<0 || dp[i-3]==1)){\n dp[i]=1;\n }\n }\n return dp[n-1];\n }\n};", "memory": "90300" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n int n=nums.size();\n vector<int> dp(n+3,0);\n dp[n]=1;\n dp[n+1]=1;\n dp[n+2]=2;\n for(int i=n-1;i>=0;i--)\n {\n if(i+1<n && nums[i]==nums[i+1]) dp[i] |= dp[i+2];\n if(i+2<n && nums[i]==nums[i+1] && nums[i]==nums[i+2]) dp[i] |= dp[i+3];\n if(i+2<n && nums[i+1]-nums[i]==1 && nums[i+2]-nums[i+1]==1) dp[i] |= dp[i+3];\n }\n return dp[0];\n }\n};", "memory": "90400" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int>dp;\n bool Valid(vector<int>& nums, int start, int end){\n int len = end - start + 1;\n if(len > 3) return false;\n if(len == 2) return nums[start] == nums[end];\n if (len == 3) return (nums[start] == nums[start+1] && nums[start+1] == nums[end]) ||\n (nums[start] + 1 == nums[start+1] && nums[start+1] + 1 == nums[end]);\n return false;\n }\n // int vini(vector<int>& nums, int ind){\n // int n = nums.size();\n // if(ind == n) return true;\n // if(dp[ind] != -1) return dp[ind];\n // for(int i = ind; i < n; i++){\n // if(i > ind && Valid(nums, ind, i)){\n // if(vini(nums, i+1)) return dp[ind] = true;\n // }\n // }\n // return dp[ind] = false;\n // }\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n dp.resize(n+1, 0);\n vector<bool> isValidPartition(n, false);\n \n // return vini(nums, 0);\n dp[n] = true;\n for(int ind = n-1; ind >= 0; ind--){\n for(int i = ind; i < min(n, ind+3); i++){\n if(Valid(nums, ind, i)){\n if(dp[i+1]){\n dp[ind] = true;\n break;\n }\n }\n }\n }\n return dp[0];\n }\n};", "memory": "90500" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int>dp;\n bool Valid(vector<int>& nums, int start, int end){\n int len = end - start + 1;\n if(len > 3) return false;\n if(len == 2) return nums[start] == nums[end];\n if (len == 3) return (nums[start] == nums[start+1] && nums[start+1] == nums[end]) ||\n (nums[start] + 1 == nums[start+1] && nums[start+1] + 1 == nums[end]);\n return false;\n }\n // int vini(vector<int>& nums, int ind){\n // int n = nums.size();\n // if(ind == n) return true;\n // if(dp[ind] != -1) return dp[ind];\n // for(int i = ind; i < n; i++){\n // if(i > ind && Valid(nums, ind, i)){\n // if(vini(nums, i+1)) return dp[ind] = true;\n // }\n // }\n // return dp[ind] = false;\n // }\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n dp.resize(n+1, 0);\n vector<bool> isValidPartition(n, false);\n \n // return vini(nums, 0);\n dp[n] = true;\n for(int ind = n-1; ind >= 0; ind--){\n for(int i = ind; i < min(n, ind+3); i++){\n if(Valid(nums, ind, i)){\n if(dp[i+1]){\n dp[ind] = true;\n break;\n }\n }\n }\n }\n return dp[0];\n }\n};", "memory": "90500" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int dp[100005];\n int solve(int i, vector<int>&arr){\n if(i>=arr.size()){\n return true;\n }\n if(dp[i]!=-1) return dp[i];\n \n bool op1 = (i+1<arr.size()) and (arr[i]==arr[i+1]);\n bool op2 = (i+2<arr.size()) and (arr[i]==arr[i+1] and arr[i+1]==arr[i+2]);\n bool op3 = (i+2<arr.size()) and ((arr[i+1]-arr[i])==1 and (arr[i+2]-arr[i+1]==1));\n \n bool temp = false;\n if(op1){\n temp =solve(i+2,arr);\n }\n if((op2 or op3) and !temp){\n temp = temp | solve(i+3,arr);\n }\n \n return dp[i] = temp;\n }\n bool validPartition(vector<int>& nums) {\n memset(dp,-1,sizeof(dp));\n return solve(0,nums);\n }\n};\n\n// 1 3 2\n \n \n// 1 2 3", "memory": "90700" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int dp[100005];\n int solve(int i, vector<int>&arr){\n if(i>=arr.size()){\n return true;\n }\n if(dp[i]!=-1) return dp[i];\n \n bool op1 = (i+1<arr.size()) and (arr[i]==arr[i+1]);\n bool op2 = (i+2<arr.size()) and (arr[i]==arr[i+1] and arr[i+1]==arr[i+2]);\n bool op3 = (i+2<arr.size()) and ((arr[i+1]-arr[i])==1 and (arr[i+2]-arr[i+1]==1));\n \n bool temp = false;\n if(op1){\n temp =solve(i+2,arr);\n }\n if((op2 or op3) and !temp){\n temp = temp | solve(i+3,arr);\n }\n \n return dp[i] = temp;\n }\n bool validPartition(vector<int>& nums) {\n memset(dp,-1,sizeof(dp));\n return solve(0,nums);\n }\n};\n\n// 1 3 2\n \n \n// 1 2 3", "memory": "90700" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int dp[100001];\n bool valid(int i, int k, vector<int>& nums){\n if(k-i + 1 == 2){\n return (nums[i] == nums[k]);\n }else{\n return (nums[i] == nums[i+1] && nums[i+1] == nums[k] || (nums[i+1] - nums[i] == 1 && nums[k]-nums[i+1] == 1));\n }\n }\n bool solve(int i, vector<int>& nums){\n int n = nums.size();\n\n if(i >= n){\n return true;\n }\n\n if(dp[i] != -1) return dp[i];\n\n bool ans = false;\n for(int k = i + 1; k < n && k < i+3 ; k++){\n if(valid(i,k,nums)){\n ans = ans | solve(k+1,nums);\n if(ans) return true;\n }\n }\n\n return dp[i] = ans;\n }\n bool validPartition(vector<int>& nums) {\n //index, nums taken, nums\n memset(dp,-1,sizeof(dp));\n\n return solve(0,nums);\n }\n};", "memory": "90800" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool check(vector<int>& nums, int idx) {\n if (idx < 0) return true;\n\n if (idx > 0 && nums[idx] == nums[idx-1]) {\n if (check(nums, idx-2)) return true;\n }\n\n if (idx > 1 && nums[idx] == nums[idx-1] && nums[idx-1] == nums[idx-2]) {\n if (check(nums, idx-3)) return true;\n }\n\n if (idx > 1 && nums[idx] == nums[idx-1]+1 && nums[idx-1] == nums[idx-2]+1) {\n if (check(nums, idx-3)) return true;\n }\n\n return false;\n }\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n\n // vector<int> dp(n, 0);\n return check(nums, n-1);\n\n }\n};", "memory": "90900" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n return solve(n - 1, nums);\n }\n\n // checks if there is a valid partition ending with an index\n bool solve(int index, vector<int>& nums) {\n if (index <= 0) {\n return false;\n }\n \n bool any = false;\n if (nums[index - 1] == nums[index]) {\n // cout << \"found double equal ending at \" << index << endl;\n if (index - 1 == 0) {\n return true;\n }\n else {\n any = any || solve(index - 2, nums);\n }\n }\n if (index - 2 >= 0 && nums[index - 1] == nums[index] && nums[index - 2] == nums[index]) {\n // cout << \"found triple equal ending at \" << index << endl;\n if (index - 2 == 0) {\n return true;\n }\n else {\n any = any || solve(index - 3, nums);\n }\n }\n if (\n index - 2 >= 0 && nums[index - 1] == nums[index] - 1 && nums[index - 2] == nums[index] - 2\n ) {\n // cout << \"found consecutive ending at \" << index << endl;\n if (index - 2 == 0) {\n return true;\n }\n else {\n any = any || solve(index - 3, nums);\n }\n }\n\n return any;\n }\n};", "memory": "91000" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n return partition(nums, n - 1);\n }\n\n bool partition(vector<int>& nums, int index) \n {\n if (index < 0) {\n return true;\n }\n\n // if 2 are same\n if (index >= 1 && nums[index] == nums[index - 1]) {\n if (partition(nums, index - 2)) {\n return true;\n }\n }\n\n // exactly 3\n if (index >= 2 && nums[index] == nums[index - 1] &&\n nums[index - 1] == nums[index - 2]) {\n if (partition(nums, index - 3)) {\n return true;\n }\n }\n\n // Check if the last 3 elements form a consecutive increasing sequence\n if (index >= 2 && nums[index] - nums[index - 1] == 1 &&\n nums[index - 1] - nums[index - 2] == 1) {\n if (partition(nums, index - 3)) {\n return true;\n }\n }\n\n\n return false;\n }\n};", "memory": "91100" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int dp[100001];\n bool sol(int i, int n, vector<int>&nums)\n {\n if(i >= n) return true;\n if(dp[i] != -1) return dp[i];\n bool ans = false;\n\n if(i + 2 < n){\n if(nums[i] == nums[i + 1] && nums[i + 1] == nums[i + 2]){\n ans = ans | sol(i + 3, n, nums);\n }\n if(nums[i + 2] - nums[i] == 2 && nums[i + 2] - nums[i + 1] == 1){\n ans = ans | sol(i + 3, n, nums);\n }\n }\n if(i + 1 < n){\n if(nums[i] == nums[i + 1])\n ans = ans | sol(i + 2, n, nums);\n }\n\n return dp[i] = ans;\n }\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n memset(dp, -1, sizeof(dp));\n return sol(0, n, nums);\n }\n};", "memory": "91200" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool dp[100002];\n bool vis[100002];\n bool solve(int i, vector<int>& nums) {\n int n = nums.size();\n if (i == n) return true; // Base case: all elements are partitioned\n\n if(vis[i]) return dp[i];\n\n // Option 1: Check for pair\n if (i + 1 < n && nums[i] == nums[i + 1]) {\n if (solve(i + 2, nums))\n {\n vis[i] = true;\n return dp[i] = true;\n }\n }\n\n // Option 2: Check for triplet (all equal)\n if (i + 2 < n && nums[i] == nums[i + 1] && nums[i] == nums[i + 2]) {\n if (solve(i + 3, nums))\n {\n vis[i] = true;\n return dp[i] = true;\n }\n }\n\n // Option 3: Check for triplet (consecutive numbers)\n if (i + 2 < n && nums[i + 1] == nums[i] + 1 && nums[i + 2] == nums[i + 1] + 1) {\n if (solve(i + 3, nums)){\n vis[i] = true;\n return dp[i] = true;\n }\n }\n\n vis[i] = true;\n return dp[i] = false; // No valid partition found\n}\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n memset(dp , 0 , sizeof(dp));\n memset(dp , 0 , sizeof(dp));\n return solve(0 , nums);\n }\n};", "memory": "91300" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int dp[100001];\n bool solve(vector<int>& nums, int idx){\n int n = nums.size();\n if(idx >= n){\n return true;\n }\n if(dp[idx] != -1){\n return dp[idx];\n }\n bool result = false;\n if(idx+1 < n && nums[idx] == nums[idx+1]){\n result = solve(nums, idx+2);\n if(result == true){\n return dp[idx] = true;\n }\n }\n if(idx+1 < n && idx+2 < n && nums[idx] == nums[idx+1] && nums[idx+1] == nums[idx+2]){\n result = solve(nums, idx+3);\n if(result == true){\n return dp[idx] = true;\n }\n \n }\n if(idx+1 < n && idx+2 < n && nums[idx+1] - nums[idx] == 1 && nums[idx+2] - nums[idx+1] == 1 ){\n result = solve(nums, idx+3);\n if(result == true){\n return dp[idx] = true;\n }\n \n }\n return dp[idx] = false;\n }\n bool validPartition(vector<int>& nums) {\n memset(dp, -1, sizeof(dp));\n return solve(nums, 0);\n }\n};", "memory": "91400" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int dp[100010];\n int n;\n int rec(int level,vector<int>& nums){\n if(level>=n)return true;\n if(dp[level]!=-1)return dp[level];\n \n if(level<n-1 and nums[level+1]==nums[level]){\n dp[level]=rec(level+2,nums);\n if(dp[level])return true;\n if(level<n-2 and nums[level+2]==nums[level]){\n dp[level]=rec(level+3,nums);\n if(dp[level])return true;\n }\n }\n\n if(level<n-2 and nums[level+1]-nums[level]==1 and nums[level+2]-nums[level+1]==1){\n dp[level]=rec(level+3,nums);\n if(dp[level])return true;\n }\n return dp[level]=false;\n }\n bool validPartition(vector<int>& nums) {\n n=nums.size();\n memset(dp,-1,sizeof(dp));\n return rec(0,nums);\n }\n};", "memory": "91500" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int dp[100001];\n bool solve(vector<int>& nums, int idx){\n int n = nums.size();\n if(idx >= n){\n return true;\n }\n if(dp[idx] != -1){\n return dp[idx];\n }\n bool result = false;\n if(idx+1 < n && nums[idx] == nums[idx+1] && solve(nums, idx+2)){\n return true;\n }\n if(idx+1 < n && idx+2 < n && nums[idx] == nums[idx+1] && nums[idx+1] == nums[idx+2] && solve(nums, idx+3)){\n return true;\n \n }\n if(idx+1 < n && idx+2 < n && nums[idx+1] - nums[idx] == 1 && nums[idx+2] - nums[idx+1] == 1 && solve(nums, idx+3)){\n return true;\n \n }\n return dp[idx] = false;\n }\n bool validPartition(vector<int>& nums) {\n memset(dp, -1, sizeof(dp));\n return solve(nums, 0);\n }\n};", "memory": "91600" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\nint dp[100005];\n bool f(vector<int>& nums, int i){\n if(i>=nums.size()) return true;\n if(dp[i]!=-1) return dp[i];\n int n = nums.size();\n bool res=false;\n if(i+1<n && nums[i] == nums[i+1]){\n res = res | f(nums, i+2);\n }\n\n if(i+2<n && nums[i]==nums[i+1] && nums[i]==nums[i+2]){\n res = res | f(nums, i+3);\n }\n\n if(i+2 < n && nums[i+1]- nums[i] == 1 && nums[i+2]-nums[i+1]==1){\n res = res | f(nums, i+3);\n }\n\n return dp[i] = res;\n\n }\n bool validPartition(vector<int>& nums) {\n memset(dp, -1, sizeof(dp));\n return f(nums, 0);\n }\n};", "memory": "92200" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "\n\n\n\nclass Solution {\npublic:\n\n int dp[100001];\n\n int helper(int ind, vector<int>& nums, int n){\n if(ind == n){\n return 1;\n }\n if(dp[ind] != -1){\n return dp[ind];\n }\n int ans = 0;\n if(ind <= (n-2) && nums[ind] == nums[ind+1]){\n ans = helper(ind+2, nums, n);\n }\n if((ind <= n-3) && nums[ind] == nums[ind+1] && nums[ind+1] == nums[ind+2]){\n ans |= helper(ind+3, nums, n);\n }\n if((ind <= n-3) && (nums[ind] == nums[ind+1] - 1) && (nums[ind+1] == nums[ind+2] - 1)){\n ans |= helper(ind+3, nums, n);\n }\n return dp[ind] = ans;\n }\n\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n int i = 0;\n memset(dp, -1, sizeof(dp));\n return helper(0, nums, n);\n\n \n\n }\n};", "memory": "92300" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int dp[100001];\n bool solve(int ind , vector<int>& nums){\n int n = nums.size();\n if(ind == n ){\n return true ;\n }\n if(dp[ind] != -1)return dp[ind];\n\n if(ind + 1 < n && nums[ind] == nums[ind+1]){\n if(solve(ind+2 , nums))return dp[ind]=true ;\n }\n\n if(ind + 2 < n && nums[ind] == nums[ind+1] && nums[ind+1] == nums[ind+2]){\n if( solve(ind+3 , nums))return dp[ind]=true ;\n }\n\n if(ind + 2 < n && nums[ind] +1 == nums[ind+1] && nums[ind+1] +1 == nums[ind+2]){\n if( solve(ind+3 , nums))return dp[ind]=true ;\n }\n\n return dp[ind]=false ;\n }\n bool validPartition(vector<int>& nums) {\n memset(dp,-1,sizeof(dp));\n return solve(0,nums);\n }\n};", "memory": "92400" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int dp[100005];\n int func(int indx, vector<int>& nums){\n if (indx == nums.size()) return 1;\n if (dp[indx]!=-1) return dp[indx];\n int ch1 = 0, ch2 = 0, ch3 = 0;\n if (indx+1 < nums.size()){\n if (nums[indx] == nums[indx+1]) ch1 = func(indx+2,nums);\n }\n if (indx+2 < nums.size()){\n if (nums[indx] == nums[indx+1] && nums[indx+1] == nums[indx+2]){\n ch2 = func(indx+3,nums);\n }\n if (nums[indx+1] == nums[indx]+1 && nums[indx+2] == nums[indx+1]+1){\n ch3 = func(indx+3,nums);\n }\n }\n return dp[indx] = ch1 | ch2 | ch3;\n }\n bool validPartition(vector<int>& nums) {\n memset(dp,-1,sizeof(dp));\n return (func(0,nums) == 0) ? false : true;\n }\n};", "memory": "93200" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n\n bool ans(vector<int>& nums,int size,int i,vector<int>& memo){\n\n if(i >= size){\n return true;\n }\n\n if(memo[i] != -1){\n return memo[i];\n }\n\n if(i < (size-2) && nums[i] == nums[i+1] && nums[i] == nums[i+2]){\n\n if(ans(nums,size,i+3,memo)){\n return memo[i] = true;\n }\n \n }\n \n if( i < (size-1) && nums[i] == nums[i+1]){\n\n if(ans(nums,size,i+2,memo)){\n return memo[i] = true;\n }\n\n }\n \n if( i < (size-2) && nums[i] + 1 == nums[i+1] && nums[i] + 2 == nums[i+2]){\n\n if(ans(nums,size,i+3,memo)){\n return memo[i] = true;\n }\n }\n\n \n\n return memo[i] = false;\n\n }\n\n bool validPartition(vector<int>& nums) {\n\n int size = nums.size();\n vector<int> memo(size,-1);\n return ans(nums,size,0,memo);\n \n }\n};", "memory": "93400" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool solve(int i, std::vector<int>& nums, int n,vector<int>&dp) {\n if (i == n) {\n return true;\n }\n\n if (dp[i] != -1) {\n return dp[i];\n }\n\n bool t = false, nt = false;\n if (i + 2 < n) {\n if ((nums[i] == nums[i+1] && nums[i+1] == nums[i+2]) || \n (nums[i+1] - nums[i] == 1 && nums[i+2] - nums[i+1] == 1)) {\n t = solve(i + 3, nums, n,dp);\n }\n }\n \n if (i + 1 < n) {\n if (nums[i] == nums[i+1]) {\n nt = solve(i + 2, nums, n,dp);\n }\n }\n\n dp[i] = t || nt;\n return dp[i];\n }\n\n bool validPartition(std::vector<int>& nums) {\n int n = nums.size();\n vector<int>dp(n,-1);\n return solve(0, nums, n,dp);\n }\n};\n", "memory": "93500" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int n;\n bool recsol(vector<int>&nums,int i,vector<int>&dp)\n {\n if(i>=n) return true;\n if(dp[i]!=-1) return dp[i];\n\n // Rule 1\n if(i+1<n && nums[i]==nums[i+1])\n {if(recsol(nums,i+2,dp)) return dp[i]=true;}\n // Rule 2\n if(i+2<n && nums[i]==nums[i+1] && nums[i+1]==nums[i+2])\n {if(recsol(nums,i+3,dp)) return dp[i]=true;}\n // Rule3\n if(i+2<n && nums[i]+1==nums[i+1] && nums[i+1]+1==nums[i+2])\n {if(recsol(nums,i+3,dp)) return dp[i]=true;}\n return dp[i]=false;\n }\n int tab(vector<int>&nums)\n {\n vector<int>dp(n+1,0);\n dp[n]=true;\n\n for(int i=n-1;i>=0;i--)\n {\n if (i + 1 < n && nums[i] == nums[i + 1]) {\n if (dp[i + 2]) dp[i] = true;\n }\n // Rule 2\n if (i + 2 < n && nums[i] == nums[i + 1] && nums[i + 1] == nums[i + 2]) {\n if (dp[i + 3]) dp[i] = true;\n }\n // Rule 3\n if (i + 2 < n && nums[i] + 1 == nums[i + 1] && nums[i + 1] + 1 == nums[i + 2]) {\n if (dp[i + 3]) dp[i] = true;\n }\n }\n return dp[0];\n }\n bool validPartition(vector<int>& nums) {\n n=nums.size();\n vector<int>dp(n+1,-1);\n // return recsol(nums,0,dp);\n return tab(nums);\n }\n};", "memory": "93600" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int n;\n bool recsol(vector<int>&nums,int i,vector<int>&dp)\n {\n if(i>=n) return true;\n if(dp[i]!=-1) return dp[i];\n\n // Rule 1\n if(i+1<n && nums[i]==nums[i+1])\n {if(recsol(nums,i+2,dp)) return dp[i]=true;}\n // Rule 2\n if(i+2<n && nums[i]==nums[i+1] && nums[i+1]==nums[i+2])\n {if(recsol(nums,i+3,dp)) return dp[i]=true;}\n // Rule3\n if(i+2<n && nums[i]+1==nums[i+1] && nums[i+1]+1==nums[i+2])\n {if(recsol(nums,i+3,dp)) return dp[i]=true;}\n return dp[i]=false;\n }\n int tab(vector<int>&nums)\n {\n vector<int>dp(n+1,0);\n dp[n]=true;\n\n for(int i=n-1;i>=0;i--)\n {\n if (i + 1 < n && nums[i] == nums[i + 1]) {\n dp[i] = dp[i] || dp[i + 2];\n }\n // Rule 2\n if (i + 2 < n && nums[i] == nums[i + 1] && nums[i + 1] == nums[i + 2]) {\n dp[i] = dp[i] || dp[i + 3];\n }\n // Rule 3\n if (i + 2 < n && nums[i] + 1 == nums[i + 1] && nums[i + 1] + 1 == nums[i + 2]) {\n dp[i] = dp[i] || dp[i + 3];\n }\n }\n return dp[0];\n }\n bool validPartition(vector<int>& nums) {\n n=nums.size();\n vector<int>dp(n+1,-1);\n // return recsol(nums,0,dp);\n return tab(nums);\n }\n};", "memory": "93800" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\n bool isValid(int index, vector<int>&nums,vector<int> &dp)\n {\n if(index>=nums.size())\n return false;\n bool p1=false,p3= false;\n int n =nums.size()-1;\n if(index == n-2)\n {\n if(nums[index]==nums[index+1] && nums[index+1]==nums[index+2])\n {\n\n return true;\n }\n if(nums[index] == nums[index+1]-1 && nums[index+1]==nums[index+2]-1)\n {\n return true;\n }\n return false;\n }\n if(index == n-1)\n {\n if(nums[index]==nums[index+1])\n return true;\n return false;\n }\n if(dp[index]!=-1)\n return dp[index];\n if(index<= n-2)\n {\n if(nums[index]==nums[index+1] && nums[index+1]==nums[index+2])\n {\n \n p1 = p1 | isValid(index+3,nums,dp);\n }\n if(nums[index] == nums[index+1]-1 && nums[index+1]==nums[index+2]-1)\n {\n \n\n p1 = p1 | isValid(index+3,nums,dp);\n }\n }\n if(index<=n-1)\n {\n if(nums[index]==nums[index+1])\n {\n \n p3 = p3 | isValid(index+2,nums,dp);\n }\n }\n\n return dp[index]=p1 | p3;\n }\npublic:\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n vector<int> dp(n,-1);\n return isValid(0,nums,dp);\n \n \n }\n};", "memory": "93900" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\n vector<optional<bool>> dp; \n bool validPartitionUtility(vector<int>& nums, vector<optional<bool>>& dp, int idx)\n {\n if(idx < 0)\n return true;\n\n if(dp[idx] != nullopt)\n return (dp[idx] == true);\n\n bool a = false;\n bool b = false;\n bool c = false;\n if ((idx > 0) && (nums[idx] == nums[idx-1]))\n a = validPartitionUtility(nums, dp, idx-2);\n\n if((idx > 1) && (nums[idx] == nums[idx-1]) && (nums[idx-1] == nums[idx-2]))\n b= validPartitionUtility(nums, dp, idx-3);\n\n if((idx > 1) && (nums[idx] == (nums[idx-1]+1)) && (nums[idx-1] == (nums[idx-2]+1)))\n c = validPartitionUtility(nums, dp, idx-3);\n \n bool result = a|b|c;\n \n\n dp[idx] = result;\n return result;\n }\n\npublic:\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n dp = vector<optional<bool>>(n, nullopt);\n return validPartitionUtility(nums,dp, n-1);\n }\n\n};", "memory": "94000" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool isValid(vector<int>& nums, vector<int> &vec, int pos){\n if(pos >= nums.size()){\n return 1;\n }else if(vec[pos] != -1){\n return vec[pos];\n }else if(nums.size()-pos < 2){\n return vec[pos] = 0;\n }else if(nums.size()-pos >= 3 and nums[pos] == nums[pos+1] and nums[pos+1] == nums[pos+2]){\n return vec[pos] = isValid(nums, vec, pos+2) or isValid(nums, vec, pos+3);\n }else if(nums[pos] == nums[pos+1]){\n return vec[pos] = isValid(nums, vec, pos+2);\n }else if(nums.size()-pos >= 3 and nums[pos]+1 == nums[pos+1] and nums[pos+1]+1 == nums[pos+2]){\n return vec[pos] = isValid(nums, vec, pos+3);\n }else{\n return vec[pos] = 0;\n }\n }\n bool validPartition(vector<int>& nums) {\n vector<int> vec(nums.size()+1, -1);\n return isValid(nums, vec, 0);\n }\n};", "memory": "94100" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int>dp;\n int n;\n bool memo(vector<int>&nums,int ind){\n if(ind == n)return dp[ind] = true;\n if(dp[ind] != -1)return dp[ind];\n\n bool two_same = false, three_same = false, three_consecutive = false;\n\n if(ind+1 < n && nums[ind] == nums[ind+1])\n two_same = memo(nums,ind+2);\n if(two_same)return dp[ind] = true;\n\n\n if((ind+1 < n) && (ind + 2 < n) && (nums[ind] == nums[ind+1]) && (nums[ind+1] == nums[ind+2]))\n three_same = memo(nums,ind+3);\n if(three_same)return dp[ind] = true;\n\n\n if((ind+1 < n) && (ind + 2 < n) && (nums[ind] + 1 == nums[ind+1]) && (nums[ind+1] + 1 == nums[ind+2]))\n three_consecutive = memo(nums,ind+3);\n if(three_consecutive)return dp[ind] = true;\n \n return dp[ind] = false;\n }\n bool validPartition(vector<int>& nums) {\n n = nums.size();\n dp.resize(n+1,-1);\n return memo(nums,0);\n }\n};", "memory": "94200" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\nbool recursion(int ind,vector<int> &nums,vector<int> &dp){\n\n if(ind>=nums.size()) return true;\n if(dp[ind] != -1) return dp[ind];\n\n bool ans1=false,ans2=false,ans3=false;\n\n if(ind+2 < nums.size()){\n\n if(nums[ind]==nums[ind+1] && nums[ind+1]==nums[ind+2]){\n ans1 = recursion(ind+3,nums,dp);\n }\n\n if(nums[ind+1]==nums[ind]+1 && nums[ind+2]==nums[ind+1]+1) {\n ans2 = recursion(ind+3,nums,dp);\n }\n }\n\n if(ind+1<nums.size()){\n\n if(nums[ind]==nums[ind+1]) ans3 = recursion(ind+2,nums,dp);\n\n }\n\n return dp[ind]= (ans1 || ans2) || ans3;\n\n}\n bool validPartition(vector<int>& nums) {\n \n vector<int> dp(nums.size(),-1);\n\n return recursion(0,nums,dp);\n }\n};", "memory": "94400" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int n;\n int dp[1000001];\n bool solve(vector<int>& nums, int i) {\n if (i >= n) {\n return true;\n }\n if(dp[i] != -1){\n return dp[i];\n }\n bool result = false;\n if (i < n - 1 && nums[i] == nums[i + 1]) {\n result = solve(nums, i + 2);\n if (result) {\n return dp[i] = true;\n }\n }\n if (i < n - 2 &&\n (nums[i] == nums[i + 1] && nums[i + 1] == nums[i + 2])) {\n result = solve(nums, i + 3);\n if (result) {\n return dp[i] = true;\n }\n }\n if (i < n - 2 &&\n (nums[i] == nums[i + 1] - 1 && nums[i + 1] == nums[i + 2] - 1)) {\n result = solve(nums, i + 3);\n if (result) {\n return dp[i] = true;\n }\n }\n return dp[i] = result;\n }\n\n bool validPartition(vector<int>& nums) {\n n = nums.size();\n memset(dp, -1, sizeof(dp));\n return solve(nums, 0);\n }\n};", "memory": "94700" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n \n bool solve(vector<int>& nums, int i, int n, vector<int> &dp){\n \n if(i == n){\n return true;\n }\n \n if(dp[i] != -1){\n return dp[i];\n }\n \n if(i + 1 < n and (nums[i] == nums[i+1])){\n if(solve(nums, i + 2, n, dp))\n return dp[i] = true;\n }\n \n if(i + 2 < n and ((nums[i] - nums[i+1] == -1 and nums[i+1] - nums[i+2] == -1) \n or (nums[i] == nums[i+1] and nums[i+1] == nums[i+2]))){\n if(solve(nums, i + 3, n, dp))\n return dp[i] = true;\n }\n \n \n return dp[i] = false;\n \n }\n \n bool validPartition(vector<int>& nums) {\n \n int n = nums.size();\n vector<int> dp(n + 1, -1);\n return solve(nums, 0, n, dp);\n }\n};", "memory": "94800" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\n int n;\n bool solve(int i,vector<int>&nums,vector<int>&dp){\n if(i>=n) return true;\n if(dp[i]!=-1) return dp[i];\n\n bool res=false;\n //Rule1:\n if(i+1<n && nums[i]==nums[i+1]){\n res=solve(i+2,nums,dp);\n if(res==true) return dp[i]= true;\n }\n\n //Rule 2:\n if(i+2<n && nums[i]==nums[i+1] && nums[i+1]==nums[i+2]){\n res=solve(i+3,nums,dp);\n if(res) return dp[i]= true;\n }\n\n //Rule 3:\n if(i+2<n && nums[i+1]-nums[i]==1 && nums[i+2]-nums[i+1]==1){\n res=solve(i+3,nums,dp);\n }\n\n return dp[i]= res;\n \n }\npublic:\n bool validPartition(vector<int>& nums) {\n n=nums.size();\n vector<int>dp(n+1,-1);\n return solve(0,nums,dp);\n }\n};", "memory": "94900" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n// [1,1,1,2,3,4,1]\n// 1 1 1 , 2 3 4 , 1 ->false -> 1 bach gaya\n bool check(int ind, vector<int>&nums, vector<int>&dp){\n // base case \n if(ind<0) return true; // empty array is always valid\n if(ind == 0) return false; // a single element is not valid;\n if(dp[ind] != -1) return dp[ind];\n\n bool result = false;\n\n // check for two\n if(ind>=1 && nums[ind] == nums[ind-1]){\n result = check(ind-2, nums, dp);\n }\n\n //check for three\n if(!result && ind>=2 && (nums[ind] == nums[ind-1]) && (nums[ind-1] == nums[ind-2])){\n result = check(ind-3, nums, dp);\n }\n\n //check for three consecutive\n if(!result && ind>=2 && (nums[ind] == nums[ind-1]+1) && (nums[ind-1] == nums[ind-2]+1)){\n result = check(ind-3, nums, dp);\n }\n\n return dp[ind] = result;\n }\n bool validPartition(vector<int>& nums) {\n // 2 equal\n // 3equal\n // 3 consecutive\n\n int n = nums.size();\n\n vector<int>dp(n,-1);\n\n return check(n-1, nums, dp);\n }\n};", "memory": "95000" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int n;\n vector<int> a;\n int memo[100005];\n\n bool solve(int index) {\n if (index == n) return true;\n\n if (memo[index] != -1) return memo[index];\n\n if (index + 1 < n && a[index] == a[index + 1]) {\n if (solve(index + 2)) return memo[index] = true;\n\n if (index + 2 < n && a[index] == a[index + 2]) {\n if (solve(index + 3)) return memo[index] = true;\n }\n }\n\n if (index + 2 < n && a[index] + 1 == a[index + 1] && a[index] + 2 == a[index + 2]) {\n if (solve(index + 3)) return memo[index] = true;\n }\n\n return memo[index] = false;\n }\n\n bool validPartition(vector<int>& arr) {\n this->a = arr;\n this->n = arr.size();\n\n memset(memo, -1, sizeof(memo));\n\n return solve(0);\n }\n};\n", "memory": "95300" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> arr;\n int dp[100100];\n bool solve(int idx){\n int n = arr.size();\n\n if(idx==n)return true;\n if(idx>n)return false;\n if(dp[idx]!=-1)return dp[idx];\n // 1st\n bool ans = false;\n bool dd = false;\n if(idx+1<n && arr[idx] == arr[idx+1]){\n ans= solve(idx+2);\n if(!ans && idx+2<n && arr[idx+1] == arr[idx+2] ){\ndd=true;\n ans = ans || solve(idx+3);\n }\n }\n if(!dd && !ans && idx+2<n && arr[idx] +1 == arr[idx+1] && arr[idx+1]+1 == arr[idx+2] ){\n ans = ans|| solve(idx+3);\n }\n // 2nd\n return dp[idx]=ans;\n \n }\n bool validPartition(vector<int>& nums) {\n arr= nums;\n memset(dp,-1,sizeof dp);\n return solve(0);\n }\n // i == i+1\n // 1. i+1 == i+2 \n\n};", "memory": "95400" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool rec(vector<int>& nums,int index,vector<int>& dp){\n //base case\n if(index >= nums.size() ) return 1;\n if(dp[index] != -1) return dp[index];\n\n int size=nums.size();\n int a=0;\n if(size-index >= 2 && (nums[index]==nums[index+1])){\n a=a|rec(nums,index+2,dp);\n }\n if(a==1) return dp[index]=1;\n if((size-index>=3) && (nums[index]==nums[index+1])&&(nums[index+1]==nums[index+2])){\n a=a|rec(nums,index+3,dp);\n }\n if(a==1) return dp[index]=1;\n if((size-index>=3) && (nums[index]+2==nums[index+1]+1)&&(nums[index+1]+1==nums[index+2])){\n a=a|rec(nums,index+3,dp);\n }\n return dp[index]=a;\n }\n\n bool validPartition(vector<int>& nums) {\n vector<int> dp(nums.size(),-1);\n return rec(nums,0,dp);\n }\n};", "memory": "95500" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n//using Front partition technique\n//recursion + memoization\n//TC-O(n), SC-O(n)\nint f(int ind, vector<int> &nums, vector<int> &dp)\n{\n int n=nums.size();\n if(ind==n) return true;//base case, means array has been traversed\n if(dp[ind]!=-1) return dp[ind];\n\n bool check1=false,check2=false,check3=false;\n //check 1- when remaining length is atleast 2\n if(ind<=n-2 && nums[ind]==nums[ind+1])\n check1=f(ind+2,nums,dp);\n\n //check 2 & 3\n if(ind<=n-3)//remaining length is at least 3\n {\n if(nums[ind]==nums[ind+1] && nums[ind+1]==nums[ind+2])\n check2=f(ind+3,nums,dp);\n\n if(nums[ind+1]-nums[ind]==1 && nums[ind+2]-nums[ind+1]==1)\n check3=f(ind+3,nums,dp);\n }\n\n return dp[ind]= check1 || check2 || check3;\n}\n bool validPartition(vector<int>& nums) {\n int n=nums.size();\n vector<int> dp(n,-1);\n return f(0,nums,dp);\n }\n};", "memory": "95600" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\nprivate:\n bool solve(int ind, int n, vector<int>&nums, vector<int>&dp){\n // base case\n if(ind==n){\n return true;\n }\n\n if(dp[ind]!=-1) return dp[ind];\n\n // nums[i] == nums[i+1]\n if(ind+1<n && nums[ind]==nums[ind+1]){\n bool res = solve(ind+2,n,nums,dp);\n if(res){\n return dp[ind] = res;\n }\n }\n\n // nums[i] == nums[i+1] == nums[i+2]\n if(ind+2<n && nums[ind]==nums[ind+1] && nums[ind+1]==nums[ind+2]){\n bool res = solve(ind+3,n,nums,dp);\n if(res){\n return dp[ind] = res;\n }\n }\n\n // nums[i+1] - nums[i] == nums[i+2] - nums[i+1] == 1\n if(ind+2<n && nums[ind+1]-nums[ind] == 1 && nums[ind+2] - nums[ind+1] == 1){\n bool res = solve(ind+3,n,nums,dp);\n if(res){\n return dp[ind] = res;\n }\n }\n\n return dp[ind] = false; \n }\npublic:\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n vector<int> dp(n,-1);\n return solve(0,n,nums,dp);\n }\n};", "memory": "95700" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\n vector<int> dp;\n bool check(vector<int> &nums, int ind){\n if(ind>=nums.size()) return true;\n if(dp[ind]!=-1) return dp[ind];\n bool result=false;\n if(ind+1<nums.size() && nums[ind+1]==nums[ind]){\n result=result|check(nums, ind+2);\n }\n if(ind+2<nums.size() && nums[ind]==nums[ind+1] && nums[ind+1]==nums[ind+2]){\n result=result|check(nums, ind+3);\n }\n if(ind+2<nums.size() && nums[ind+1]-nums[ind]==1 && nums[ind+2]-nums[ind+1]==1){\n result=result|check(nums, ind+3);\n }\n return dp[ind]=result;\n }\npublic:\n bool validPartition(vector<int>& nums) {\n dp.resize(nums.size(), -1);\n return check(nums, 0);\n }\n};", "memory": "95700" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int dp(vector<int>& nums, int n, int i, vector<int>& isValid){\n if(i>n) return 1;\n if(i==n) return 2;\n if(isValid[i]) return isValid[i];\n int ans = 1;\n if(n-i>=2 && nums[i]==nums[i+1]) ans = max(ans, dp(nums,n,i+2, isValid));\n if(n-i>2){\n if(nums[i]==nums[i+1] && nums[i]==nums[i+2])\n ans = max(ans, dp(nums,n,i+3, isValid));\n if(nums[i]==nums[i+1] -1 && nums[i]==nums[i+2]-2)\n ans = max(ans, dp(nums,n,i+3, isValid));\n }\n return isValid[i]=ans;\n }\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n vector<int> isValid(n, 0);\n return dp(nums,n,0, isValid)==2;\n }\n};", "memory": "95800" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> v;\n int dp[100005];\n int solve(int pos) {\n if(pos == v.size()) {\n return true;\n }\n if(dp[pos]!=-1) {\n return dp[pos];\n }\n if(pos +2 > v.size()) {\n dp[pos] = 0;\n return 0;\n } else {\n if(v[pos] == v[pos+1] && solve(pos+2)==1) {\n dp[pos] = 1;\n return 1;\n }\n }\n if(pos+3 > v.size()) {\n dp[pos] = 0;\n return 0;\n } else {\n if(v[pos] == v[pos+1] && v[pos] == v[pos+2] && solve(pos+3)==1) {\n dp[pos] = 1;\n return 1;\n } else if(v[pos] == v[pos+1]-1 && v[pos] == v[pos+2]-2 && solve(pos+3)==1) {\n dp[pos] = 1;\n return 1;\n }\n }\n dp[pos] = 0;\n return 0;\n\n }\n bool validPartition(vector<int>& nums) {\n memset(dp, -1, sizeof(dp));\n v = nums;\n return solve(0)==1;\n }\n};", "memory": "96200" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool helper(vector<int>& nums, int ind, vector<int>& dp) {\n if (ind == nums.size())\n return true;\n if (dp[ind] != -1)\n return dp[ind];\n if (ind + 1 < nums.size() && nums[ind] == nums[ind + 1]) \n {\n if (helper(nums, ind + 2, dp))\n return true;\n if (ind + 2 < nums.size() && nums[ind] == nums[ind + 2]) \n {\n if (helper(nums, ind + 3, dp))\n return true;\n }\n }\n if (ind + 2 < nums.size() && nums[ind] == nums[ind + 1] - 1 && nums[ind] == nums[ind + 2] - 2) \n {\n if (helper(nums, ind + 3, dp))\n return true;\n }\n return dp[ind] = false;\n }\n bool validPartition(vector<int>& nums) \n {\n vector<int> dp(nums.size(), -1);\n return helper(nums, 0, dp);\n }\n};", "memory": "96300" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool f(int i,vector<int> &nums,vector<int> &dp){\n\n if (i==nums.size()) return true;\n if (dp[i]!=-1) return dp[i];\n bool a=false,b=false,c=false;\n if (i+1<nums.size() && nums[i]==nums[i+1]) a=f(i+2,nums,dp);\n if (i+2<nums.size() && nums[i]==nums[i+1] && nums[i+1]==nums[i+2]) b=f(i+3,nums,dp);\n if (i+2<nums.size() && nums[i+1]==nums[i]+1 && nums[i+2]==nums[i+1]+1) c=f(i+3,nums,dp);\n\n return dp[i]= a || b || c;\n\n\n }\n bool validPartition(vector<int>& nums) {\n vector<int> dp(nums.size(),-1);\n return f(0,nums,dp);\n }\n};", "memory": "96400" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool isValidPartition(vector<int>& nums, vector<int>& dp, int i) {\n if(i >= nums.size()) return true;\n if(dp[i] != -1) return dp[i];\n\n if(i+1 < nums.size() && nums[i] == nums[i+1]){\n dp[i] = isValidPartition(nums, dp, i+2);\n if(dp[i]) return true;\n\n if(i+2 < nums.size() && nums[i] == nums[i+2]){\n dp[i] = isValidPartition(nums, dp, i+3);\n if(dp[i]) return true;\n }\n }\n\n if(i+2 < nums.size() && nums[i+1] - nums[i] == 1 && nums[i+2] - nums[i+1] == 1){\n return dp[i] = isValidPartition(nums, dp, i+3);\n }\n\n return false;\n }\n\n bool validPartition(vector<int>& nums) {\n vector<int> dp (nums.size(), -1);\n return isValidPartition(nums, dp, 0);\n }\n};", "memory": "96500" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums, vector<bool>& visited, int i)\n {\n if(i == nums.size())\n return true;\n\n if(visited[i])\n return false;\n\n visited[i] = true;\n bool valid = false;\n\n if(i < nums.size() - 2 && nums[i+1] == nums[i+2] && nums[i+1] == nums[i])\n valid = validPartition(nums, visited, i+3);\n \n if(valid)\n return true;\n \n if(i < nums.size() - 1 && nums[i+1] == nums[i])\n valid = validPartition(nums, visited, i+2);\n \n if(valid)\n return true;\n \n if(i < nums.size() - 2 && nums[i+1] -1 == nums[i+2] - 2 && nums[i+1]- 1 == nums[i])\n valid = validPartition(nums, visited, i+3);\n \n if(valid)\n return true;\n \n return false;\n }\n\n bool validPartition(vector<int>& nums) {\n vector<bool> visited(nums.size(), false);\n return validPartition(nums, visited, 0);\n }\n};", "memory": "96600" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\n \npublic:\n int dp[1000002];\n bool solve(vector<int>& nums,int i,int j){\n if(i==j){\n return true;\n }\n if(dp[i]!=-1){\n return dp[i];\n }\n bool res = false;\n if(i+1<j && nums[i]==nums[i+1]){\n res |= solve(nums,i+2,j);\n }\n if(i+2<j && nums[i+1]==nums[i+2] && nums[i]==nums[i+1]){\n res |= solve(nums,i+3,j);\n }\n if(i+2<j && nums[i+1]-nums[i]==1 && nums[i+2]-nums[i+1]==1){\n res |= solve(nums,i+3,j);\n }\n return dp[i] = res;\n }\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n cout<<n;\n memset(dp,-1,sizeof(dp));\n bool check = solve(nums,0,n);\n return check;\n }\n};", "memory": "97100" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int i,vector<int> &nums,int n,vector<int> &dp){\n if(i==n){\n return 1;\n }\n if(dp[i]!=-1){\n return dp[i];\n }\n int a=0,b=0,c=0,d=0;\n int val=nums[i];\n\n if(i+1<n && nums[i+1]==val){\n a=solve(i+2,nums,n,dp);\n }\n if(i+1<n && i+2<n && nums[i+1]==val && nums[i+2]==val){\n b=solve(i+3,nums,n,dp);\n \n }\n if(i+1<n && i+2<n && nums[i+1]==val+1 && nums[i+2]==val+2){\n c=solve(i+3,nums,n,dp);\n }\n\n if(a==1 || b==1 || c==1){\n return dp[i]=1;\n }\n\n return dp[i]=0;\n \n }\n bool validPartition(vector<int>& nums) {\n int n=nums.size();\n vector<int> dp(n+1,-1);\n int ans=solve(0,nums,n,dp);\n\n return ans;\n }\n};", "memory": "97200" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint N;\nvector<int>NUMS;\nbool naive_recurse(int i) {\n if (i == N)\n return true;\n if (i + 1 < N and NUMS[i] == NUMS[i + 1] and naive_recurse(i + 2))\n return true;\n if (i + 2 < N and NUMS[i] == NUMS[i + 1] == NUMS[i + 2] and\n naive_recurse(i + 3))\n return true;\n if (i + 2 < N and NUMS[i] == NUMS[i + 1] - 1 and\n NUMS[i + 1] == NUMS[i + 2] - 1 and naive_recurse(i + 3))\n return true;\n return false;\n}\n\nvector<int> cache;\nbool validPartition_topdown(int i) {\n if (i == N)\n return true;\n if (cache[i] == -1) {\n if (i + 1 < N and NUMS[i] == NUMS[i + 1] and validPartition_topdown(i + 2))\n cache[i] = 1;\n else if (i + 2 < N and NUMS[i] == NUMS[i + 1] and NUMS[i+1]== NUMS[i + 2] and\n validPartition_topdown(i + 3))\n cache[i] = 1;\n else if (i + 2 < N and NUMS[i] == NUMS[i + 1] - 1 and\n NUMS[i + 1] == NUMS[i + 2] - 1 and validPartition_topdown(i + 3))\n cache[i] = 1;\n else\n cache[i] = 0;\n }\n return cache[i];\n}\nbool validPartition(vector<int> &nums) {\n NUMS = nums;\n N = nums.size();\n cache = vector(N + 1, -1);\n return validPartition_topdown(0);\n}\n\n\n};", "memory": "98500" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n vector<int> nums;\n vector<int> dp;\n bool solve(int index) {\n if (index == nums.size()) return true;\n\n if (dp[index] != -1) return dp[index];\n bool ans = false;\n\n if (index+1 < nums.size() && nums[index] == nums[index+1]) {\n ans = ans | solve(index+2);\n }\n\n if ( index+2 < nums.size() && (nums[index] == nums[index+1]) && (nums[index+2] == nums[index+1])) {\n ans = ans | solve(index+3);\n }\n\n if (index+2 < nums.size() && (nums[index+1] - nums[index] == 1) && (nums[index+2] - nums[index+1] == 1)) {\n ans = ans | solve(index+3);\n }\n\n return dp[index] = ans;\n }\n bool validPartition(vector<int>& nums) {\n this->nums = nums;\n dp.resize(nums.size(),-1);\n return solve(0);\n }\n};", "memory": "99400" }
2,443
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p> <p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p> <ol> <li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li> <li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li> </ol> <p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,4,4,5,6] <strong>Output:</strong> true <strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,2] <strong>Output:</strong> false <strong>Explanation:</strong> There is no valid partition for this array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n vector<int> dp;\n\n bool validPartition(vector<int>& nums) {\n for(int i = 0;i<nums.size();i++) dp.push_back(-1);\n return find(nums,0);\n }\n\n bool find(vector<int>& nums,int index){\n int n = nums.size();\n if(index==n) return true;\n if(dp[index]!=-1) return dp[index];\n int ans = false;\n if(index<=n-2){\n if(nums[index]==nums[index+1]) ans = ans || find(nums,index+2);\n }\n if(index<=n-3){\n if(nums[index]==nums[index+1] && nums[index]==nums[index+2]) ans = ans || find(nums,index+3);\n if(nums[index]+1==nums[index+1] && nums[index]+2==nums[index+2]) ans = ans || find(nums,index+3);\n }\n return dp[index] = ans;\n }\n\n};", "memory": "100400" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\nclass Solution {\npublic:\n int longestIdealString(string& s, int k) {\n int seq[26]={0};// max len of seq ending at char(i+'a')\n int ans=0;\n for(char c: s){\n int i=c-'a';\n int j0=max(0, i-k), j1=min(i+k, 25);\n for(int j=j0; j<=j1; j++)\n seq[i]=max(seq[i], seq[j]);//longest seq ending at char(j+'a')\n seq[i]++;//Put c to the tail\n }\n return *max_element(seq, seq+26); \n }\n};\n", "memory": "12911" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\nclass Solution {\npublic:\n int longestIdealString(string& s, int k) {\n int seq[26]={0};// max len of seq ending at char(i+'a')\n int ans=0;\n for(char c: s){\n int i=c-'a';\n int j0=max(0, i-k), j1=min(i+k, 25);\n for(int j=j0; j<=j1; j++)\n seq[i]=max(seq[i], seq[j]);//longest seq ending at char(j+'a')\n seq[i]++;//Put c to the tail\n }\n return *max_element(seq, seq+26); \n }\n};", "memory": "12911" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n/*\ndp[n] length of the longest ideal string ending at index k\ndp[n] = dp[l] + 1, where l is the nearest index to n such that diff(t[l], t[n]) <= k\ndp[0] = 1;\n e d u k t d b k=2\ni 0 1 2 3 4 5 6\ndp 1 2 1 2 3 4 \n */\n int longestIdealString(string s, int k) {\n vector<int> dp(s.size(), 1);\n\n int max_len = 1;\n for (size_t i = 1; i < s.size(); i++) {\n bool left = true;\n bool right = true;\n for (int j = static_cast<int>(i) - 1; j >=0 && j >= (dp[i] - 1) && (left|| right); j--) {\n if (abs(s[j] - s[i]) <= k) {\n dp[i] = max(dp[i], dp[j] + 1);\n if (s[j] <= s[i])\n left = false;\n if (s[j] >= s[i])\n right = false;\n }\n\n }\n max_len = max(max_len, dp[i]);\n }\n\n return max_len;\n \n }\n};", "memory": "17335" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int longestIdealString(string s, int k) {\n int n = s.size();\n vector<int> longest(26);\n vector<int> dp(n);\n\n for (int i = 0; i < n; ++i) {\n int curr = s[i] - 'a';\n int max_len = 0;\n\n for (int j = max(0, curr - k); j <= min(25, curr + k); ++j) {\n max_len = max(max_len, longest[j]);\n }\n\n dp[i] = max_len + 1;\n longest[curr] = max(longest[curr], dp[i]);\n }\n\n return *max_element(longest.begin(), longest.end());\n }\n};", "memory": "17335" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int longestIdealString(string s, int k) {\n\n long long n=s.size();\n vector<long long> dp(n+1,0);\n \n map<long long,long long> mp;\n\n long long ans=-1;\n for(int i=1;i<=n;i++){\n for(int j=(-k);j<=k;j++){\n if(mp[abs((s[i-1]-'a')-j)]>0){\n // cout<<\"in=\"<<mp[abs((s[i-1]-'a')-k)]<<endl;\n // cout<<i<<endl;\n dp[i]=max(dp[i],(dp[mp[abs((s[i-1]-'a')-j)]]+1));\n }\n }\n mp[(s[i-1]-'a')]=i;\n ans=max(ans,dp[i]);\n }\n // for(auto x:mp){\n // cout<<x.first<<\" \"<<x.second<<endl;\n // }\n // for(auto x:dp){\n // cout<<x<<\" \";\n // }\n // cout<<endl;\n return (ans+1);\n\n\n\n\n\n\n\n }\n};", "memory": "21759" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int longestIdealString(string s, int k) {\n\n long long n=s.size();\n vector<long long> dp(n+1,0);\n \n map<long long,long long> mp;\n\n long long ans=-1;\n for(int i=1;i<=n;i++){\n for(int j=(-k);j<=k;j++){\n if(mp[abs((s[i-1]-'a')-j)]>0){\n \n dp[i]=max(dp[i],(dp[mp[abs((s[i-1]-'a')-j)]]+1));\n }\n }\n mp[(s[i-1]-'a')]=i;\n ans=max(ans,dp[i]);\n }\n \n return (ans+1);\n\n\n\n\n\n\n\n }\n};", "memory": "21759" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "// class Solution{\n// public:\n// vector<vector<int>>dp;\n// int helper(string& s,int ind,int k,int last){\n// if(ind>=s.length())return 0;\n// int l=0,r=0;\n// if(last!=-1 && dp[ind][last]!=-1)return dp[ind][last];\n// l=helper(s,ind+1,k,last);\n// if(last==-1 || abs(last-(s[ind]-'a'))<=k){\n// r=1+helper(s,ind+1,k,s[ind]-'a');\n// }\n// if(last!=-1)dp[ind][last]=max(l,r);\n// return max(l,r);\n// }\n// int longestIdealString(string& s, int k) {\n// dp.resize(s.length()+1,vector<int>(26,-1));\n// return helper(s,0,k,-1);\n// }\n// };\nclass Solution {\npublic:\n int dp[100001][26]; // Assuming the maximum length of string s is 1000\n\n int helper(string& s, int ind, int k, int last) {\n if (ind >= s.length()) return 0;\n int l = 0, r = 0;\n if (last != -1 && dp[ind][last] != -1) return dp[ind][last];\n \n l = helper(s, ind + 1, k, last);\n \n if (last == -1 || abs(last - (s[ind] - 'a')) <= k) {\n r = 1 + helper(s, ind + 1, k, s[ind] - 'a');\n }\n \n if (last != -1) dp[ind][last] = max(l, r);\n \n return max(l, r);\n }\n\n int longestIdealString(string& s, int k) {\n memset(dp, -1, sizeof(dp)); // Initialize the dp array with -1\n return helper(s, 0, k, -1);\n }\n};\n\nclass Solution2 {\npublic:\n int longestIdealString(string& s, int k) {\n vector<int> dp(26, 0); // dp array to store the max length of ideal string ending with each character\n for (char c : s) {\n int idx = c - 'a';\n int max_len = 0;\n // Check the range of indices that differ by at most k\n for (int i = max(0, idx - k); i <= min(25, idx + k); ++i) {\n max_len = max(max_len, dp[i]);\n }\n dp[idx] = max_len + 1;\n }\n return *max_element(dp.begin(), dp.end());\n }\n};\n", "memory": "26183" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int dp[100005][26];\n int solve(string &s, int &k, int i = 0, int prev = -1) {\n if(i >= size(s)) return 0;\n if(prev >= 0 && dp[i][prev] != -1) return dp[i][prev];\n\n int op1 = solve(s, k, i+1, prev), op2 = 0;\n if(prev == -1 || abs(s[i]-'a'-prev) <= k) {\n op2 = 1 + solve(s, k, i+1, s[i]-'a');\n }\n if(prev >= 0) dp[i][prev] = max(op1, op2);\n\n return max(op1, op2);\n }\n int longestIdealString(string s, int k) {\n memset(dp, -1, sizeof(dp));\n cout<<s.size();\n return solve(s, k);\n }\n};", "memory": "30606" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:int dp[100000][27];\nint solve(int i,string &s,int &k,int prev)\n{\n if(i==s.size())\n {\n return 0;\n }\n if(dp[i][prev+1]!=-1)\n return dp[i][prev+1];\n\n int u=s[i]-'a'; int take=0;\n if(prev==-1 || abs(u-prev)<=k )\n {\n take=1+solve(i+1,s,k,u);\n }\n int ntake=solve(i+1,s,k,prev);\n return dp[i][prev+1]= max(take,ntake);\n\n\n\n\n}\n int longestIdealString(string s, int k) {\n memset(dp,-1,sizeof(dp));\n\n return solve(0,s,k,-1);\n}};", "memory": "30606" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\nint dp[100001][28];\n int longestIdealString(string s, int k) {\n memset(dp, -1, sizeof(dp));\n return solve(s, k, '{', 0);\n }\n\n int solve(string& s, int& k, char prev, int index){\n if(index >= s.length())return 0;\n if(dp[index][prev-'a'] != -1)return dp[index][prev-'a'];\n int ans=0;\n ans = max(ans, solve(s, k, prev, index+1));\n if(prev == '{' || abs(prev - s[index]) <= k){\n ans = max(ans, 1+solve(s, k, s[index], index+1));\n }\n return dp[index][prev-'a'] = ans;\n }\n};", "memory": "35030" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string s;\n int k;\n vector<int> cache;\n\n int longestIdealString(string s, int k) {\n this->s = s;\n this->k = k;\n \n cache = vector<int>(s.size(), -1e9);\n int ans = 0;\n for (int i = 0; i < s.size(); i++) {\n if (cache[i] == -1e9) \n cache[i] = solve(i, cache);\n ans = max(ans, cache[i]);\n }\n\n return ans;\n }\n\n int solve(int idx, vector<int>& cache) {\n if (idx == 0) return 1;\n \n int ans = 1;\n int count = 0;\n int count2 = 0;\n vector<bool> mp(26, false);\n for (int i = idx - 1; i >= 0; i--) {\n if (mp[(int)(s[i] - 'a')] == false) {\n count++;\n mp[(int)(s[i] - 'a')] = true;\n if (abs(s[i] - s[idx]) <= k) {\n count2++;\n if (cache[i] == -1e9) cache[i] = solve(i, cache);\n ans = max(ans, 1 + cache[i]);\n }\n }\n\n if (count >= 26) break;\n if (count2 > k) break;\n }\n\n return ans;\n }\n};", "memory": "35030" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string s;\n int k;\n vector<int> cache;\n\n int longestIdealString(string s, int k) {\n this->s = s;\n this->k = k;\n \n cache = vector<int>(s.size(), -1e9);\n int ans = 0;\n for (int i = 0; i < s.size(); i++) {\n if (cache[i] == -1e9) \n cache[i] = solve(i, cache);\n ans = max(ans, cache[i]);\n }\n\n return ans;\n }\n\n int solve(int idx, vector<int>& cache) {\n if (idx == 0) return 1;\n \n int ans = 1;\n int count = 0;\n int count2 = 0;\n vector<bool> mp(26, false);\n for (int i = idx - 1; i >= 0; i--) {\n if (mp[(int)(s[i] - 'a')] == false) {\n count++;\n mp[(int)(s[i] - 'a')] = true;\n if (abs(s[i] - s[idx]) <= k) {\n count2++;\n if (cache[i] == -1e9) cache[i] = solve(i, cache);\n ans = max(ans, 1 + cache[i]);\n }\n }\n\n if (count > 25) break;\n if (count2 > k) break;\n }\n\n return ans;\n }\n};", "memory": "39454" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string s;\n int k;\n vector<int> cache;\n\n int longestIdealString(string s, int k) {\n this->s = s;\n this->k = k;\n \n cache = vector<int>(s.size(), -1e9);\n int ans = 0;\n for (int i = 0; i < s.size(); i++) {\n if (cache[i] == -1e9) \n cache[i] = solve(i, cache);\n ans = max(ans, cache[i]);\n }\n\n return ans;\n }\n\n int solve(int idx, vector<int>& cache) {\n if (idx == 0) return 1;\n\n if (cache[idx] != -1e9) return cache[idx];\n \n int ans = 1;\n int count = 0;\n int count2 = 0;\n vector<bool> mp(26, false);\n for (int i = idx - 1; i >= 0; i--) {\n if (mp[(int)(s[i] - 'a')] == false) {\n count++;\n mp[(int)(s[i] - 'a')] = true;\n if (abs(s[i] - s[idx]) <= k) {\n count2++;\n if (cache[i] == -1e9) cache[i] = solve(i, cache);\n ans = max(ans, 1 + cache[i]);\n }\n }\n\n if (count > 25) break;\n if (count2 > k) break;\n }\n\n return ans;\n }\n};", "memory": "39454" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string s;\n int k;\n vector<int> cache;\n\n int longestIdealString(string s, int k) {\n this->s = s;\n this->k = k;\n \n cache = vector<int>(s.size(), -1e9);\n int ans = 0;\n for (int i = 0; i < s.size(); i++) {\n if (cache[i] == -1e9) \n cache[i] = solve(i, cache);\n ans = max(ans, cache[i]);\n }\n\n return ans;\n }\n\n int solve(int idx, vector<int>& cache) {\n if (idx == 0) return 1;\n \n int ans = 1;\n int count = 0;\n int count2 = 0;\n vector<bool> mp(26, false);\n for (int i = idx - 1; i >= 0; i--) {\n if (mp[(int)(s[i] - 'a')] == false) {\n count++;\n mp[(int)(s[i] - 'a')] = true;\n if (abs(s[i] - s[idx]) <= k) {\n count2++;\n if (cache[i] == -1e9) cache[i] = solve(i, cache);\n ans = max(ans, 1 + cache[i]);\n }\n }\n\n if (count > 25) break;\n if (count2 > k) break;\n }\n\n return ans;\n }\n};", "memory": "43878" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string s;\n int k;\n vector<int> cache;\n\n int longestIdealString(string s, int k) {\n this->s = s;\n this->k = k;\n \n cache = vector<int>(s.size(), -1e9);\n int ans = 0;\n for (int i = 0; i < s.size(); i++) {\n if (cache[i] == -1e9) \n cache[i] = solve(i, cache);\n ans = max(ans, cache[i]);\n }\n\n return ans;\n }\n\n int solve(int idx, vector<int>& cache) {\n if (idx == 0) return 1;\n \n int ans = 1;\n int count = 0;\n int count2 = 0;\n vector<bool> mp(26, false);\n for (int i = idx - 1; i >= 0; i--) {\n if (count > 25) break;\n if (count2 > k) break;\n \n if (mp[(int)(s[i] - 'a')] == false) {\n count++;\n mp[(int)(s[i] - 'a')] = true;\n if (abs(s[i] - s[idx]) <= k) {\n count2++;\n if (cache[i] == -1e9) cache[i] = solve(i, cache);\n ans = max(ans, 1 + cache[i]);\n }\n }\n }\n\n return ans;\n }\n};", "memory": "43878" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "int fn (int node , vector<int>&dp, vector<vector<int> > &m, int k , string &s )\n{\n if (node>=s.size()) return 0;\n\n if (dp[node]!=-1) return dp[node];\n int res = 0;\n\n for (int i = 0; i<=k; i++){\n if (s[node] + i <= 'z'){\n int ch = s[node] + i - 'a';\n int idx = upper_bound(m[ch].begin() , m[ch].end() , node ) - m[ch].begin();\n\n if (idx<m[ch].size()) {\n res = max(res , fn(m[ch][idx] , dp , m , k , s));\n }\n }\n if (s[node] - i >= 'a'){\n int ch = s[node] - i - 'a';\n int idx = upper_bound(m[ch].begin() , m[ch].end() , node ) - m[ch].begin();\n\n if (idx<m[ch].size()) {\n res = max(res , fn(m[ch][idx] , dp , m , k , s));\n }\n }\n }\n dp[node] = 1 + res;\n return (1 + res);\n}\n\nclass Solution {\npublic:\n int longestIdealString(string &s, int k) {\n int n = s.size();\n vector < int > dp(n, -1);\n\n vector < vector<int> > m(26);\n for (int i = 0; i<n ; i++){\n m[s[i]-'a'].push_back(i);\n }\n int res = 0;\n for (int i = 0; i<n; i++){\n if(dp[i]==-1)\n res = max(res, fn(i , dp , m , k , s));\n }\n\n return res;\n }\n};", "memory": "48301" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "int fn (int node , vector<int>&dp, unordered_map<char , vector<int> > &m, int k , string &s )\n{\n if (node>=s.size()) return 0;\n\n if (dp[node]!=-1) return dp[node];\n int res = 0;\n\n for (int i = 0; i<=k; i++){\n if (s[node] + i <= 'z'){\n char ch = s[node] + i;\n int idx = upper_bound(m[ch].begin() , m[ch].end() , node ) - m[ch].begin();\n\n if (idx<m[ch].size()) {\n res = max(res , fn(m[ch][idx] , dp , m , k , s));\n }\n }\n if (s[node] - i >= 'a'){\n char ch = s[node] - i;\n int idx = upper_bound(m[ch].begin() , m[ch].end() , node ) - m[ch].begin();\n\n if (idx<m[ch].size()) {\n res = max(res , fn(m[ch][idx] , dp , m , k , s));\n }\n }\n }\n dp[node] = 1 + res;\n return (1 + res);\n}\n\nclass Solution {\npublic:\n int longestIdealString(string &s, int k) {\n int n = s.size();\n vector < int > dp(n, -1);\n\n unordered_map < char , vector<int> > m;\n for (int i = 0; i<n ; i++){\n m[s[i]].push_back(i);\n }\n int res = 0;\n for (int i = 0; i<n; i++){\n if(dp[i]==-1)\n res = max(res, fn(i , dp , m , k , s));\n }\n\n return res;\n }\n};", "memory": "52725" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int longestIdealString(string s, int k) {\n unordered_map<char, int> cm;\n unordered_map<int, int> m;\n int res = 0;\n for(int i = 0; i<s.size(); i++){\n char c = s[i];\n m[i] = 1;\n for(char j = 'a'; j<='z'; j++){\n if(abs(j-c)>k) continue;\n if(cm.find(j)!=cm.end()){\n m[i] = max(m[i], cm[j]+1);\n }\n }\n cm[c] = m[i];\n res = max(res, m[i]);\n }\n return res;\n }\n};", "memory": "57149" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int N,K;\n int dp[100001][123];\n int solve(string &s, int i, char last_char){\n\n if(i>=N) return 0;\n if(dp[i][last_char] != -1) return dp[i][last_char];\n\n int take=0,skip=0;\n\n if(last_char=='0' || abs(last_char-s[i])<=K) {\n take=1+solve(s,i+1,s[i]);\n }\n \n skip=solve(s,i+1,last_char);\n\n return dp[i][last_char]=max(take,skip);\n }\n\n int longestIdealString(string s, int k) {\n N=s.length();\n K=k;\n // chat last=''\n memset(dp,-1,sizeof(dp));\n return solve(s,0,'0');\n }\n};", "memory": "61573" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n int dp[100005][150];\n\n int find(int index,string &s,int &k,char prevChar){\n \n if(index == s.size()) return 0;\n\n if(dp[index][prevChar] != -1) return dp[index][prevChar];\n\n int option1 = 0;\n int option2 = 0;\n\n if(prevChar == '#' || abs((s[index]-'a') - (prevChar-'a')) <=k ){\n option1 = 1 + find(index+1,s,k,s[index]);\n } \n\n option2 = max(option2,find(index+1,s,k,prevChar));\n\n \n return dp[index][prevChar] = max(option1 , option2);\n\n }\n\n\n int longestIdealString(string s, int k) {\n\n memset(dp,-1,sizeof(dp));\n\n return find(0,s,k,'#');\n \n }\n};", "memory": "65996" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int longestIdealString(string s, int k) {\n vector<int>dp(26, 0);\n int i, n;\n n = s.size();\n int j;\n for(i=0;i<n;i++){\n vector<int>next = dp;\n for(j=0;j<26;j++){\n if(abs(j-(s[i]-'a'))>k)continue;\n next[s[i]-'a'] = max(dp[j] + 1, next[s[i]-'a']);\n }\n dp = next;\n }\n int ans = 0;\n for(auto it:dp)ans = max(it, ans);\n return ans;\n }\n};", "memory": "92539" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int longestIdealString(string s, int k) {\n int n=s.size();\n int i,j;\n vector<int> dp(26,0);\n for(i=0;i<n;i++) {\n vector<int> temp=dp;\n for(j=0;j<=25;j++) {\n if(abs(s[i]-j-'a')<=k) {\n temp[s[i]-'a']=max(temp[s[i]-'a'], dp[j]+1);\n }\n }\n dp=temp;\n }\n int ans=0;\n for(i=0;i<26;i++)\n {\n cout<<dp[i]<<endl;\n ans=max(ans,dp[i]);\n\n }\n return ans;\n }\n};", "memory": "96963" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n// int solve(int i, string s, int k, int prev, vector<vector<int>>&mem){\n// if(i>=s.length()){\n// return mem[i][prev+1]=0;\n// }\n// if(mem[i][prev+1] != -1){\n// return mem[i][prev+1];\n// }\n// int ans = -1e8;\n// if(prev ==-1 || abs(s[i]-s[prev])<=k){\n// ans = max(ans,1+ solve(i+1, s, k, i, mem));\n// }\n// int a = solve(i+1, s, k, prev, mem);\n// return mem[i][prev+1]= max(ans, a);\n// }\nint solve2(string s, int k){\n int n = s.length();\n //vector<vector<int>>dp(n+1, vector<int>(n+1, 0));\n vector<int>prev(27, 0);\n // for(int i=0; i<=n; i++){\n // dp[n][i]=0;\n // }\n for(int i=n-1; i>=0; i--){\n vector<int>curr(27, 0);\n for(int j=0; j<27; j++){\n int ans = INT_MIN;\n if(j-1==-1 || abs(s[i]-('a'+j-1))<=k){\n ans = max(ans, 1+prev[(s[i]-'a') +1 ]);\n }\n ans = max(ans, prev[j]);\n curr[j]= ans;\n }\n prev = curr;\n }\n return prev[0];\n}\n int longestIdealString(string s, int k) {\n int n = s.length();\n // vector<vector<int>>mem(n+1, vector<int>(n+1, -1));\n //return solve(0, s, k, -1, mem);\n return solve2(s, k);\n }\n};", "memory": "101386" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int longestIdealString(string_view s, int k) {\n vector<vector<int>> dp(s.size()+1, vector(26, 0));\n\n for (int i = 1; i <= s.size(); ++i) {\n for (int j = 0; j < 26; ++j) {\n if (abs(s[i-1] - (j+'a')) > k) dp[i][j] = dp[i-1][j];\n else dp[i][j] = max(dp[i-1][j], 1 + dp[i-1][s[i-1] - 'a']);\n }\n }\n\n return ranges::max(dp.back());\n }\n};", "memory": "105810" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int longestIdealString(string_view s, int k) {\n vector<vector<int>> dp(s.size()+1, vector(26, 0));\n\n for (int i = 1; i <= s.size(); ++i) {\n for (int j = 0; j < 26; ++j) {\n if (abs(s[i-1] - (j+'a')) > k) dp[i][j] = dp[i-1][j];\n else dp[i][j] = max(dp[i-1][j], 1 + dp[i-1][s[i-1] - 'a']);\n }\n }\n\n return ranges::max(dp.back());\n }\n};", "memory": "110234" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int longestIdealString(string s, int k) {\n int n = s.length();\n vector<vector<int>> dp(n + 1, vector<int>(27, 0));\n\n for (int idx = 1; idx <= n; ++idx) {\n int curCharIndex = s[idx - 1] - 'a';\n\n for (char prev = 'a'; prev <= 'z'; ++prev) {\n int prevIdx = prev - 'a';\n\n // Option 1: Do not take the current character\n dp[idx][prevIdx] = max(dp[idx][prevIdx], dp[idx - 1][prevIdx]);\n\n // Option 2: Take the current character if the condition is met\n if (abs(curCharIndex - prevIdx) <= k) {\n dp[idx][curCharIndex] = max(dp[idx][curCharIndex], 1 + dp[idx - 1][prevIdx]);\n }\n }\n\n // Handle the case where no previous character is taken\n dp[idx][curCharIndex] = max(dp[idx][curCharIndex], 1 + dp[idx - 1][26]);\n\n // Update the state where no character is taken before this character\n dp[idx][26] = max(dp[idx][26], dp[idx - 1][26]);\n }\n\n // Find the maximum value in the last row of the dp array\n int maxLength = 0;\n for (int j = 0; j < 27; ++j) {\n maxLength = max(maxLength, dp[n][j]);\n }\n\n return maxLength;\n }\n};\n", "memory": "114658" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int solve(string &s,int &k,char prev_ch,int curr,vector<vector<int>>&dp){\n\n if(curr>=s.size()){\n\n return 0;\n }\n\n if(dp[prev_ch-'a'][curr]!=-1){\n\n return dp[prev_ch-'a'][curr];\n }\n int include=0;\n if(prev_ch=='{' || abs(prev_ch-s[curr])<=k){\n\n include=1+solve(s,k,s[curr],curr+1,dp);\n }\n int exclude=solve(s,k,prev_ch,curr+1,dp);\n\n dp[prev_ch-'a'][curr]=max(include,exclude);\n return dp[prev_ch-'a'][curr];\n }\n int solve2(string &s,int &k){\n\n vector<vector<int>>dp(s.size()+1,vector<int>(27,0));\n\n for(int curr=s.size()-1 ; curr>=0 ; curr--){\n\n for(char prev_ch='z' ; prev_ch>='a'-1 ; prev_ch--){\n\n int include=0;\n if(prev_ch=='a'-1 || abs(prev_ch-s[curr])<=k){\n\n include=1+dp[curr+1][s[curr]-'a'+1];\n }\n int exclude=dp[curr+1][prev_ch-'a'+1];\n dp[curr][prev_ch-'a'+1]=max(include,exclude);\n }\n }\n\n return dp[0][0];\n }\n int solve3(string &s,int &k){\n\n vector<int>prev1(s.size()+2,0);\n vector<int>prev2(s.size()+2,0);\n\n for(int curr=s.size()-1 ; curr>=0 ; curr--){\n\n for(int prev=curr-1 ; prev>=-1 ; prev--){\n\n int include=0;\n if(prev==-1 || abs(s[prev]-s[curr])<=k){\n\n include=1+prev1[curr+1];\n }\n int exclude=prev1[prev+1];\n\n prev2[prev+1]=max(include,exclude);\n }\n prev1=prev2;\n }\n\n return prev1[0]; \n }\n int longestIdealString(string s, int k) {\n \n // vector<vector<int>>dp(27,vector<int>(s.size(),-1));//bole to 27\n\n // return solve(s,k,'z'+1,0,dp);\n\n return solve2(s,k);\n // return solve3(s,k);\n }\n};", "memory": "119081" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n // int f(int i, string& s, char prev, int& k, vector<vector<int>>& dp) {\n // if (i < 0) return 0;\n\n // int prevIdx = (prev == '{') ? 0 : (prev - 'a' + 1);\n // if (dp[i][prevIdx] != -1) return dp[i][prevIdx];\n\n // int take = 0, nottake = 0;\n // if (prev == '{') {\n // take = 1 + f(i - 1, s, s[i], k, dp);\n // nottake = 0 + f(i - 1, s, prev, k, dp);\n // } else {\n // if (abs(s[i] - prev) <= k) {\n // take = 1 + f(i - 1, s, s[i], k, dp);\n // }\n // nottake = 0 + f(i - 1, s, prev, k, dp);\n // }\n // return dp[i][prevIdx] = max(take, nottake);\n // }\n\n // int longestIdealString(string s, int k) {\n // int n = s.length();\n // vector<vector<int>> dp(n, vector<int>(27, -1)); \n // return f(n - 1, s, '{', k, dp);\n // }\n int longestIdealString(string s, int k) {\n int n = s.length();\n vector<vector<int>> dp(n+1,vector<int>(27,0)); \n\n\n //Tabulation\n for(int idx=n-1; idx>=0; idx--){\n for(int prev=0; prev<=26; prev++){\n int take=0,nottake=0;\n nottake = dp[idx+1][prev];\n if(prev == 0 || abs(s[idx]-'a'+1 - prev) <= k){\n take = 1+ dp[idx+1][s[idx]-'a'+ 1];\n }\n dp[idx][prev] = max(take , nottake);\n }\n }\n return dp[0][0];\n\n }\n};\n", "memory": "123505" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n // int solve(int i, char prev , string s, int k, vector<vector<int> > &dp)\n // {\n // if(i==s.size())\n // {\n // return 0;\n // }\n\n // if(dp[i][prev-'a']!=-1) return dp[i][prev-'a'];\n\n // int nontake = solve(i+1,prev,s,k,dp);\n // int take = 0;\n // if(prev == 'a' + 26 or abs(prev-s[i])<=k)\n // {\n // take = 1 + solve(i+1,s[i],s,k,dp);\n // }\n\n // return dp[i][prev-'a'] = max(take,nontake);\n // }\n\n\n\n int longestIdealString(string s, int k) {\n int n = s.size();\n vector<vector<int> > dp(n+1,vector<int>(27,0));\n vector<int> nex(27,0);\n vector<int> curr(27,0);\n for(int i = n-1;i>=0;i--)\n {\n for(char j = 'a' ; j<='a'+26 ; j++)\n {\n int nontake = dp[i+1][j-'a'];\n int take = 0;\n if(j == 'a' + 26 or abs(j-s[i])<=k)\n {\n take = 1 + dp[i+1][s[i]-'a'];\n }\n\n dp[i][j-'a'] = max(take,nontake);\n }\n }\n\n int ans = dp[0][26];\n return ans;\n }\n};", "memory": "127929" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\n\n int func(int prev, int i , string s, int k, int n )\n {\n if(i>=n){\n return 0;\n }\n int l1= func(prev,i+1,s,k,n);\n int l2=0;\n if(prev==0|| abs(int(s[i]-96-prev)) <=k)\n {\n l2= 1+func(int(s[i])-96,i+1,s,k,n);\n }\n return max(l1,l2);\n }\npublic:\n int longestIdealString(string s, int k) {\n int n= s.length();\n // return func(0,0,s,k,n);\n\n vector<vector<int>> dp(n+1, vector<int> (27,0));\n int maxno= INT_MIN;\n\n for(int i=n-1;i>=0;i--)\n {\n for(int prev=26;prev>=0;prev--)\n {\n int l1= dp[i+1][prev];\n int l2=0;\n if(prev==0|| abs(int(s[i]-96-prev)) <=k)\n {\n l2= 1+dp[i+1][int(s[i])-96];\n }\n dp[i][prev]= max(l1,l2);\n maxno= max(maxno, dp[i][prev]);\n }\n }\n return maxno;\n \n }\n};", "memory": "132353" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int longestIdealStringRec(string &s,int k,int i,int prev){\n if(i>=s.size()){\n return 0;\n }\n int include = 0;\n int exclude = 0;\n if(prev==-1 or abs(s[i]-s[prev])<=k){\n include = 1 + longestIdealStringRec(s,k,i+1,i);\n }\n exclude = longestIdealStringRec(s,k,i+1,prev);\n return max(include,exclude);\n }\n int longestIdealStringMem(string &s,int k,int i,int prev,vector<vector<int>>&dp){\n if(i>=s.size()){\n return 0;\n }\n if(dp[i][prev+1]!=-1){\n return dp[i][prev+1];\n }\n int include = 0;\n int exclude = 0;\n if(prev==-1 or abs(s[i]-(prev+'a'))<=k){\n include = 1 + longestIdealStringMem(s,k,i+1,s[i]-'a',dp);\n }\n exclude = longestIdealStringMem(s,k,i+1,prev,dp);\n dp[i][prev+1] = max(include,exclude);\n return dp[i][prev+1];\n }\n int longestIdealStringTab(string &s,int k){\n int n = s.size();\n vector<vector<int>>dp(n+1,vector<int>(28,0));\n for(int i = s.size()-1;i>=0;i--){\n for(int prev=25;prev>=-1;prev--){\n int include = 0;\n int exclude = 0;\n if(prev==-1 or abs(s[i]-(prev+'a'))<=k){\n include = 1 + dp[i+1][s[i]-'a'+1];\n }\n exclude = dp[i+1][prev+1];\n dp[i][prev+1] = max(include,exclude);\n }\n }\n return dp[0][0];\n }\n\n\n int longestIdealString(string s, int k) {\n // return longestIdealStringRec(s,k,0,-1);\n int n = s.size();\n // vector<vector<int>>dp(n,vector<int>(27,-1));\n // return longestIdealStringMem(s,k,0,-1,dp);\n return longestIdealStringTab(s,k);\n }\n};", "memory": "136776" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int longestIdealString(string s, int k) {\n int n = s.size();\n vector<vector<int>>dp(n,vector<int>(26,0));\n dp[0][s[0]-'a']=1;\n int ans = 1;\n for(int i=1;i<n;i++){\n for(int j=0;j<26;j++){\n dp[i][j] = dp[i-1][j];\n ans=max(ans,dp[i][j]);\n }\n int j = s[i]-'a';\n for(int x=0;x<=k;x++){\n int dx = j+x;\n if(dx<26){\n dp[i][j] = max(dp[i][j],1+dp[i-1][dx]);\n }\n dx = j-x;\n if(dx>=0)dp[i][j] = max(dp[i][j],1+dp[i-1][dx]);\n ans=max(ans,dp[i][j]);\n }\n }\n return ans;\n }\n};", "memory": "141200" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int f(int index, string& s, int k, char prev, vector<vector<int>>& dp) {\n if (index < 0) return 0; // Base case: no characters to consider, length is 0\n \n if (dp[index][prev - 'a'] != -1) return dp[index][prev - 'a'];\n \n int take = 0;\n if (abs(s[index] - prev) <= k) {\n take = 1 + f(index - 1, s, k, s[index], dp);\n }\n \n int nottake = f(index - 1, s, k, prev, dp);\n \n return dp[index][prev - 'a'] = max(take, nottake);\n }\n\n int longestIdealString(string s, int k) {\n int n = s.size();\n vector<vector<int>> dp(n, vector<int>(26, -1));\n int maxLen = 0;\n\n for (int i = 0; i < n; ++i) {\n maxLen = max(maxLen, f(i, s, k, s[i], dp));\n }\n\n return maxLen;\n }\n};\n", "memory": "145624" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n // int solve(string s, int k , int index,char prev,vector<vector<int>> &dp ){\n // if(index == s.size()) return 0;\n // int take = INT_MIN;\n // if(dp[index][prev-'a']!=-1) return dp[index][prev-'a'];\n\n // if(prev == 'z' +1 || abs(s[index] - prev) <=k ) { take = 1 + solve(s, k, index+1, s[index],dp);}\n\n // int n_take = solve(s, k , index+1, prev,dp);\n \n \n // return dp[index][prev-'a'] = max(take,n_take);\n // }\nint tab(string s, int k ){\n vector<vector<int>> dp(s.size()+2,vector<int>(28,0));\n int prev = 26;\n for(int index = s.size()-1 ; index>= 0;index--){\n int x = s[index] - 'a';\n for(int prev = 26; prev>=0 ; prev--){\n int take = INT_MIN;\n \n if(prev == 26 || abs(s[index] -'a' - prev) <=k ) { take = 1 + dp[index+1][s[index]-'a'];}\n\n int n_take = dp[index+ 1][prev];\n \n \n dp[index][prev] = max(take,n_take);\n }\n }\n return dp[0][26];\n}\n int longestIdealString(string s, int k){\n // vector<vector<int>> dp(s.size()+1,vector<int>(27,-1));\n // return solve(s,k,0,'z'+1,dp);\n return tab(s,k);\n }\n};", "memory": "150048" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int longestIdealString(string s, int k) {\n unordered_map<char, int> ump;\n for (char c = 'a'; c <= 'z'; ++c)\n ump[c] = c - 'a';\n \n int n = s.length();\n vector<vector<int>> dp(n + 1, vector<int>(27, 0));\n\n for (int ind = n - 1; ind >= 0; --ind) {\n for (int prev = 0; prev <= 26; ++prev) {\n int val1 = dp[ind + 1][prev];\n int val2 = 0;\n if (prev == 26 || abs(ump[prev + 'a'] - ump[s[ind]]) <= k)\n val2 = 1 + dp[ind + 1][s[ind] - 'a'];\n dp[ind][prev] = max(val1, val2);\n }\n }\n\n return dp[0][26]; // Initial call with prevchar as '\\0' equivalent\n }\n};", "memory": "154471" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int index, const string& s, int k, int lastChar, vector<vector<int>>& dp) {\n if (index >= s.length()) {\n return 0;\n }\n\n if (lastChar != -1 && dp[index][lastChar] != -1) {\n return dp[index][lastChar];\n }\n\n // Don't take the current character\n int notTake = solve(index + 1, s, k, lastChar, dp);\n\n // Take the current character if it satisfies the condition\n int take = 0;\n if (lastChar == -1 || abs(s[index] - 'a' - lastChar) <= k) {\n take = 1 + solve(index + 1, s, k, s[index] - 'a', dp);\n }\n\n if (lastChar != -1) {\n dp[index][lastChar] = max(notTake, take);\n }\n return max(notTake, take);\n }\n\n int longestIdealString(string s, int k) {\n vector<vector<int>> dp(s.length(), vector<int>(26, -1));\n return solve(0, s, k, -1, dp);\n }\n};", "memory": "158895" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int solve(string& s,int idx,char prev,int k,vector<vector<int>>&dp){\n if(idx==s.size())\n return 0;\n if(prev!='{' && dp[idx][prev-'a']!=-1)\n return dp[idx][prev-'a'];\n int dontTake= solve(s,idx+1,prev,k,dp);\n \n int take=0;\n if(prev=='{' || abs(s[idx]-prev)<=k)\n take=1+ solve(s,idx+1,s[idx],k,dp);\n\n return dp[idx][prev-'a']= max(take,dontTake);\n \n }\n int longestIdealString(string s, int k) {\n int n=s.length();\n vector<vector<int>>dp(n+1,vector<int>(27,-1));\n return solve(s,0,'{',k,dp);\n }\n};", "memory": "163319" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n #define vi vector<int>\n #define vvi vector<vi>\n vvi dp;\n int n;\n\n int f(int i, char c, string &s, int k) {\n if (i == n) return 0;\n if (c != '#' && dp[i][c - 'a'] != -1) return dp[i][c - 'a'];\n int nt = f(i + 1, c, s, k);\n int t = 0;\n if (c == '#' || abs(s[i] - c) <= k) { \n t = 1 + f(i + 1, s[i], s, k);\n }\n \n return c == '#' ? max(nt, t) : dp[i][c - 'a'] = max(nt, t);\n }\n\n int longestIdealString(string s, int k) {\n n = s.size();\n dp.clear();\n dp.resize(n, vi(26, -1));\n return f(0, '#', s, k);\n }\n};\n", "memory": "167743" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n #define vi vector<int>\n #define vvi vector<vi>\n vvi dp;\n int n;\n\n int f(int i, char c, string &s, int k) {\n if (i == n) return 0;\n if (c != '#' && dp[i][c - 'a'] != -1) return dp[i][c - 'a'];\n int nt = f(i + 1, c, s, k);\n int t = 0;\n if (c == '#' || abs(s[i] - c) <= k) { \n t = 1 + f(i + 1, s[i], s, k);\n }\n return c == '#' ? max(nt, t) : dp[i][c - 'a'] = max(nt, t);\n }\n\n int longestIdealString(string s, int k) {\n n = s.size();\n dp.clear();\n dp.resize(n, vi(26, -1));\n return f(0, '#', s, k);\n }\n};\n", "memory": "167743" }
2,444
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p> <ul> <li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li> <li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned. Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcd&quot;, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n vector<vector<int>> dp;\n\n int helper(int ind, char prev, string& s, int k) {\n if (ind == s.size()) {\n return 0;\n }\n \n int prevIndex = (prev == -1) ? 0 : prev - 'a' + 1;\n if (dp[ind][prevIndex] != -1) {\n return dp[ind][prevIndex];\n }\n\n int maxLength = 0;\n\n // Option 1: Include current character if it's a valid addition\n if (prev == -1 || abs(prev - s[ind]) <= k) {\n maxLength = 1 + helper(ind + 1, s[ind], s, k);\n }\n\n // Option 2: Skip current character\n maxLength = std::max(maxLength, helper(ind + 1, prev, s, k));\n\n dp[ind][prevIndex] = maxLength;\n return maxLength;\n }\n\npublic:\n int longestIdealString(string& s, int k) {\n int n = s.size();\n dp.resize(n, vector<int>(27, -1));\n\n return helper(0, -1, s, k); \n }\n};\n", "memory": "172166" }