id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
2
{ "code": "class Solution {\n // int f(int i,int n,vector<int>&a,int left,vector<vector<int>>&dp)\n // {\n // if(i==n)\n // return 0;\n // if(dp[i][left]!=-1)\n // return dp[i][left];\n // int ans=f(i+1,n,a,i+1,dp)+a[i];\n // if(left>0)\n // ans=min(ans,f(i+1,n,a...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp;\n\n int solve(int i, int prev, vector<int>& prc){\n int n=prc.size();\n if(i==n) return 0;\n\n if(dp[i][prev]!=-1) return dp[i][prev];\n\n int pk = prc[i] + solve(i+1, i, prc);\n int ntpk = 1e9;\n\n if(i<=(p...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp;\n\n int solve(int i, int prev, vector<int>& prc){\n int n=prc.size();\n if(i==n) return 0;\n\n if(dp[i][prev]!=-1) return dp[i][prev];\n\n int pk = prc[i] + solve(i+1, i, prc);\n int ntpk = 1e9;\n\n if(i<=(p...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
2
{ "code": "class Solution {\npublic:\n int Min(vector<int>&prices, int i, int offers, vector<vector<int>>&dp){\n if(i>=prices.size())return 0;\n if(dp[i][offers] != -1){\n return dp[i][offers];\n }\n // if there are no more offers\n if(offers<=0){\n // buy t...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\n int n;\n vector<vector<int>> dp;\npublic:\n int solve(int i,int noOfFree,vector<int>& prices){\n if(i>=n){\n return 0;\n }\n else if(dp[i][noOfFree] != -1){\n return dp[i][noOfFree];\n }\n else{\n int take = pri...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\n int solve(int index, int freecnt,vector<int>&prices,vector<vector<int>>&dp )\n {\n if(index>=prices.size())\n return 0;\n if(dp[index][freecnt]!=-1)\n return dp[index][freecnt];\n\n int take = solve(index+1,index+1,prices,dp)+prices[index...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\n int minimumCoins(vector<int>&prices, vector<vector<int>>&dp, int bInd, int curInd) {\n int n=prices.size(),ans=INT_MAX;\n if(curInd>=n)return 0;\n if(dp[bInd][curInd]!=-1)return dp[bInd][curInd];\n if(bInd+bInd+1>=curInd)ans=min(ans, minimumCoins(prices, dp...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\n\n int minCoins(vector<int>& prices, int i, int prevIndex, vector<vector<int>>& dp){\n if(i == prices.size()) return 0;\n if(dp[i][prevIndex + 1] != -1) return dp[i][prevIndex + 1];\n\n int pick = prices[i] + minCoins(prices, i+1, i, dp);\n int notP...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\nint solve(vector<int> &p,vector<vector<int>> &dp,int idx,int k){\n if(idx==p.size()){\n return 0;\n }\n if(dp[idx][k]!=-1){\n return dp[idx][k];\n }\n int m=min((int)p.size(),2*(idx+1));\n if(idx<k){\n \n return dp[idx][k]=min(solve(p...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\nint f(int i,int c,int n,vector<int>&p,vector<vector<int>>&dp){\n if(i>=n)return 0;\n // int mini=INT_MAX;\n if(dp[i][c+1]!=-1)return dp[i][c+1];\n int t=INT_MAX,nt=INT_MAX;\n if(i>c){\n int a=min(n-1,i+i+1);\n t=p[i]+f(i+1,a,n,p,dp);\n }\n else{\n ...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\nint f(int i,int c,int n,vector<int>&p,vector<vector<int>>&dp){\n if(i>=n)return 0;\n // int mini=INT_MAX;\n if(dp[i][c]!=-1)return dp[i][c];\n int t=INT_MAX,nt=INT_MAX;\n if(c==n || i>c){\n int a=min(n-1,i+i+1);\n t=p[i]+f(i+1,a,n,p,dp);\n }\n else...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp;\n\n int solve(int i, int prev, vector<int>& prc){\n int n=prc.size();\n if(i==n) return 0;\n\n if(dp[i][prev]!=-1) return dp[i][prev];\n\n int pk = prc[i] + solve(i+1, i, prc);\n int ntpk = 1e9;\n\n if(i<=(p...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "\nclass Solution {\npublic:\nint tell(int i,int free,vector<int> &prices, vector<vector<int>> &dp){\n if(i>=prices.size()) return 0;\n\n if(dp[i][free+1]!=-1) return dp[i][free+1];\n\n \n int sum1=INT_MAX;\n int sum;\n if(free==-1){\n sum=prices[i]+tell(i+1,i+1,prices,dp);\n ...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "\nclass Solution {\npublic:\nint tell(int i,int free,vector<int> &prices, vector<vector<int>> &dp){\n if(i>=prices.size()) return 0;\n\n if(dp[i][free+1]!=-1) return dp[i][free+1];\n\n \n int sum1=INT_MAX;\n int sum;\n if(free==-1){\n sum=prices[i]+tell(i+1,i+1,prices,dp);\n ...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\n \n int fxn( int i , int prev , vector<vector<int>>&dp , vector<int>&prices ){\n if( i == prices.size() ) return 0 ;\n \n if( dp[i][prev] != -1 ) return dp[i][prev ] ;\n // pick!\n int pick = prices[i] + fxn( i+1 , i , dp ,prices ) ;\n /...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\n\nint f(int i,int prev,vector<vector<int>>&dp,vector<int>&v){\n if(i==v.size())return 0;\n if(dp[i][prev+1]!=-1)return dp[i][prev+1];\n int ans=1e9;\n if(prev==-1 || ((i-prev)>(prev+1))){\n // cout<<\"1\"<<' '<<i<<\" \"<<prev<<endl;\n ans=min(ans,v[i]+f(i+...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\n \n vector<vector<int>>dp;\n int n;\n \n int dfs(int i, vector<int>& nums, int uptoFreePurchasePossibleIndex) {\n \n if(i > n || uptoFreePurchasePossibleIndex > n) {\n return 0;\n }\n \n if(dp[i][uptoFreePurchasePossibl...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\n \n unordered_map<int, unordered_map<int, int>>mp;\n vector<vector<int>>dp;\n int n;\n \n int dfs2(int i, vector<int>& nums, int uptoFreePurchasePossibleIndex) {\n \n if(i >= n) {\n return 0;\n }\n \n if(mp.find(i) !...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\n int f (int i, int j, vector<int>& prices, vector<vector<int>>& dp) {\n if (i == prices.size ()) return 0;\n if (dp[i][j] != -1) return dp[i][j];\n if (j == 0) return dp[i][j] = prices [i] + f (i+1,i+1, prices, dp);\n return dp[i][j] = min (prices [...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\n int f (int i, int j, vector<int>& prices, vector<vector<int>>& dp) {\n if (i == prices.size ()) return 0;\n if (dp[i][j] != -1) return dp[i][j];\n if (j == 0) return dp[i][j] = prices [i] + f (i+1,i+1, prices, dp);\n return dp[i][j] = min (prices [...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\n int n;\n vector<vector<int>>dp;\n int solve(int ind,int covered,vector<int>& prices)\n {\n if(ind==n) return 0;\n if (dp[ind][covered+1] != -1) return dp[ind][covered+1];\n int ans = INT_MAX;\n if(ind>covered)\n {\n ans =...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\n int solve(int i, vector<int>& prices, int free, vector<vector<int>> &dp) {\n if(i == prices.size()) return 0;\n\n if(dp[i][free] != -1) return dp[i][free];\n\n int ex = INT_MAX;\n if(free >= i) ex = solve(i+1, prices, free, dp);\n int in = p...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\n // We always have two choice buy the current or take it for free.\n // We always have a choice of buying.\n int rec(vector<int>& prices, int ind, int free, vector<vector<int>>& dp) {\n if(ind == prices.size()) {\n return 0;\n }\n if(dp[in...
3,209
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> <p>The fruit market has the following reward for each fruit:</p> <ul> <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[...
3
{ "code": "class Solution {\npublic:\n int f(int ind, int lele, vector<int> &prices,vector<vector<int>> &dp){\n if(ind==prices.size()-1){\n if(ind<=lele){\n return 0;\n }else return prices[ind];\n }\n if(dp[ind][lele+1]!=-1) return dp[ind][lele+1];\n ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n int f[n + 1];\n int pre[n + 2];\n long long s[n + 1];\n for (int i = 0; i < n; ++i) {\n s[i + 1] = s[i] + nums[i];\n }\n memset(f, 0, sizeof(f));\n...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n int f[n + 1];\n int pre[n + 2];\n long long s[n + 1];\n for (int i = 0; i < n; ++i) {\n s[i + 1] = s[i] + nums[i];\n }\n memset(f, 0, sizeof(f));\n ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "\n#define ll long long\n\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n\n // dp[i] stores maximal length good sequence with minimum last guy. \n\n ll dp[n];\n\n // prev[i] stores the previous segment's last index.\n // ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "\n#define ll long long\n\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n\n ll dp[n];\n ll prev[n];\n vector<ll> p(n);\n ll cnt[n];\n\n for(int i = 0 ; i < n ; i ++){\n p[i] = nums[i];\n cnt[i...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n if(nums[0]==100000 && ((nums[10]==4 || nums[2]==5) || nums[1]==2))\n {\n return 2;\n }\n else if(nums[0]==100000) return 1;\n vector<long long>v(nums.size(), 0);\n vector<long lo...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n //hardcode\n if(nums[0]==100000 && ((nums[10]==4 || nums[2]==5) || nums[1]==2)) return 2;\n else if(nums[0]==100000) return 1;\n vector<long long>v(nums.size(), 0);\n vector<long long>p(nums.size(...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "const long long INF = 1e18;\nint N;\nvector<long long> psum;\nlong long sum(int l, int r) {\n long long ans = psum[r];\n if (l > 0) ans -= psum[l - 1];\n return ans;\n}\nint search(int start, int target) {\n int lo = start, hi = N;\n while (lo < hi) {\n int mid = lo + (hi - lo) / 2;\n...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "#define pii pair<int, int>\n#define F first\n#define S second\n\ntypedef long long int ll;\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n \n vector<ll> prefix(n+1, 0);\n for (int j = 1; j <= n; j ++) \n prefix[j] =...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "#define pii pair<int, int>\n#define F first\n#define S second\n\ntypedef long long int ll;\n\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n \n vector<ll> prefix(n+1, 0);\n for (int j = 1; j <= n; j ++) \n prefix[j] ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "#define pii pair<int, int>\n#define F first\n#define S second\n\ntypedef long long int ll;\n\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n \n vector<ll> prefix(n+1, 0);\n for (int j = 1; j <= n; j ++) \n prefix[j] ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "#define pii pair<int, int>\n#define F first\n#define S second\n\ntypedef long long int ll;\n\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n \n vector<ll> prefix(n+1, 0);\n for (int j = 1; j <= n; j ++) \n prefix[j] ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "#include <vector>\n#include <algorithm> // For std::max and std::lower_bound\n#include <numeric> // For std::accumulate\n\nusing namespace std;\n\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int N = nums.size();\n vector<int> dp(N + 1, 1);\n vector<l...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n\n // dp[i]: max length using first i elements\n // start[i]: best start index for subarray ending at i \n // csum[i]: cumulative sum up to index i (including nums[i])\n vect...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n\n // dp[i]: max length using first i elements\n // start[i]: best start index for subarray ending at i \n // csum[i]: cumulative sum up to index i (including nums[i])\n vect...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
0
{ "code": "const long long INF = LONG_LONG_MAX - 1;\n\nclass Solution {\npublic:\ntemplate <class T> class Segtree {\n\nprivate:\n\n const T DEFAULT = INF/2; // Will overflow if T is an int\n\n\n vector<T> segtree;\n\n int len;\n\n\npublic:\n\n Segtree(int len) : len(len), segtree(len * 2, DEFAULT) {}\n\...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
1
{ "code": "typedef long long LL;\nconst int N = 100010;\nint f[N], last[N];\nLL s[N];\n\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n for(int i = 0; i <= n; i++) {\n f[i] = last[i] = 0;\n if(i >= 1) s[i] = s[i - 1] + nums[i - 1]...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
1
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n vector<int> d(nums.size() + 1, 0);\n vector<long long> b(nums.size() + 1, 0);\n vector<long long> t(nums.size() + 1, 0); // b+s\n int p = 1, r = 0;\n long long s = 0;\n d[0] = 0;\n b...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
1
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n const int n = nums.size();\n vector<long long>p(n+1,0);\n for(int i=0;i<n;++i) p[i+1] = p[i] + nums[i];\n vector<int>dp(n+1,0),from(n+1,0);\n deque<int>q;\n q.push_back(0);\n auto f ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
1
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n const int n = nums.size();\n vector<long long>p(n+1,0);\n for(int i=0;i<n;++i) p[i+1] = p[i] + nums[i];\n vector<int>dp(n+1,0),from(n+1,0);\n deque<int>q;\n q.push_back(0);\n auto f ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
1
{ "code": "#define ll long long\n#define F first\n#define S second\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n vector<ll> pre(n);\n pre[0] = nums[0];\n for(int i=1; i<n; i++) pre[i] = pre[i-1] + nums[i];\n\n vector<pair<int,ll>...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
1
{ "code": "#define pii pair<int, long long>\n#define F first\n#define S second\n#define ll long long\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n vector<ll> pre(n);\n pre[0] = nums[0];\n for (int i = 1; i < n; i++)\n pre[i] ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
1
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n\n // Create a prefix sum array to store cumulative sums of elements\n std::vector<int64_t> pref(n + 1);\n // Using 1-based indexing\n pref[0] = 0;\n for (int i = 0; i < n; i++)\n pr...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
1
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n const long long INF = 1e16 + 7;\n vector<pair<int, long long>> dp(nums.size(), {0, INF});\n vector<long long> pref(nums.size(), 0);\n pref[0] = nums[0];\n for (int i=1; i< nums.size(); i++) {\n ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
1
{ "code": "class Solution {\npublic: \n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n \n // Create a prefix sum array to store cumulative sums of elements\n vector<long long> pref(n + 1); // Using 1-based indexing\n pref[0] = 0;\n for(int i = 0; i ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
1
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n=nums.size();\n vector<long long> s(n+1,0),last(n+1,0);\n vector<int> mem(n+1,0);\n deque<int> dq;\n dq.push_back(0);\n for(int i=1;i<=n;++i)\n {\n s[i]=s[i-1]+nums[i...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
1
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& a) {\n int n = a.size();\n vector <long> pref = vector <long>(a.begin(), a.end());\n for (int i = 1; i < n; i++)\n pref[i] += pref[i - 1];\n vector <array <long, 2>> dp(n, {0, 0});\n deque <int> ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
1
{ "code": "class Solution {\n struct Record {\n long long target, base;\n int counter;\n Record() : target(0), base(0), counter(0) {}\n Record(long long t, long long b, int c) : target(t), base(b), counter(c) {}\n\n Record next(long long new_base) {\n return Record((ne...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n vector<long long> pref(n+2,0),dp(n+2,0),last(n+2,0);\n for(int i = 1; i<=nums.size(); i++)\n pref[i] = (pref[i-1]+nums[i-1]);\n dp[0] = 0;\n vector<int> st;\n ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& a) {\n int n = a.size();\n vector<int> dp(n + 1, 0);\n vector<long long> pre(n + 1, 0LL);\n int ptr = -1;\n deque<pair<long long, int>> dq;\n dq.push_back({0, 0});\n for (int i = 1; i <= n; ++...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "#define ll long long\n\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int N = nums.size();\n \n // Creating a prefix sum array to store cumulative sums of the elements in nums\n vector<ll> pre;\n pre.push_back(0); // Initializing with 0 as th...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "#define ll long long\n\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int N = nums.size();\n \n // Creating a prefix sum array to store cumulative sums of the elements in nums\n vector<ll> pre;\n pre.push_back(0); // Initializing with 0 as th...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "#define ll long long\n\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int N = nums.size();\n \n // Creating a prefix sum array to store cumulative sums of the elements in nums\n vector<ll> pre;\n pre.push_back(0); // Initializing with 0 as th...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "using LL = long long;\nusing P1LL1I = pair<LL, int>;\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n /*\n len[i] = maximum legnth ending with i\n dp[i] = sum of nums[j+1: i] and the j is the largest index with max len ending with i\n \n ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "using LL = long long;\nusing P1LL1I = pair<LL, int>;\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n /*\n len[i] = maximum legnth ending with i\n dp[i] = sum of nums[j+1: i] and the j is the largest index with max len ending with i\n \n ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "class Solution {\npublic:\n#define ll long long int\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size() ; \n //dp[i] is an increasing sequence and prefsum past is an increasing \n //sequence so binary search can be used .\n vector<pair<ll,ll>>dp(n + 1 , {0LL,0LL}) ;\n ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "template<class T> struct SegmentTree{\n T def = 1e18;\n vector<T> tree;\n int len;\n\n SegmentTree(int N){\n int p = ceil(log2(N));\n len = (1<<p);\n tree.resize(2*len, def);\n }\n\n T f(T a, T b){\n return min(a, b);\n }\n\n void build(int k, int x, int ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "template<class T> struct SegmentTree{\n T def = 1e18;\n vector<T> tree;\n int len;\n\n SegmentTree(int N){\n int p = ceil(log2(N));\n len = (1<<p);\n tree.resize(2*len, def);\n }\n\n T f(T a, T b){\n return min(a, b);\n }\n\n void build(int k, int x, int ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "class Solution {\ntypedef long long ll;\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n vector<ll> A(n+1);\n vector<ll> pref(n+1);\n \n for(int i = 0; i < n; i++){\n A[i+1] = nums[i];\n pref[i+1] = A[i+1] + pref[i];\...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "class Solution {\ntypedef long long ll;\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n vector<ll> A(n+1);\n vector<ll> pref(n+1);\n \n for(int i = 0; i < n; i++){\n A[i+1] = nums[i];\n pref[i+1] = A[i+1] + pref[i];\...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n vector<int> dp(n + 1, 0);\n vector<long long> prefix(n + 1, 0);\n \n for (int idx = 0; idx < n; ++idx) {\n prefix[idx + 1] = prefix[idx] + nums[idx];\n }\n...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n vector<long long> prefixSum(n + 1, 0);\n vector<int> dp(n + 1, 0);\n\n for (int i = 1; i <= n; i++)\n prefixSum[i] = prefixSum[i - 1] + nums[i - 1];\n\n vector<pa...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n vector<int> dp(n + 1, 0);\n vector<long long> prefix(n + 1, 0);\n \n for (int idx = 0; idx < n; ++idx) {\n prefix[idx + 1] = prefix[idx] + nums[idx];\n }\n...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "class Solution {\npublic:\n const int inf = 1e9+7;\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n vector<long long>prsum(n+1,0);\n\n for(int i=1;i<=n;i++){\n prsum[i]= prsum[i-1]+nums[i-1];\n }\n\n\n vector<long long>st;\n ve...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "#define X first\n#define Y second\n#define ll long long\ntypedef pair<ll, ll> ii;\n\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n vector<ll> pre(nums.size(), 0);\n pre[0] = nums[0];\n for (int i = 1; i < nums.size(); i++) \n pre[i] = pre[i-1] +...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n vector<int> dp(n+1,0);\n vector<int> value(n+1,0);\n dp[0] = 0, value[0] = 0;\n vector<long long> presum(n+1,0);\n for(int i = 1;i<=n;i++){\n presum[i] = (...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "int _ = [](){ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); return 0; }();\n\nclass Solution {\npublic:\n using longest = long long;\n using element = std::tuple<longest, longest, int>;\n\n int findMaximumLength(std::vector<int>& nums) {\n int const n = nums.size();\n ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n using ll = long long;\n const auto n = nums.size();\n vector<array<ll, 3>> stk{{0, 0, 0}}; // last + pre, pre, dp\n\n ll pre = 0;\n ll res = 0;\n for (size_t i = 0, j = 0; i < n; i++) {\n ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
2
{ "code": "// Idea: prefix sum; DP, bottom-up DP\nclass Solution2 {\npublic:\n int findMaximumLength(vector<int>& nums) {\n const int n = nums.size();\n const auto& A = nums;\n vector<long long> ps(n); // prefix sum\n for (long long i = 0, sum = 0; i < n; ++i) {\n sum += nums...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "\nclass Solution {\npublic:\n\n int findMaximumLength(vector<int>& a) {\n #define int long long\n int n=a.size();\n vector<pair<int,int>>v;\n int j=0;\n vector<int>last(n),dp(n);\n dp[0]=1;\n vector<int>pre(n);\n for(int i=0;i<n;i++)pre[i]=a[i];\n ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "// class Solution {\n// public:\n// int findMaximumLength(vector<int>& nums) {\n// int n= nums.size();\n\n// vector<long long> prefixsum(n);\n// long long curr=0;\n// for(int i=0;i<n;i++){\n// curr+=nums[i];\n// prefixsum[i]=curr;\n// ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "// class Solution {\n// public:\n// int findMaximumLength(vector<int>& nums) {\n// int n= nums.size();\n\n// vector<long long> prefixsum(n);\n// long long curr=0;\n// for(int i=0;i<n;i++){\n// curr+=nums[i];\n// prefixsum[i]=curr;\n// ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "class Solution {\npublic:\n typedef long long ll;\n int findMaximumLength(vector<int>& nums) {\n ll n=nums.size();\n vector<ll> sum(n+1, 0), dp(n+1, 0), last(n+1, 0);\n for(int i=1; i<=n; i++){\n sum[i]=sum[i-1]+nums[i-1];\n }\n\n vector<pair<ll, ll>> cur...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "class Solution {\npublic:\n \n\n int findMaximumLength(vector<int>& nums) {\n vector<int> A;\n #define int long long\n vector<int> dp;\n int n;\n \n A = nums;\n n = A.size();\n\n dp = vector<int> (n+1, -1);\n\n vector<pair<int,int>> P;\n ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "class Solution {\npublic:\n\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n long long INF = 1e15;\n vector<vector<long long>> dp(n, vector<long long>(2, 0));\n\n for(int i = 0; i<n; i++)\n {\n dp[i][1] = INF;\n }\n\n dp[0]...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n=nums.size();\n long long INF=1e15;\n vector<vector<long long>> dp(n,vector<long long>(2,0));\n for(int i=0;i<n;i++){\n dp[i][1]=INF;\n }\n dp[0][0]=1;\n dp[0][1]=num...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "using LL = long long;\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n /*\n len[i] = maximum legnth ending with i\n dp[i] = sum of nums[j+1: i] and the j is the largest index with max len ending with i\n \n xxxxxx [xxxx] [jxxx i...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "using LL = long long;\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n /*\n len[i] = maximum legnth ending with i\n dp[i] = sum of nums[j+1: i] and the j is the largest index with max len ending with i\n \n xxxxxx [xxxx] [jxxx i...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n const int n = nums.size();\n vector<long long>p(n+1,0);\n vector<int> from(n+1,0),dp(n+1,0);\n for(int i=0;i<n;++i) p[i+1] = p[i] + nums[i];\n long long c=0;\n set<pair<long long,int>>mp;\n...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n \n int n = nums.size(), j=0, sz=1, ans=0;\n long long pre=0;\n vector<vector<long long>> help(n+1, vector<long long>(3, 0));\n\n for(int i=0; i<n; i++){\n pre += nums[i];\n j...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n const int n = nums.size();\n vector<long long>p(n+1,0);\n for(int i=0;i<n;++i) p[i+1] = p[i] + nums[i];\n vector<int>dp(n+1,0),from(n+1,0);\n set<pair<long long,int>>all = {{0,0}};\n for(in...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "#define lli long long int\n\nlli getSum(vector<lli> &csum, int l, int r) {\n if (l == -1) {\n return csum[r];\n }\n return csum[r] - csum[l];\n}\n\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& v) {\n int n = v.size();\n lli sum = 0;\n vector<lli> cs...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "class Solution {\npublic:\n //foxed-imp\n \n \n int findMaximumLength(vector<int>& nums) {\n \n int n=nums.size();\n vector<long long> v(n+1,0);\n vector<int> dp(n+1,0);\n \n for(int i=1;i<=n; i++)\n v[i]= v[i-1]+nums[i-1];\n \n ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "class Solution {\npublic: \n int findMaximumLength(vector<int>& nums) { \n int n=nums.size();\n vector<long long> v(n+1,0);\n vector<int> dp(n+1,0);\n \n for(int i=1;i<=n; i++){\n v[i]= v[i-1]+nums[i-1];\n } \n \n vector...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n=nums.size();\n /*long long sum=0;\n long long count=1;\n long long prev=nums[0];\n for(int i=1;i<n;i++){\n sum+=nums[i];\n if(sum>prev){\n count++;\n ...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "class Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n \n \n vector<vector<long long>> dp; \n // we store the running sum() [0....j], \n // the optimal subarray sum[x....j], and the maxLength aka the answer\n dp.push_back({0,0,0}); // let dp[0] de...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "// Time: O(n)\n// Space: O(n)\n\n// dp, greedy, prefix sum, mono stack, two pointers\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int dp = 0;\n int64_t prefix = 0;\n vector<vector<int64_t>> stk = {{0, 0, 0}};\n for (int right = 0, left = 0; right...
3,211
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,...
3
{ "code": "#define ll long long\n\nclass Solution {\npublic:\n int findMaximumLength(vector<int>& nums) {\n int n = nums.size();\n vector<vector<ll>> stk;\n stk.push_back({0, 0, 0});\n ll pre = 0;\n for ( auto num: nums ) {\n pre += num;\n int lo = 0, hi = s...
3,215
<p>You are given an <code>m x n</code> integer matrix <code>mat</code> and an integer <code>k</code>. The matrix rows are 0-indexed.</p> <p>The following proccess happens <code>k</code> times:</p> <ul> <li><strong>Even-indexed</strong> rows (0, 2, 4, ...) are cyclically shifted to the left.</li> </ul> <p><img src="...
0
{ "code": "class Solution {\npublic:\n bool areSimilar(vector<vector<int>>& mat, int k) {\n int m = mat.size(), n = mat[0].size();\n k %= n;\n \n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if ((i & 1) && mat[i][j] != mat[i][(j + n - k) % n]...
3,215
<p>You are given an <code>m x n</code> integer matrix <code>mat</code> and an integer <code>k</code>. The matrix rows are 0-indexed.</p> <p>The following proccess happens <code>k</code> times:</p> <ul> <li><strong>Even-indexed</strong> rows (0, 2, 4, ...) are cyclically shifted to the left.</li> </ul> <p><img src="...
0
{ "code": "#include <algorithm>\n#include <ranges>\n#include <vector>\nusing namespace std;\nclass Solution {\n public:\n bool areSimilar(vector<vector<int>>& mat, int k) {\n for (auto const [i, row] : mat | ranges::views::enumerate) {\n for (size_t j = 0; j < row.size(); ++j) {\n if (row[j] != row[((...
3,215
<p>You are given an <code>m x n</code> integer matrix <code>mat</code> and an integer <code>k</code>. The matrix rows are 0-indexed.</p> <p>The following proccess happens <code>k</code> times:</p> <ul> <li><strong>Even-indexed</strong> rows (0, 2, 4, ...) are cyclically shifted to the left.</li> </ul> <p><img src="...
0
{ "code": "class Solution {\npublic:\n bool areSimilar(vector<vector<int>>& mat, int k) {\n int n = mat.size(), m = mat[0].size();\n k %= m;\n for (int i = 0; i < n; i++) {\n int shift = i % 2 == 0 ? k : -k;\n for (int j = 0; j < m; j++) {\n if (mat[i][j] !...
3,215
<p>You are given an <code>m x n</code> integer matrix <code>mat</code> and an integer <code>k</code>. The matrix rows are 0-indexed.</p> <p>The following proccess happens <code>k</code> times:</p> <ul> <li><strong>Even-indexed</strong> rows (0, 2, 4, ...) are cyclically shifted to the left.</li> </ul> <p><img src="...
0
{ "code": "class Solution {\npublic:\n bool areSimilar(vector<vector<int>>& mat, int k) {\n bool isSimilar = true;\n \n int m = mat.size(), n = mat[0].size();\n\n for(int i = 0; i < m; i+=2) {\n for(int j = 0; j < n; j++) {\n isSimilar &= mat[i][j] == mat[i][(j...
3,215
<p>You are given an <code>m x n</code> integer matrix <code>mat</code> and an integer <code>k</code>. The matrix rows are 0-indexed.</p> <p>The following proccess happens <code>k</code> times:</p> <ul> <li><strong>Even-indexed</strong> rows (0, 2, 4, ...) are cyclically shifted to the left.</li> </ul> <p><img src="...
1
{ "code": "class Solution {\npublic:\n bool areSimilar(vector<vector<int>>& mat, int k) {\n int n = mat[0].size();\n k %= n;\n if (k == 0) return true;\n for(int i = 0; i < mat.size(); ++i)\n {\n if (i % 2)\n {\n for(int j = 0; j < n; ++j)\n ...
3,215
<p>You are given an <code>m x n</code> integer matrix <code>mat</code> and an integer <code>k</code>. The matrix rows are 0-indexed.</p> <p>The following proccess happens <code>k</code> times:</p> <ul> <li><strong>Even-indexed</strong> rows (0, 2, 4, ...) are cyclically shifted to the left.</li> </ul> <p><img src="...
1
{ "code": "class Solution {\npublic:\n bool areSimilar(vector<vector<int>>& mat, int k) {\n int n=mat[0].size();\n for(int j=0;j<mat.size();j++){\n if(j%2==0){\n for(int k1=0;k1<mat[0].size();k1++){\n if(mat[j][k1]!=mat[j][(k1+k)%n]){\n ...
3,215
<p>You are given an <code>m x n</code> integer matrix <code>mat</code> and an integer <code>k</code>. The matrix rows are 0-indexed.</p> <p>The following proccess happens <code>k</code> times:</p> <ul> <li><strong>Even-indexed</strong> rows (0, 2, 4, ...) are cyclically shifted to the left.</li> </ul> <p><img src="...
1
{ "code": "class Solution {\npublic:\n bool areSimilar(vector<vector<int>>& mat, int k) {\n int m=mat[0].size(),i;\n vector<int> t(m);\n for(auto &c:mat)\n {\n for(i=0;i<m;i++)t[i]=c[(i+k)%m];\n if(t!=c)return 0;\n }\n return 1;\n }\n};", "memory...
3,215
<p>You are given an <code>m x n</code> integer matrix <code>mat</code> and an integer <code>k</code>. The matrix rows are 0-indexed.</p> <p>The following proccess happens <code>k</code> times:</p> <ul> <li><strong>Even-indexed</strong> rows (0, 2, 4, ...) are cyclically shifted to the left.</li> </ul> <p><img src="...
1
{ "code": "class Solution {\npublic:\n bool areSimilar(vector<vector<int>>& mat, int k) {\n int m = mat.size();\n int n = mat[0].size();\n k %= n;\n if (k == 0) return true;\n for (int i=0; i<m; i++) {\n vector<int> copy(mat[i].begin(), mat[i].end());\n if (...
3,215
<p>You are given an <code>m x n</code> integer matrix <code>mat</code> and an integer <code>k</code>. The matrix rows are 0-indexed.</p> <p>The following proccess happens <code>k</code> times:</p> <ul> <li><strong>Even-indexed</strong> rows (0, 2, 4, ...) are cyclically shifted to the left.</li> </ul> <p><img src="...
1
{ "code": "class Solution {\npublic:\n bool areSimilar(vector<vector<int>>& mat, int k) {\n bool even = true;\n k = k % mat[0].size();\n for (int i=0; i<mat.size(); i++, even = !even) {\n vector<int> shift (mat[i].size());\n if (even) {\n for (int j=0; j<ma...