id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 2 | {
"code": "class Solution {\npublic:\n map<vector<int>,int> dp;\n int fun(vector<int>& n, vector<int>& m, int s, int e, int i) {\n if(i==m.size()) return 0;\n if(dp.find({s,e,i})!=dp.end()) {\n return dp[{s,e,i}];\n }\n return dp[{s,e,i}]=max(m[i]*n[s] + fun(n,m,s+1, e, i+... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 2 | {
"code": "class Solution {\npublic:\n int rec(vector<int>& nums,int start,int end,vector<int>& mul,int ind,vector<vector<int>>& dp){\n if(ind>=mul.size()) return 0;\n \n if(dp[start][ind] !=-1001) return dp[start][ind];\n //take the start elemnt\n int s=(mul[ind] * nums[start]) + rec(nums,sta... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 2 | {
"code": "\nclass Solution {\npublic:\n vector<vector<int>>dp;\n int sz;\n int solve(vector<int>&nums,int i,int n,vector<int>&mult,int ind,int m){\n if(ind==m) return 0;\n if(i>n) return 0;\n\n if(dp[i][sz-n]!=-1001) return dp[i][sz-n];\n\n int left=mult[ind]*nums[i]+solve(nums,i... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 2 | {
"code": "class Solution {\npublic:\n int func(int i,int s,int e,vector<int>& nums,vector<int>& multipliers,vector<vector<int>>& dp)\n {\n if(i >= multipliers.size()) \n return 0; \n if(dp[s][i] != -1001)\n return dp[s][i];\n\n int t1 = func(i+1,s+1,e,nums,multiplie... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 2 | {
"code": "class Solution {\npublic:\n int rec(vector<int>& nums,int start,int end,vector<int>& mul,int ind,vector<vector<int>>& dp){\n if(ind>=mul.size()) return 0;\n \n if(dp[start][ind] !=-1001) return dp[start][ind];\n //take the start elemnt\n int s=(mul[ind] * nums[start]) + rec(nums,sta... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 2 | {
"code": "class Solution {\npublic:\n int go(int i,int s,int e,vector<int> &nums,vector<int> &mu,vector<vector<int>> &dp){\n if(i>=mu.size()){\n return 0;\n }\n if(dp[s][i]!=-1){\n return dp[s][i];\n }\n\n int t1 = go(i+1,s+1,e,nums,mu,dp)+ nums[s]*mu[i];\n... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp;\n int Util(int i, int j, vector<int>& nums, vector<int>& multipliers, int m) {\n if(m==multipliers.size() || i>j) return 0;\n if(dp[i][m]!=-1001) return dp[i][m];\n return dp[i][m] = max(nums[i]*multipliers[m] + Util(i+1,j,nums,... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp;\n int Util(int i, vector<int>& nums, vector<int>& multipliers, int m) {\n if(m==multipliers.size()) return 0;\n if(dp[i][m]!=-1001) return dp[i][m];\n int j = nums.size()-m+i-1;\n return dp[i][m] = max(nums[i]*multipliers... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 2 | {
"code": "class Solution {\npublic:\n\n int helper(vector<int>& nums, vector<int>& multipliers, int i, int j, int k, unordered_map<int,unordered_map<int,unordered_map<int,int>>>&dp){\n if(k==multipliers.size()) return 0;\n\n if(dp.find(i)!= dp.end() and dp[i].find(j)!= dp[i].end() and dp[i][j].find(... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 2 | {
"code": "class Solution {\npublic:\n\n int helper(vector<int>& nums, vector<int>& multipliers, int i, int j, int k, unordered_map<int,unordered_map<int,unordered_map<int,int>>>&dp){\n if(k==multipliers.size()) return 0;\n\n if(dp.find(i)!= dp.end() and dp[i].find(j)!= dp[i].end() and dp[i][j].find(... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 2 | {
"code": "class Solution {\n vector<int> nums;\n vector<int> multipliers;\n\n std::map<std::vector<int>, int> dp;\n std::map<std::vector<int>, bool> calculated;\n\n int solve(int first_num, int last_num, int mi) {\n std::vector<int> index {first_num, last_num, mi};\n if(calculated[index]... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\n vector<int> nums;\n vector<int> multipliers;\n\n std::map<std::vector<int>, int> dp;\n std::map<std::vector<int>, bool> calculated;\n\n int solve(int first_num, int last_num, int mi) {\n std::vector<int> index {first_num, last_num, mi};\n if(calculated[index]... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, vector<int>& multipliers) {\n // sizeof nums = n\n // sizeof multipliers = m\n // begin with score 0, \n // perform m operations,\n // ith operation\n // 1. choose x from start/end\n // 2. ... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\n vector<vector<vector<int>>> dp;\n int helper(vector<int>& nums, vector<int>& mul, int l, int r, int i) {\n if(i >= mul.size()) return 0;\n if(l >= nums.size() || r < 0) return 0;\n if(dp[l][nums.size()-r-1][i] != -1) return dp[l][nums.size()-r-1][i];\n i... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\npublic:\n\n int solve(vector<int>& nums, vector<int>& multipliers,vector<vector<int>>& dp,int i, int j,int idx){\n int n = nums.size(),m = multipliers.size();\n if(idx>=m){\n return 0;\n }\n if(dp[idx][i]!=-1){\n return dp[idx][i];\n ... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\npublic:\n\n int dp(int i, int left, vector<vector<int>> & ans, vector<int>& nums, vector<int>& multipliers){\n //base case\n if(i == multipliers.size()) return 0;\n\n if(ans[i][left] == -1){\n int right = nums.size() - 1 - (i - left);\n int m ... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\npublic:\n\n int dp(int i, int left, vector<vector<int>> & ans, vector<int>& nums, vector<int>& multipliers){\n //base case\n if(i == multipliers.size()) return 0;\n\n if(ans[i][left] == -1){\n int right = nums.size() - 1 - (i - left);\n int m ... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, vector<int>& mults) {\n int N = nums.size(), M = mults.size(), mult;\n vector<vector<int>> dp(M, vector<int>(N+1, 0));\n\n mult = mults[M-1];\n for (int i = 0; i < N; i++) {\n // If #s cut off from lef... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, vector<int>& multipliers) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n \n vector<vector<int>> memo(multipliers.size(),\n vector<int>(nums.size(), -1));\n\n return getMax... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\npublic:\n int solve(int i, int l, vector<int>& nums, vector<int>& multipliers, int n, int m, vector<vector<int>>& dp) {\n if (i == m) return 0;\n int r = n - 1 - (i - l); \n if(l > r) return 0;\n\n if (dp[i][l] != -1) return dp[i][l];\n\n int op1 = nums[l] * multiplie... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\npublic:\n int solve(int i, int l, vector<int>& nums, vector<int>& multipliers, int n, int m, vector<vector<int>>& dp) {\n if (i == m) return 0;\n int r = n - 1 - (i - l);\n\n if (dp[i][l] != -1) return dp[i][l];\n\n int op1 = nums[l] * multipliers[i] + solve(i + 1, l + 1, n... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "const static auto speedup = [] () {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n \n return 1;\n} ();\n\nclass Solution {\npublic:\n int maximumScore(vector<int>& nums, vector<int>& multipliers) {\n this->nums = nums, this->multipliers = m... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\n int maxScore(int i, int start, vector<int>& nums, vector<int>& multipliers, vector<vector<int>> &dp){\n if(i==multipliers.size()){\n return 0;\n }\n int end = (nums.size()-1) - (i-start);\n if(start>end){\n return 0;\n }\n ... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\n int maxScore(int i, int start, vector<int>& nums, vector<int>& multipliers, vector<vector<int>> &dp){\n if(i==multipliers.size()){\n return 0;\n }\n int end = (nums.size()-1) - (i-start);\n if(start>end){\n return 0;\n }\n ... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\npublic:\n long long solveUsingRecursion(vector<int>& nums, vector<int>& multipliers, int l, int r, int index) {\n if(index >= multipliers.size() || l>r) {\n return 0;\n }\n long long left = multipliers[index]*nums[l] + solveUsingRecursion(nums, multiplie... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\n int func(vector<int>& nums, vector<int>& multi, int n, int m, int ind, int l, int r, vector<vector<int>> &memo)\n {\n if(ind==m)\n {\n return 0;\n }\n if(memo[l][ind]!=-1) return memo[l][ind];\n int a=max(multi[ind]*nums[l]+func(nums, m... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\npublic:\n int m;\n int n;\n int solve(int start, int end ,int ope, vector<int>& nums, vector<int>& multipliers, vector<vector<int>> &dp)\n {\n \n if(ope == m ) return 0;\n\n if(dp[start][ope] != -1) return dp[start][ope];\n\n int one = multipliers[o... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\npublic:\n\n int dp(int mIdx, int processed, vector<int>& nums, vector<int>& multipliers, vector<vector<int>>& memo) {\n if(mIdx == multipliers.size()) return 0;\n\n if(memo[mIdx][processed] == 0) {\n int right = nums.size() - 1 - (mIdx - processed);\n ... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\npublic:\n int helper(int x, int y, vector<vector<int>> &dp, vector<int>& nums, vector<int>& multipliers, int i, int m){\n if(i==m)return 0;\n if(dp[x][i]!=-1)return dp[x][i];\n return dp[x][i] = max(nums[x]*multipliers[i] + helper(x+1,y,dp,nums,multipliers,i+1,m),\... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\npublic:\n int func(vector<int>&nums, int i, int m,vector<int>&mul , vector<vector<int>>&dp){ int j=(nums.size()-1-(m-i));\n if(m>=mul.size()){\n return 0;\n }\n if(dp[i][m]!=-1){\n ... |
1,896 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n >= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</cod... | 3 | {
"code": "class Solution {\npublic:\n int func(vector<int>& nums, int i, int m, vector<int>& mul,\n vector<vector<int>>& dp) {\n int j = (nums.size() - 1 - (m - i));\n if (m >= mul.size()) {\n return 0;\n }\n if (dp[i][m] != -1) {\n return dp[i][m];\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int sold = -INT_MAX;\n int held = -INT_MAX;\n int reset = 0;\n for (int i = 0; i < prices.size(); i++) {\n int soldcpy = sold;\n int resetcpy = reset;\n int heldcpy = held;\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size();\n if (n == 0) return 0;\n int hold = INT_MIN; \n int sell = 0; \n int cooldown = 0; \n for (int i = 0; i < n; ++i) {\n int prev_hold = hold; \n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n if (prices.size() == 1) return 0;\n int m = max((-1) * prices[0], (-1) * prices[1]), p1 = 0, p2 = 0;\n p2 = max(0, prices[1] - prices[0]);\n if (prices.size() == 2) return p2;\n\n for (int i = 2; i < pr... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "// Idea: in this case, we have 4 case might show up\n// buy, comes from cool state, goto holding or sell\n// hodding, comes from buy stage, go to sell stage\n// sell, comes from buy stage or hodding stage, go to cool stage\n// cool, comes from sell stage or cool stage, go to cool stage or buy stage.\n\n// ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "class Solution {\npublic:\n \n int maxProfit(vector<int>& arr) {\n int n = arr.size();\n \n if (n == 0) return 0; // Edge case: no transactions possible\n\n // Use scalar variables instead of vectors for optimization\n int ahead1_not_buy = 0, ahead1_buy = 0;\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n if (prices.size() == 1) return 0;\n int m = max((-1) * prices[0], (-1) * prices[1]), p1 = 0, p2 = 0;\n p2 = max(0, prices[1] - prices[0]);\n if (prices.size() == 2) return p2;\n\n for (int i = 2; i < pr... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size();\n vector<int> after1(2, 0);\n vector<int> after2(2, 0);\n vector<int> curr(2, 0);\n for(int ind = n-1; ind>=0; ind--) {\n int buy = -prices[ind] + after1[0];\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int size = prices.size();\n int buyCost = -prices[0];\n int sellCost = 0;\n int coolPeriod = 0;\n\n for(int i = 1; i<size; i++){\n int newBuyCost = coolPeriod - prices[i];\n int ne... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& a) {\n int n=a.size();\n vector<int> nxt(2),cur(2),c(2);\n for(int i=n-1;i>=0;i--){\n for(auto &b:{0,1}){\n if(b&1){\n cur[b]=max(-a[i]+nxt[0],nxt[1]);\n }else{\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "class Solution {\npublic:\nint f(int ind, int buy, vector<int> &arr, vector<vector<int>> &dp)\n{\n if (ind >= arr.size())\n {\n return 0;\n }\n if (dp[ind][buy] != -1)\n {\n return dp[ind][buy];\n }\n if (buy == 1)\n {\n return dp[ind][buy] = max(-arr[ind] + f(i... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "class Solution {public:\n int maxProfit(vector<int>& prices) {\n int n=prices.size();\n vector<int> front2(2,0);\n vector<int> front1(2,0);\n vector<int> cur(2,0);\n for(int ind=n-1;ind>=0;ind--){\n cur[1]= max(-prices[ind]+front1[0],\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size();\n vector<int> s0 (n, 0);\n vector<int> s1 (n, 0); s1[n - 1] = prices[n - 1];\n vector<int> s2 (n, 0);\n\n for(int i = n - 2; i >= 0; i--) {\n s0[i] = max(-prices[i] + s... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "class Solution {\npublic:\n int dp[5001][2]; // Memoization table\n \n int solve(int i, int holding, const vector<int>& prices) {\n if (i >= prices.size()) return 0;\n \n if (dp[i][holding] != -1) return dp[i][holding];\n \n int cooldown = solve(i + 1, holding, p... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n std::array<int[2], 5000> dp;\n //std::array<int, 500> hold_prices;\n dp[0][0] = 0;\n dp[0][1] = -prices[0];\n //hold_prices[0] = prices[0];\n for (int i = 1; i < prices.size(); i++) {\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "class Solution {\npublic:\n int dp[5001][2]; // Memoization table\n \n int solve(int i, int holding, const vector<int>& prices) {\n if (i >= prices.size()) return 0;\n \n if (dp[i][holding] != -1) return dp[i][holding];\n \n int cooldown = solve(i + 1, holding, p... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 0 | {
"code": "const int MAX = 5000 + 1;\nint memory[MAX][2][3];\nvector<int> prices;\n\nenum {\n\tNOTHING = 0, SELL = 1, BUY = 2\n};\n\nclass Solution {\npublic:\n\tint trade(int idx, int have_stock, int last_transaction) {\n\t\tif (idx == (int) prices.size())\n\t\t\treturn 0;\n\n\t\tauto &ret = memory[idx][have_stock][... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 1 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size();\n if (n == 0) return 0;\n\n // dp[i][0] represents the maximum profit on day i when not holding a stock\n // dp[i][1] represents the maximum profit on day i when holding a stock\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 1 | {
"code": "class Solution {\nprivate:\n int f(int ind,int buy,vector<int> &prices, int n,vector<vector<int>> &dp){\n if(ind>=n) return 0;\n if(dp[ind][buy]!=-1) return dp[ind][buy];\n if(buy){\n return dp[ind][buy]= max(-prices[ind]+f(ind+1,0,prices,n,dp),\n 0... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 1 | {
"code": "class Solution {\npublic:\nint f(int ind, int buy, vector<int> &arr, vector<vector<int>> &dp)\n{\n if (ind >= arr.size())\n {\n return 0;\n }\n if (dp[ind][buy] != -1)\n {\n return dp[ind][buy];\n }\n if (buy == 1)\n {\n return dp[ind][buy] = max(-arr[ind] + f(i... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 1 | {
"code": "class Solution {\nprivate:\n int f(vector<int>& prices,int ind,int bought) {\n if(ind >= prices.size()) return 0;\n int profit = 0;\n if(bought == 0) {\n profit = max(f(prices,ind+1,bought),-prices[ind] + f(prices,ind+1,1));\n }\n if(bought == 1) {\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 1 | {
"code": "class Solution {public:\n int maxProfit(vector<int>& prices) {\n int n=prices.size();\n vector<vector<int>> dp(n+2,vector<int> (2,0));\n for(int ind=n-1;ind>=0;ind--){\n dp[ind][1]= max(-prices[ind]+dp[ind+1][0],\n 0+dp[ind+1][1]);\... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 2 | {
"code": "class Solution {\npublic:\n int solve(int ind, bool buy,vector<int>& prices, vector<vector<int>>&dp){\n if(ind >= prices.size()){\n return 0;\n }\n\n if(dp[ind][buy] != -1 ) return dp[ind][buy];\n\n\n int profit = INT_MIN;\n if(buy){\n profit = m... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 2 | {
"code": "class Solution {\npublic:\n int dp(vector <int>& prices , int index , int buy , vector<vector<int>>&memo){\n if(index > prices.size()){ \n return 0 ; \n } \n\n if(memo[index][buy] != -1) return memo[index][buy] ; \n\n if(buy){\n int Buy = -1e9 ; \n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 2 | {
"code": "class Solution {\npublic:\n long 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\n long profit = 0;\n if (buy) {\n profit = max(-prices[ind] + f(ind + 1, 0, n, pri... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 2 | {
"code": "class Solution {\nprivate:\n int f(int ind,int buy,vector<int> &prices, int n,vector<vector<int>> &dp){\n if(ind>=n) return 0;\n if(dp[ind][buy]!=-1) return dp[ind][buy];\n if(buy){\n return dp[ind][buy]= max(-prices[ind]+f(ind+1,0,prices,n,dp),\n 0... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\nint f(vector<int>&prices,int n,bool buy,vector<vector<int>>&dp){\n if(n>=prices.size())return 0;\n if(dp[n][buy]!=-1)return dp[n][buy];\n //dont do anything\n int ans1=f(prices,n+1,buy,dp);\n if(buy){\n int ans2=f(prices,n+1,0,dp)-pri... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n// max profit \n// sell a stock cannot buy next day \n// buy at lowest sell at highest greedy won't work \n// profit = (sell - buy) or profit - buy andn profit + buy \n// dp \n// top down with recursion \n\n int helper(vector<int>& prices , int index , int holding , v... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "#include <vector>\n\nclass Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size();\n vector<vector<int>> memo(n, vector<int>(2, -1));\n return profit(prices, 0, 0, memo);\n }\n \nprivate:\n int profit(vector<int>& prices, int i, int holding, vect... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\n const int inf=1e7+1;\n int find(int ind,int b,vector<int>&v,vector<vector<int>>&dp)\n {\n if(ind>=v.size()) return 0;\n if(dp[ind][b]!=-inf) return dp[ind][b];\n int ans=find(ind+1,b,v,dp);\n if(b==1)\n {\n ans=max(ans,find(ind+1,0,v... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp;\n int f(int i,int buy,int c,vector<int>& prices,int n){\n if(i>=n) return 0;\n if(dp[i][buy]!=-1) return dp[i][buy];\n int ans=f(i+1,buy,c,prices,n);\n if(buy==0) {\n ans=max(ans,f(i+1,buy+1,c,prices,n)-prices[... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n //dp(i,cooldown,holding) i=index,cooldown=1 if(i-1) you sell holding =0\n //dp(i,0,0)= max(-prices[i]+dp(i+1,0,1),+dp(i+1,0,0)); //buy and ahead , nothing\n //dp(i,1,0)= dp(i+1,0,0); //cooldown so just move ahead\n //dp(i,0,1)=max(dp(i+1,0,1), +prices[i]+dp(i+1,1,0))... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int findMaxProfit(vector<int>&prices,vector<vector<int>>&dp,int ind, bool brought){\n if(ind>=prices.size())return 0;\n if(dp[ind][brought]!=-1)return dp[ind][brought];\n\n int maxProfit=0;\n if(brought==true){\n int buy=findMaxProfit(pr... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int f(int i,vector<int> &prices,vector<int> &dp){\n if(i>=prices.size()-1) return 0;\n if(dp[i]!=-1) return dp[i];\n dp[i]=0;\n int maxi = 0;\n for(int j = i+1;j<prices.size();j++){\n int b = prices[j]-prices[i];\n dp[i... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int dp[5001][2];\n int helper(vector<int>& prices,int i,int flag)\n {\n if(i>=prices.size()) return 0;\n if(dp[i][flag]!=-1) return dp[i][flag];\n int buy=0,sell=0,leave=0;\n if(flag) buy=helper(prices,i+1,0)-prices[i];\n if(!flag) sel... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n //dp(i,cooldown,holding) i=index,cooldown=1 if(i-1) you sell holding =0\n //dp(i,0,0)= max(-prices[i]+dp(i+1,0,1),+dp(i+1,0,0)); //buy and ahead , nothing\n //dp(i,1,0)= dp(i+1,0,0); //cooldown so just move ahead\n //dp(i,0,1)=max(dp(i+1,0,1), +prices[i]+dp(i+1,1,0))... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\nprivate: \n vector<vector<int>> memo;\n int N;\n vector<int> p;\npublic:\n int maxProfit(vector<int>& prices) {\n p = prices;\n N = p.size();\n vector<vector<int>> dp(N+2, vector<int>(2, 0));\n memo.assign(N, vector<int>(2, 0));\n /*\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\nprivate: \n vector<vector<int>> memo;\n int N;\n vector<int> p;\npublic:\n int maxProfit(vector<int>& prices) {\n p = prices;\n N = p.size();\n vector<vector<int>> dp(N+2, vector<int>(2, 0));\n memo.assign(N, vector<int>(2, 0));\n /*\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\nprivate: \n vector<vector<int>> memo;\n int N;\n vector<int> p;\npublic:\n int maxProfit(vector<int>& prices) {\n p = prices;\n N = p.size();\n vector<vector<int>> dp(N+2, vector<int>(2, 0));\n memo.assign(N, vector<int>(2, 0));\n /*\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int dp[5001][2];\n int solve(int i, vector<int> &prices, bool bought) {\n int n = prices.size();\n if (i >= n) return 0;\n if (dp[i][bought] != -1) return dp[i][bought];\n int profit = 0;\n int ignore = solve(i+1, prices, bought);\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n \nint solve(int i,int buy,int cooldown,vector<int>&prices, vector<vector<vector<int>>>&dp){\n if(cooldown==2 && buy==1)return 0;\n if(i==prices.size())return 0;\n if(dp[i][buy][cooldown]!=-1)return dp[i][buy][cooldown];\n if(buy){\n if(cooldown==1)\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int getAns(vector<int>& Arr, int n, int ind, int buy, int cap, vector<vector<vector<int>>>& dp) {\n // Base case\n if (ind >= n) return 0;\n\n // Check if the result is already computed\n if (dp[ind][buy][cap] != -1)\n return dp[ind][buy][cap];\n\n int p... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int f(int ind, int buy, int cool, vector<int>& prices,\n vector<vector<vector<int>>>& dp) {\n if (ind == prices.size())\n return 0;\n if (dp[ind][buy][cool] != -1)\n return dp[ind][buy][cool];\n if (cool != 1) {\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\n private:\n int solve(int i, int buy, int cooldown, vector<int>& prices, vector<vector<vector<int>>>& dp ){\n if(i >= prices.size()) return 0;\n if(dp[i][buy][cooldown] != -1) return dp[i][buy][cooldown];\n\n int nottake = solve(i+1, buy, 0, prices, dp);\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int helper(int ind , vector<int>&prices,int bought,vector<vector<int>> &dp){\n // buy\n if(ind >=prices.size()) return 0 ; \n if(dp[ind][bought] != -1) return dp[ind][bought] ; \n int x = 0 , y = 0 , z = 0 ; \n if(bought == 0) x = -prices[ind]+helper(ind+1,pric... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp;\n //cooldown 1 means cooldown laga hai\n //0 means u r free to buy\n int helper(int index, int canBuy, vector<int>& prices) {\n if (index >= prices.size()) {\n return 0;\n }\n\n if (dp[index][canBuy]!= -1) {\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\n using ll=long long;\n ll f(ll i, ll x, ll n, vector<int>&a, vector<vector<ll>>&dp){\n if(i>=n) return 0;\n \n\n if(dp[i][x]!=-1) return dp[i][x];\n ll ans=0;\n if(x){\n ans=max({ans,-a[i]+f(i+1, 0, n, a, dp), f(i+1, x, n, a, dp)});\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = (int) prices.size();\n vector<vector<int>> dp(n, vector<int> (2, -1));\n function<int(int, int)> f = [&] (int i, int buy) {\n if (i >= n) return 0;\n if (dp[i][buy] != -1) return dp[i][b... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\nmap< pair<int,bool>,int>mp;\n int solve( int i, bool canSell,vector<int>& prices ){\n if( i>= prices.size()) return 0;\n int p=0;\n if( mp.find({i,canSell})!=mp.end())return mp[{i,canSell}];\n if(!canSell )p= max( -prices[i]+ solve( i+1, true, prices) , solve( i+1, ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int> prices) {\n static const int MAX_SIZE = 5000;\n static int DP[MAX_SIZE + 2] = {0, 0};\n static int* dp = &DP[2];\n\n const int SIZE = prices.size();\n memset(dp, 0, SIZE << 2);\n\n map<int, deque<int>> val_to... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "#include <tuple>\n#include <unordered_map>\n#include <vector>\n#include <iostream>\n\nusing namespace std;\n\nclass Solution {\n private:\n long getKey(int i, int bought) {\n return (long)i << 1 | (long)bought;\n }\n\n // returns tha max amount of money that can be made starting from a ce... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\nprivate:\n int helper(vector<int>& prices, int i, bool buy, unordered_map<int, int>& memo) {\n if (i >= prices.size())\n return 0;\n\n int key = (i << 1) | static_cast<int>(buy);\n if (memo.find(key) != memo.end()) {\n return memo[key];\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "#include <tuple>\n#include <unordered_map>\n#include <vector>\n#include <iostream>\n\nusing namespace std;\n\nclass Solution {\n private:\n long getKey(int i, int bought) {\n return (long)i << 1 | (long)bought;\n }\n\n // returns tha max amount of money that can be made starting from a ce... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n unordered_map<long, int> dp;\n return dfs(0, true, prices, dp);\n \n }\n int dfs(int i, bool buying, vector<int>& prices, unordered_map<long, int>& dp){\n if (i >= prices.size()) return 0;\n long ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n map<pair<int,bool>,int> memo;\n return maxProfitMemo(prices, 0, false, memo);\n }\n\n int maxProfitMemo(vector<int>& prices, int day, bool holding, map<pair<int,bool>,int>& memo) {\n pair<int,bool> p = {day, ho... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n\n enum States\n {\n BUY = 0,\n SELL,\n COOL_DOWN\n };\n\n struct data \n {\n States state;\n int index;\n\n bool operator<(const data &d) const\n {\n return (index == d.index)? (state < d.state) : (index ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n\n enum States\n {\n BUY = 0,\n SELL,\n COOL_DOWN\n };\n\n struct data \n {\n States state;\n int index;\n\n bool operator<(const data &d) const\n {\n return (index == d.index)? (state < d.state) : (index ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int n;\n map<pair<int, int>, int> memo; \n int maxProfit(vector<int>& prices) {\n n = prices.size();\n return helper(0, prices, 2); \n }\n\n int helper(int i, vector<int>& prices, int prev) //0 holding stock, 1 need to cool-down, 2 can buy.\n {\... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic: map<pair<int, bool>, int> dp;\npublic:\n int maxProfit(vector<int>& prices) {\n return dfs(prices, 0, true);\n }\n\n int dfs(vector<int>& prices, int i , bool buying){\n if(i >= prices.size()) return 0;\n if(dp[{i, buying}]) return dp[{i, buying}];\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n \n int dfs(int i,int f,vector<int>& prices,map<pair<int,int>,int>& dp)\n {\n if(i>=prices.size())\n return 0;\n if(dp.count({i,f}))\n return dp[{i,f}];\n int c,b,s;\n c=dfs(i+1,f,prices,dp);\n if(f)\n {\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic: map<pair<int, bool>, int> dp;\npublic:\n int maxProfit(vector<int>& prices) {\n return dfs(prices, 0, true);\n }\n\n int dfs(vector<int>& prices, int i , bool buying){\n if(i >= prices.size()) return 0;\n if(dp[{i, buying}]) return dp[{i, buying}];\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int n;\n int mxprof(int i,vector<int> &prices,bool cooldown, bool buyable,vector<vector<vector<int>>> &dp)\n {\n if(i==n) return 0;\n if(dp[i][cooldown][buyable]!=-1) return dp[i][cooldown][buyable];\n int buy=0,sell=0,notxn;\n notxn=mxprof(i... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n\n struct hash_pair {\n size_t operator()(pair<int, int> p) const {\n size_t first = hash<int>()(p.first);\n size_t second = hash<int>()(p.second);\n return first ^ (second << 1);\n }\n };\n\n int maxProfit(vector<int>& pric... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n map<std::pair<int, bool>, int> d;\n\n std::function<int (int, int)> dfs; \n \n dfs = [&](int day, bool buying) {\n std::pair<int, bool> curr_pair(day, buying);\n\n if (day >= prices.size(... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n vector<unordered_map<int,int>> maps(4);\n int ans = max(dfs(0,0,maps,prices), dfs(0,2,maps,prices));\n return ans;\n }\n // 0 : buy\n // 1 : skip and have\n // 2 : skip and dont have\n // 3 : sell\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\nmap<int,map<bool,int>>dp;\npublic:\n int maxProfit(vector<int>& prices) {\n \n return dfs(prices,true,0);\n \n }\n int dfs(vector<int>& prices,bool buying,int i)\n {\n if(i>=prices.size())\n return 0;\n if(dp[i].count(buying))\n ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "\n\nclass Solution {\npublic:\n int rec(int index, bool is_buy, vector<int>& prices, map<int, map<bool, int>>& dp) {\n if (index >= prices.size()) return 0;\n if (dp[index][is_buy] > 0) return dp[index][is_buy];\n if (is_buy) {\n int sol = max(\n rec(index ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\n\n enum States\n {\n BUY = 0,\n SELL,\n COOL_DOWN\n };\n\n struct data \n {\n States state;\n int index;\n\n bool operator<(const data &d) const\n {\n return (index == d.index)? (state < d.state) : (index ... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "// Utility function for hashing pairs in unordered_map\nstruct hash_pair {\n template <class T1, class T2>\n size_t operator() (const pair<T1, T2>& p) const {\n auto hash1 = hash<T1>{}(p.first);\n auto hash2 = hash<T2>{}(p.second);\n return hash1 ^ hash2;\n }\n};\nstatic int s... |
309 | <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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll... | 3 | {
"code": "class Solution {\npublic:\nunordered_map<int,unordered_map<bool,int>> dp;\nint helper(vector<int>& prices,int i,bool buy)\n{\n if(i>=prices.size()) return 0;\n if(dp[i].count(buy))\n {\n return dp[i][buy];\n }\n if(buy)\n {\n return dp[i][buy]=max(helper(prices,i+2,!buy)+pri... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.