id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int f(int i,int&n,vector<int>&v,vector<int>&dp){\n if(i>=n-1)return 0;\n if(dp[i]!=-1)return dp[i];\n int ans=0;\n ans=max(ans,f(i+1,n,v,dp));\n for(int j=i+1;j<n;j++){\n if(v[j]-v[i] > 0){\n ans=max(ans,v[j]-v[i]+f...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\nprivate:\n vector<pair<int, int>> ngePair(vector<int>& prices) {\n int n = prices.size();\n vector<pair<int, int>> ans(n, {-1, -1});\n stack<int> s;\n\n for (int i = 0; i < n; i++) {\n while (!s.empty() && prices[i] > prices[s.top()]) {\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\nprivate:\n vector<pair<int, int>> ngePair(vector<int>& prices) {\n int n = prices.size();\n vector<pair<int, int>> ans(n, {-1, -1}); \n stack<int> s;\n for (int i = n - 1; i >= 0; i--) {\n while (!s.empty() && prices[s.top()] <= prices[i]) {\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n // int Memo(int idx,int buy,vector<int>& prices,vector<vector<int>>& dp){\n // if(idx==prices.size()) return 0;\n // if(dp[idx][buy]!=-1) return dp[idx][buy];\n // int profit = 0;\n // if(buy){\n // profit = max(-prices[idx]+Memo(idx+1,0...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\nprivate:\n int recursion(int ind, int buy, vector<int>&prices){\n // base case\n if(ind==prices.size()) return 0;\n // logic\n int profit=0;\n if(buy){\n profit = max(-prices[ind] + recursion(ind+1, 0, prices), \n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n\n // Recursive\n int stocks(int i, int buy, vector<int>& arr,\n vector<vector<int>> &dp){\n if(i == arr.size()){\n return 0;\n }\n\n if(dp[i][buy] != -1){\n return dp[i][buy];\n }\n\n int profit;\n if(buy){...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& values) {\n int n = values.size();\n vector<int> prev (2,0) ;\n prev[1] = -(10e5+1);\n for(int i = 0;i<n;i++)\n {\n vector<int> curr(2,0);\n curr[0] = max(prev[0],prev[1]+values[i]);\n curr[1] = max(pre...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\nprivate:\n int recursion(int ind, int buy, vector<int>&prices){\n // base case\n if(ind==prices.size()) return 0;\n // logic\n int profit=0;\n if(buy){\n profit = max(-prices[ind] + recursion(ind+1, 0, prices), \n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "/*\nclass Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int maxprofit = 0;\n for(int i = 1 ; i < prices.size() ; i++){\n if(prices[i-1] < prices[i]){\n maxprofit += prices[i] - prices[i-1];\n }\n }\n return maxprofit;\n }...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n\n typedef struct State {\n int price;\n int profit;\n\n State(int v) {\n price = v;\n profit = v;\n }\n } State;\n\n int maxProfit(vector<int>& prices) {\n\n vector<State> hold_some(prices.size()+1, 0);\n v...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int** profit = new int*[prices.size()];\n for(int i = 0; i < prices.size(); i ++)\n {\n profit[i] = new int[2];\n }\n\n profit[0][0]= 0;\n profit[0][1] = -prices[0];\n\n for(int...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int findnextmax(vector<int>& prices,int start,int prev){\n while(start<prices.size() && prices[start]>prices[prev]){\n start++;\n prev++;\n }\n return prev;\n }\n int findans(vector<int>& prices,int start,int prev){\n if...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\nint dp[30001][2];\n\nint solve(vector<int>&p, int ind,int buy){\n if(ind>=p.size()){\n return 0;\n }\n\n if(dp[ind][buy]!=-1){\n return dp[ind][buy];\n }\n int ans=-1e9;\n if(buy==1){\n ans=max(-1*p[ind]+solve(p,ind+1,0),solve(p,ind+1,1));\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int dp[30001][2];\n int profit(int ind,int buy,vector<int> & v){\n \n if(ind==v.size()){\n // if(buy==0) return 0;\n // else{\n // return v[ind];\n // }\n return 0;\n }\n if(dp[ind][b...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int dp[30001][2];\n int profit(int ind,int buy,vector<int> & v){\n \n if(ind==v.size()-1){\n if(buy==0) return 0;\n else{\n return v[ind];\n }\n }\n if(dp[ind][buy]!=-1) return dp[ind][buy];\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n // int f(int i, int b, vector<int>&p){\n // // base case\n // if(i == p.size()) return 0;\n // // check if u hold already hold a share\n // if(b==0){\n // // max(do nothing, buy)\n // return max(f(i+1,0,p),f(i+1,1,p)-p[i]);\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n\n int f(int ind, int buy, int n, vector<int>&prices,vector<vector<int>>&dp){\n if(ind==n) return 0;\n if(dp[ind][buy]!=-1) return dp[ind][buy];\n int profit=0;\n if(buy){\n profit = max(-prices[ind] + f(ind+1,0,n,prices,dp),\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int f(int ind, int buy, vector<int> &prices, vector<vector<int>> &dp){\n int n = prices.size();\n if(ind==n) return 0;\n if(dp[ind][buy]!=-1) return dp[ind][buy];\n int profit = 0;\n if(buy){\n profit = max(-prices[ind]+f(ind+1, 0...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n // int f(int idx, int buy, vector<int>& prices, vector<vector<int>>&dp) {\n // //bcase\n // if(idx == prices.size()) {\n // //ye case tab ayega jab apan buy to karlia but sell nahi kia\n // return 0;\n // }\n\n // if(dp[idx][b...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n\n int maxProfit(vector<int>& prices) {\n int n=prices.size();\n vector<vector<int>> dp(n+1, vector<int>(2, -1));\n if(n==0){\n return 0;\n }\n\n dp[n][0]=dp[n][1]=0;\n int profit;\n for(int i=n-1;i>=0;i--){\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n\n // Recursive\n int stocks(int i, int buy, vector<int>& arr,\n vector<vector<int>> &dp){\n if(i == arr.size()){\n return 0;\n }\n\n if(dp[i][buy] != -1){\n return dp[i][buy];\n }\n\n int profit;\n if(buy){...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int max(int a,int b){\n return a>b ? a :b;\n }\n int f(vector<int>& prices,int n,int i,int buy, vector<vector<long>> dp){\n if(n==i){\n return 0;\n }\n if(dp[i][buy]!=-1) return dp[i][buy]; \n int profit=0;\n\n if(buy...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n\n int solve(vector<int>&prices, int in,int b,int n,vector<vector<int>>&dp)\n {\n if(in==n)\n {\n return 0;\n }\n\n if(dp[in][b]!=-1)\n {\n return dp[in][b];\n }\n\n int profit=0;\n\n if(b)\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int dp[40000][2];\n int solve(vector<int>& prices,int i, int n,int buy) {\n if(i >= n)\n return 0;\n \n if(dp[i][buy] != -1)\n return dp[i][buy];\n \n int profit = 0;\n\n if(buy) {\n profit = max(-p...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int solve(vector<int> &prices, int ind,int b,vector<int> &dp){\n if(ind>=prices.size() || b>=prices.size()){\n return 0;\n }\n if(dp[b]!=-1){\n return dp[b];\n }\n if(ind == b){\n return dp[b] = max(solve(pri...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n vector<vector<int>> memo(2, vector<int>(prices.size(), -1));\n return dp(prices, memo, 0, true);\n }\nprivate:\n int dp(vector<int>& prices, vector<vector<int>>& memo, int index, bool canBuy) {\n if (index == p...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n dp.resize(2, vector<int>(prices.size(), -1));\n return helper(prices, 0, 0);\n }\n\nprivate:\n vector<vector<int>> dp;\n int helper(vector<int>& prices, int i, int held) {\n \n if(i>=prices.size()) re...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int helper(vector<int>& prices, int idx, bool have_it, vector<pair<int,int>>& dp){\n if(idx == prices.size()){\n return 0;\n }\n if(have_it){\n if(dp[idx].second != -1){\n return dp[idx].second;\n }\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int dp[100001][2];\n\n int solve(vector<int>& prices, int i, bool buy) {\n if (i == prices.size()) return 0;\n \n if (dp[i][buy] != -1) return dp[i][buy];\n \n if (buy) {\n int buyNow = solve(prices, i + 1, false) - prices[i];\...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\n private:\n int max_profit(vector<int>&prices,int i,vector<int>&dp){\n int n=prices.size();\n if(i>=n){\n return 0;\n }\n if(dp[i]!=-1){\n return dp[i];\n }\n int final_ans=0;\n for(int k=i;k<n;k++){\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int n;\n int dp[100001][3];\n int f(int idx, bool buy, vector<int>& prices){\n if(idx == n) return 0;\n\n int profit = 0;\n\n if( dp[idx][buy] != -1) return dp[idx][buy];\n\n if(buy){\n profit = max(-prices[idx] + f(idx+1,false,pri...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n vector<array<int, 2>> dp(prices.size(), {-1, -1});\n return recur(0, 0, dp, prices);\n }\n\n int recur(int i, int buy, vector<array<int, 2>>& dp, vector<int>& prices) {\n if (i == prices.size())\n re...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution \n{\npublic:\n int dp(vector<int>& prices, int i, vector<vector<int>>& memo, bool holding)\n {\n if (i >= prices.size())\n {\n return 0;\n }\n\n if (memo[holding][i] != -1)\n {\n return memo[holding][i];\n }\n\n if ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\n int helper(bool buy,int index,vector<int>&prices,vector<vector<int>>&dp,\n int n){\n if(index == n)\n return 0;\n\n if(dp[buy][index] != -1)\n return dp[buy][index];\n\n if(buy)\n dp[buy][index]=max(-prices[index]+helper(false,index+1,price...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int dp[100005][3];\n int f(vector<int>& prices,int i,bool flag){\n if(i==prices.size()) return 0;\n int ans = 0;\n if(dp[i][flag]!=-1) return dp[i][flag];\n ans = max(ans,f(prices,i+1,flag));\n if(flag){\n ans = max(ans,prices[...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int greedy(vector<int>& prices) {\n int n = prices.size();\n int profit = 0;\n\n for (int i = 1; i < n; i++) {\n\n if (prices[i] > prices[i - 1]) {\n profit += prices[i] - prices[i - 1];\n }\n }\n\n retur...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int helper(int buy, int i, vector<int> &prices, int n,vector<vector<int>> &dp)\n {\n if(i==n)\n {\n return 0;\n }\n if(dp[buy][i]!=-1) return dp[buy][i];\n int profit;\n if(buy)\n {\n profit=max(-prices...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int calc_profit(vector<int> &prices,int idx,int bought,vector<vector<int>> &dp){\n if(idx==prices.size()) return 0;\n if(dp[bought][idx]!=-1) return dp[bought][idx];\n int ans = calc_profit(prices,idx+1,bought,dp);\n if(bought) ans = max(ans,prices...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int check(vector<int>&nums,int check1,int index,vector<vector<int>>&dp){\n if(index==nums.size()){\n return 0;\n }\n if(dp[check1][index]!=-1){\n return dp[check1][index];\n }\n if(check1){\n int a=-nums[inde...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n\n vector<vector<int>>dp;\n int n;\n\n int rec(int i,int lastTrasaction,vector<int>& prices){\n\n // 0 sold\n // 1 buy\n\n if(i==n){\n if(lastTrasaction==1)return -1e9;\n return 0;\n }\n\n if(dp[i][lastTrasaction]!...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\nvector<vector<int>>dp;\npublic:\n int solve(vector<int>&prices, int i,bool buy){\n if(i>=prices.size())return 0;\n // int buy, int sell\n if(dp[i][buy]!=-1)return dp[i][buy];\n int a= 0;\n if(buy){\n a = solve(prices, i+1, !buy)-prices[i];\...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int f(int i, bool canBuy, vector<int> &p, vector<vector<int>>& dp){\n if(i == p.size()) return 0;\n if(dp[i][canBuy] != -1) return dp[i][canBuy];\n if(canBuy){\n int buy = -p[i]+f(i+1, !canBuy, p, dp);\n int notbuy = f(i+1, canBuy, p...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int stocks(int i, int buy, vector<int>& arr,\n vector<vector<int>> &dp){\n if(i == arr.size()){\n return 0;\n }\n\n if(dp[i][buy] != -1){\n return dp[i][buy];\n }\n\n int profit;\n if(buy){\n int b ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\nprivate:\n int recursion(int ind, int buy, vector<int>&prices){\n // base case\n if(ind==prices.size()) return 0;\n // logic\n int profit=0;\n if(buy){\n profit = max(-prices[ind] + recursion(ind+1, 0, prices), \n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int solve(int index, int n, bool buy, vector<int>&prices, vector<vector<int>>&dp){\n //base case\n if(index>=n) return 0;\n if(dp[index][buy]!=-1) return dp[index][buy];\n //other cases\n int take=0, notTake=0;\n if(buy){\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int getTheMaxProfit(vector<int> &prices, vector<vector<int>> &dp, int itr, int buy) {\n if(itr == prices.size())\n return 0;\n if(dp[itr][buy] != -1)\n return dp[itr][buy];\n\n int maxProfit = 0;\n if(buy)\n maxProf...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n\n int helper(int ind,int buy,vector<int>& prices,vector<vector<int>>& dp) {\n if(ind == prices.size()) {\n return 0;\n }\n if(dp[ind][buy] != -1) {\n return dp[ind][buy];\n }\n int profit = 0;\n if(buy) {\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int solve(vector<int>& prices,int buy,int i,vector<vector<int>>& dp){\n if(i==prices.size()){\n return 0;\n }\n if(dp[i][buy]!=-1){\n return dp[i][buy];\n }\n int profit=0;\n if(buy){\n profit=max(-pri...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int solve(int ind,int buy,vector<int>& prices,vector<vector<int>>& dp){\n if(ind==prices.size())return 0;\n\n if(dp[ind][buy]!=-1)return dp[ind][buy];\n int profit=0;\n if(buy){\n profit=max(-prices[ind]+solve(ind+1,0,prices,dp),0+solve(...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n\n int fun(int idx,int buy,vector<int>&prices,vector<vector<int>>&dp){\n if(idx==prices.size()) return 0;\n int profit;\n if(dp[idx][buy]!=-1) return dp[idx][buy]; \n if(buy) \n profit=max(-prices[idx]+fun(idx+1,0,prices,dp),fun(idx+1,1,price...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\n private:\n\n int solve(int idx, int buy,vector<vector<int>>&dp,vector<int>&prices)\n {\n if(idx==prices.size())\n {\n return 0;\n }\n if(dp[idx][buy]!=-1)\n {\n return dp[idx][buy];\n }\n int profit=0;\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int helper(vector<int> &prices , int buy , int index , vector<vector<int>> &dp){\n if(index >= prices.size()){\n return 0;\n }\n if(dp[index][buy]!=-1){\n return dp[index][buy];\n }\n int profit = 0;\n if(buy){\...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int solve(vector<int>&nums, int idx, int flag, vector<vector<int>>&dp){\n if(idx>=nums.size())return 0;\n\n if(dp[idx][flag]!=-1)return dp[idx][flag];\n\n int take=0,notTake=0;\n if(flag){\n take = -nums[idx] + solve(nums,idx+1,0,dp);\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int getTheMaxProfit(vector<int> &prices, vector<vector<int>> &dp, int itr, int buy) {\n if(itr == prices.size())\n return 0;\n if(dp[itr][buy] != -1)\n return dp[itr][buy];\n\n int maxProfit = 0;\n if(buy)\n maxProf...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int solve(int i, vector<int>& prices, int n, bool buy, vector<vector<int>>& dp) {\n if(i == prices.size()) {\n return 0;\n }\n\n if(dp[i][buy] != -1) {\n return dp[i][buy];\n }\n\n if(buy) {\n int take = -pri...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n int helper(int ind,int buy,vector<int>&price,int n,vector<vector<int>>&dp){\n\n if(ind>=n) return 0;\n if(dp[ind][buy]!=-1) return dp[ind][buy];\n int sum=0;\n\n if(buy){\n sum=max(-price[ind]+helper(ind+1,0,price,n,dp),0+helper(ind+1,1,...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "class Solution {\npublic:\n\n int solve(vector<int>&prices, int in,int b,int n,vector<vector<int>>&dp)\n {\n if(in==n)\n {\n return 0;\n }\n\n if(dp[in][b]!=-1)\n {\n return dp[in][b];\n }\n\n int profit=0;\n\n if(b)\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
3
{ "code": "//JAI SIYA RAM\nclass Solution {\npublic:\n int solve(int buy,int i,vector<int>& nums,int n,vector<vector<int>> &dp){\n if(i==n-1){\n if(!buy) return nums[n-1];\n return 0;\n }\n if(dp[i][buy]!=-1) return dp[i][buy];\n if(buy)\n return dp[i][bu...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& nums) {\n int n = nums.size();\n int dp[n+1][5];\n for(int i=0; i<5; i++) {\n dp[n][i] = 0;\n }\n for(int i=0; i<n; i++) {\n dp[i][4] = 0;\n }\n \n for(int i=n-1; i>=0; i-...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
0
{ "code": "class Solution {\npublic:\n \n int maxProfit(vector<int>& prices) {\n \n int dp[prices.size() + 1][5];\n \n \n for(int day = (int) prices.size();day >= 0;day--){\n \n for(int transactionsLeft = 0;transactionsLeft <= 4;transactionsLeft++){\n ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
0
{ "code": "class Solution {\npublic:\n int t[100001][2][3];\n int n;\n int solve(vector<int>& A, int in , int n , bool flag , int k){\n if(in>=n){\n return 0;\n }\n if(k==0){\n return 0;\n }\n if(t[in][flag][k]!=-1){\n return t[in][flag][k];\...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n=prices.size();\n long long dp[n+1][2][3];\n // dp[n][0]=dp[n][1]=0;\n for(int i=0;i<3;i++){\n dp[n][0][i]=0;\n dp[n][1][i]=0;\n }\n for(int i=0;i<=n;i++){\n ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int M = prices[0], m = prices[0], max_profit = 0, n = prices.size();\n vector<int> dp_head;\n for(int i=0; i<n; i++){\n if(prices[i] < m){\n m = prices[i];\n M = prices[i];\n ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n vector<int> preBestSellStock(vector<int> &prices){\n int ans = 0;\n vector<int>suffixSell;\n int currSell = INT_MIN;\n for(int i = prices.size() - 1;i >= 0;i--){\n if(prices[i] > currSell){\n currSell = prices[i];\n ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int n;\n int dp[100002][2][2];\n int solve(vector<int>& prices,int i,int hold,int k){\n if(i>=n || k==0)return 0;\n int& ans=dp[i][hold][k];\n if(ans!=-1)return ans;\n if(hold==1){\n ans=max(solve(prices,i+1,1,k),solve(prices,i+1,0...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int dps[100002][4];\n int dp(int index, int step, vector<int>&prices){\n if(index==prices.size()-1){\n if(step==1||step==3){\n return prices[index];\n }\n return 0;\n }\n if(dps[index][step]!=-1)\n ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "int dp[2][100010][3];\nclass Solution {\npublic:\n\nint f(int ind,int k ,int flag,vector<int>& prices){\n \n if(ind==prices.size()){\n return 0;\n }\n int ans=-1e9;\n if(dp[flag][ind][k]!=-1)\n return dp[flag][ind][k];\n if(flag){\n int op1=(k-1 >=0)?f(ind+1,k-1,1-flag,pric...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n\n int dp[100001][2][3];\n\n int f(int idx, bool hold, int time, vector<int>& prices){\n if(idx==prices.size()) return 0;\n if(time>=2) return 0;\n if(dp[idx][hold][time]!=-1) return dp[idx][hold][time];\n int ans = 0;\n if(hold) {\n ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int dp[100100][3][4];\n int solve(vector<int>& prices, int index, bool canBuy, int limit){\n if (limit ==0) return 0;\n if (index == prices.size()) return 0;\n if(dp[index][canBuy][limit]!=-1) return dp[index][canBuy][limit];\n int profit=0;\n\n...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n\n int dp[100005][6][2];\n int f(vector<int>& prices, int k ,int si , bool flag){\n \n if(si==prices.size()) return 0;\n\n if(dp[si][k][flag]!=-1) return dp[si][k][flag];\n int ans = INT_MIN;\n\n ans = f(prices,k,si+1,flag);\n\n if(...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "int dp[100100][2][3];\nint done[100100][2][3];\nint n;\n\nint rec(int level,int bs,int cnt,vector<int>& prices){\n if(cnt > 2) return -1e9;\n if(level==n){\n return 0;\n }\n\n if(done[level][bs][cnt]) return dp[level][bs][cnt];\n\n int ans = 0;\n // buy\n if(bs == 1)\n an...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n vector<int> func(vector<int>&nums){\n int n = nums.size();\n vector<int>ans;\n int mini = INT_MAX, maxi = INT_MIN;\n for(int i=0;i<n;i++){\n if(nums[i] < mini) mini = nums[i];\n ans.push_back(nums[i] - mini);\n }\n f...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\nprivate:\n int ans(int i, bool buy_condition, int buy_no, int sell_no, vector<int>& prices, int n, int dp[][2][3][3])\n {\n if (i == n)\n return 0;\n if (dp[i][buy_condition][buy_no][sell_no] != -1)\n return dp[i][buy_condition][buy_no][sell_no];\...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int dp[5][100005][5];\n int f(vector<int>& prices,int k,int idx,bool flag){\n if(idx == prices.size()) return 0;\n if(dp[k][idx][flag]!=-1) return dp[k][idx][flag];\n //avoid stocks of that day\n int ans = f(prices,k,idx+1,flag);\n\n //in...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size();\n // find the maximum profit by day x on left side and from day x on the right side.\n //find the maximum.\n // calculate the minimums and best trades so far.\n vector<int> minimu...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n void printVec(vector<int> vec) {\n for(int i=0;i<size(vec);i++)\n cout << vec[i] << \" \";\n cout << endl;\n }\n int maxProfit(vector<int>& prices) {\n vector<int> left, right;\n int mx=0,mn=prices[0],n=size(prices),cur=0;\n ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n std::vector<int> memo;\n std::vector<int> min0;\n std::vector<int> mins;\n int num;\n\n\n int readMemo(int pos, int trans) const {\n return memo[pos+num*(trans)];\n }\n\n void writeMemo(int pos, int trans, int val) {\n memo[pos+num*(trans)] = v...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size();\n vector<vector<int>> buy(3,vector<int>(n+1,-100001));\n vector<vector<int>> sell(3,vector<int>(n+1,0));\n for(int i = 1;i<=2;i++){\n for(int j = 1;j<=n;j++){\n ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int rcrsn(vector<int>& prices,vector<vector<vector<int>>>& dp,int i,int buy,int trans,int n){\n if(i==n || trans==0) return 0;\n if(dp[trans][buy][i]!=-1) return dp[trans][buy][i];\n if(!buy){\n dp[trans][buy][i]= max(-prices[i]+rcrsn(prices,dp...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\n // int solve(vector<int>& prices, int i, int f, int k, vector<vector<vector<int>>>& dp) {\n // if (i >= prices.size() || k >= 2) return 0;\n\n // if (dp[k][f][i] != -1) return dp[k][f][i];\n\n // int buy = 0, notBuy = 0, sell = 0, notSell = 0;\n // if (f) {...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int recurse(vector<int>& prices, int transactionLeft, int day, vector<vector<int>>&dp){\n if(day >= prices.size())return 0;\n\n if(transactionLeft == 0)return 0;\n\n if(dp[transactionLeft][day]!=-1)return dp[transactionLeft][day];\n\n int ans1 = re...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "#define vii vector<vector<int>>\n#define vi vector<int>\nclass Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size();\n vi aHead(5 , 0);\n\n for(int i = n - 1; i >= 0; i--){\n vi curr(4 , 0);\n for(int tran = 3; tran >= 0; tran--){\...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int solve(bool buy,int i,vector<int>& prices,int c,vector<vector<vector<int>>> &dp){\n if(i==prices.size() || c==-1){\n return 0;\n }\n \n int op1=0;\n int op2=0;\n \n \n if(buy){\n if(dp[1][c][i]!=...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size();\n if(n==1) return 0;\n if(n==2){\n if(prices[1]>=prices[0]) return prices[1]-prices[0];\n else return 0;\n }\n int **dp = new int*[n];\n for(int i = 0...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int dp[1000001][2][3];\n int solve(int i,int buy,int cap,vector<int>&prices,int n){\n if(cap==0) return 0;\n if(i==n) return 0;\n\n if(dp[i][buy][cap]!=-1) return dp[i][buy][cap];\n int profit=0;\n if(buy){\n return dp[i][buy][...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n\n int findProfit(vector<int>& prices, vector<vector<vector<int>>>& memo, int i, bool buy, int k) {\n if (i >= prices.size()) return 0; \n if (k < 0) return 0; \n\n if (memo[k][buy][i] != -1) return memo[k][buy][i];\n\n int res; \n if (buy) r...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int solve(int idx, bool buy, vector<int>& prices, int allowed,\n vector<vector<vector<int>>>& dp) {\n if (allowed == 0 || idx == prices.size())\n return 0;\n if (dp[allowed][buy][idx] != -1)\n return dp[allowed][buy][idx];\n ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n dp.resize(3, vector<vector<int>>(2, vector<int>(prices.size(), -1)));\n return helper(prices, 0, 0, 2);\n }\n\nprivate:\n vector<vector<vector<int>>> dp;\n int helper(vector<int>& prices, int i, int held, int t) {\...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int solve(int index , vector<int>& prices , int buy,int ct,vector<vector<vector<int>>>&dp){\n if(index == prices.size() || ct == 3)return 0;\n if(dp[ct][buy][index] != -1)return dp[ct][buy][index];\n \n int a = solve(index+1,prices,buy,ct,dp);\n ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& a) {\n int n=a.size();\n // vector<vector<int>>dp(n+1,vector<int>(5,0));\n vector<int>prev(5,0);\n // return f(0,0,a,n,dp);\n for(int ind=n-1;ind>=0;ind--){\n vector<int>curr(5,0);\n for(int t...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n \n int maxProfit(vector<int>& prices) {\n int n=prices.size();\n \n vector<int> next(5,0);\n\n for(int i=n-1;i>=0;i--){\n vector<int> cur(5,0);\n for(int flag=3;flag>=0;flag--){\n if(flag%2==0){\n ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int sum = 0, sum_before = 0, sum_after = 0, sum_result = 0, sum_between = 0, sum_form_max = 0;\n auto prices_copy = prices;\n int cnt = 0;\n vector<int> result;\n\n auto min_iter = std::m...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size();\n\n vector<int>dp(n+1, 0);\n list<int>s;\n for(int i=n-1;i>=0;i--){\n while(!s.empty() && s.front()<=prices[i]){\n s.pop_front();\n }\n in...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n // cout.tie(nullptr);\n int n=prices.size();\n vector<vector<int>> dp(n+1,vector<int>(4,0));\n int pf;\n\n for(int i=n-1;i>=0;i--)\n ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "auto __unsync_ios_stdio = ios::sync_with_stdio(false);\nauto __untie_cin = cin.tie(nullptr);\n\nclass Solution {\npublic:\n\n int maxProfit(vector<int>& prices) {\n if (prices.empty()) return 0; // Edge case: If prices array is empty\n\n int n = prices.size();\n vector<vector<int>> ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\nint f(vector<int> prices,int i,int k,int buy,auto dp){\n if(k==0)return 0;\n if(i==prices.size())return 0;\n if(dp[i][k][buy]!= -1)return dp[i][k][buy];\n int profit=0;\n if(buy){\n profit= max(f(prices,i+1,k,1,dp),-prices[i]+f(prices,i+1,k,0,dp));\n }\n ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n // int maxProfit(vector<int>& prices) {\n // int n = prices.size();\n // if(n<=1)\n // {\n // return 0;\n // }\n // if(n==2)\n // {\n // if(prices[0]< prices[1])\n // return prices[1]- prices[0];\n ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n\n int all(int k, vector<int>& prices,int current, vector<vector<vector<int>>> &m,int i){\n if(i>=prices.size()){\n return 0;\n }\n if(m[k][current][i]!=-1){\n return m[k][current][i];\n }\n int ans = 0;\n if(k>0 ...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int Solve(vector<int>& prices, bool buy, int times, int ind, vector<vector<vector<int>>>& dp)\n {\n if(ind == prices.size())\n return 0;\n\n if(dp[buy][times][ind] != -1)\n return dp[buy][times][ind];\n\n if(buy)\n {\n i...
123
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
1
{ "code": "class Solution {\npublic:\n int maxProfitHelper(vector<int>& prices, int curridx, int buy, \n vector<vector<vector<int>>> &dp, int k) {\n if (k >= 2) {\n return 0;\n }\n if (curridx >= prices.size()) {\n return 0;\n }\n if (...