id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
0
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n set<string> occurences;\n set<string> result;\n if(s.length() < 10) return {};\n\n string substring;\n for(int i = 9; i < s.length(); i++)\n { \n substring = s.substr(...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n map<string,int>mp;\n string temp=s.substr(0,10);\n mp.insert({temp,1});\n vector<string>res;\n int p1=1,n=s.size();\n while(p1<=n-10){\n temp=s.substr(p1,10)...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n const int SUBSTR_LEN = 10;\n vector<string> ans;\n \n if(s.length() < SUBSTR_LEN) return ans;\n\n map<string,int> mp;\n for(int i = 0; i <= s.length() - SUBSTR_LEN; ++i) {\n ...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s)\n {\n vector<string> ret;\n map<string, int> M;\n\n int i;\n for (i = 0; i+9 < s.length(); i++)\n {\n string sub = s.substr(i, 10);\n if(M.find(sub) != M.end())\n ...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n if (s.size() < 10) return {};\n map<string, int> sequences;\n string curr_s;\n auto it = s.begin();\n for(; it < next(s.begin(), 10); ++it) {\n curr_s.push_back(*it);\n ...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n map<string,int> arr;\n if(s.size() < 10)\n return {};\n for(int i = 0; i < s.size()-9; ++i){\n string str = s.substr(i,10);\n arr[str]++;\n }\n \n ...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n long long int calculateHash(std::string s){ // string is of length 10\n long long int power_of_4 = 1;\n long long int hash = 0;\n\n std::map<char,int> convert_to_int;\n convert_to_int['A'] = 0;\n convert_to_int['T'] = 1;\n convert_to_...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n int size = s.size();\n\n if (size < 10) return {};\n\n vector<string> result;\n map<string, int> seen;\n\n for (int i = 0; i <= size - 10; i++) {\n string seq = s.substr(i, 10)...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n map<string,int> arr;\n if(s.size() < 10)\n return {};\n for(int i = 0; i < s.size()-9; ++i){\n string str = s.substr(i,10);\n arr[str]++;\n }\n \n ...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n map<string,int> m;\n vector<string> ans;\n if(s.size()<10)\n {\n return ans;\n }\n \n for(int i=0;i<s.size()-9;i++)\n {\n m[s.substr(i,10)]++;\n...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n map<string,int> arr;\n if(s.size() < 10)\n return {};\n for(int i = 0; i < s.size()-9; ++i){\n string str = s.substr(i,10);\n arr[str]++;\n }\n \n ...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n map<string, int> freqs;\n\n string cur = \"\";\n\n for(int i = 0; i < s.length(); i++)\n {\n cur += s[i];\n if(cur.length() == 10)\n {\n freqs[cur...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n map<string,int>mpp;\n int n=s.size();\n vector<string>ans;\n string curr=\"\";\n for(int i=0;i<n && i<10;i++){\n curr+=s[i];\n }\n mpp[curr]++;\n for(int i...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) \n {\n int left=0;\n int right=0;\n string ans=\"\";\n map<string,int>mpp;\n while(left < s.size() && right < s.size())\n {\n ans+=s[right];\n if(right -left +...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n int n = s.size();\n if (n < 10) {\n return {};\n }\n bitset<20> hash = 0;\n for (int i = 0; i < 10; ++i) {\n hash <<= 2;\n switch (s[i]) {\n ca...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n int d = 26;\n int prime = 101;\n int computeHash(const string &s, int length) {\n\t\tlong long hash = 0;\n\t long long multiplier = 1;\n\t for(int i = length-1; i >= 0; i--) {\n\t\t hash =(hash + (s[i]- 'A' +1) * multiplier);\n // cout<<s[i]<<\" \"<<...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n int hashVal(char c) {\n switch (c) {\n case 'A': return 0;\n case 'C': return 1;\n case 'G': return 2;\n case 'T': return 3;\n default: return 0;\n }\n }\n\n vector<string> findRepeatedDnaSequences(str...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n if (s.length() <= 10) { return {}; }\n\n unordered_map<string, int> cnt;\n for (int i = 0; i <= s.length()-10; i++) {\n string sub = s.substr(i, 10);\n cnt[sub]++;\n }\n\n ...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n unordered_map<string,int>mpp;\n vector<string>ans;\nif(s.length()<10) return ans;\n int i=0;\n while(i<s.length()-9){\n string str=s.substr(i,10);\n mpp[str]++;\n i++;\n }\n\n f...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n if (s.length() < 10) return {};\n\n unordered_map<string, int> mp;\n vector<string> ans;\n int l = 0, r = 10;\n\n while ( r <= s.length() ) {\n string temp = s.substr(l, 10);\n...
187
<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p> <ul> <li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li> </ul> <p>When s...
1
{ "code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n\n unordered_map<string , int > mp;\n\n vector<string > ans ;\n\n if(s.size()<10) return {};\n\n for(int i = 0;i<=s.size()-10; i++){\n\n string s1 = s.substr(i, 10);\n\n if(...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
0
{ "code": "class Solution {\npublic:\n // space opt\n int maxProfit(int k, vector<int>& prices) {\n int n = prices.size();\n vector<vector<int>> after(2, vector<int>(k + 1, 0));\n vector<vector<int>> curr(2, vector<int>(k + 1, 0));\n int profit = 0;\n for (int i = n - 1; i >= ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
0
{ "code": "class Solution {\n int maxProfitRec(vector<int> &prices, int index, int buy, int maxTransactions){\n if(index == prices.size() || maxTransactions == 0) return 0;\n\n if(buy){\n return max(maxProfitRec(prices, index + 1, !buy, maxTransactions) - prices[index],\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
0
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int l=prices.size();\n vector<vector<int>> curr(k+1,vector<int>(2,0));\n vector<vector<int>> prev(k+1,vector<int>(2,0));\n for(int i=l-1;i>-1;i--){\n for(int j=k-1;j>-1;j--){\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
0
{ "code": "class Solution {\n int findMaxProfit(vector<int>&prices,bool bought,int index,int k)\n {\n if(index==prices.size() || k==0)return 0;\n //we can either buy or sell or both\n if(bought==false)//we haven't bought anything\n {\n return max( findMaxProfit(prices,fals...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
0
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n \n int n = prices.size();\n \n int dp[ n+1 ][ k +1 ][ 2 ];\n\n for( int i = 0 ; i<=n ;i++ )\n {\n for( int j =0;j <=k; j++ )\n {\n dp[i][j][0] = -10000...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
0
{ "code": "class Solution {\npublic:\n int dp[1001][2][101];\n int profit(int ind,int buy,int k,vector<int> & nums){\n if(k==0)return 0;\n if(ind>=nums.size())return 0;\n if(dp[ind][buy][k]!=-1) return dp[ind][buy][k];\n int pr=0;\n if(buy==0){\n pr=max(-nums[in...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
0
{ "code": "class Solution {\npublic:\n int solve(vector<int> &prices, int operationIdx, int day, int &maxOperations, int memo[1002][202]) {\n if (day == prices.size())\n return 0;\n\n if (operationIdx == maxOperations)\n return 0;\n\n if (memo[day][operationIdx] != -1)\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
0
{ "code": "class Solution {\npublic:\nint kk;\nvector<int>v;\nint dp[1000+1][205];\n int rec(int id,int rem){\n if(id==v.size())return 0;\n if(dp[id][rem]!=-1)return dp[id][rem];\n int ans=0;\n ans=max(ans,rec(id+1,rem));\n if(rem<kk*2)ans=max(ans,v[id]*(rem%2? 1:-1)+rec(id+1,rem...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n int dp[1001][3][101];\n int profit(int i, vector<int>& prices, int count, int bought){\n if(i == prices.size() || count == 0)\n return 0;\n if(dp[i][bought][count] != -1)\n return dp[i][bought][count];\n else{\n int ans...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "\nclass Solution {\npublic:\nint n;\nint dp[1001][3][101];\nint solve(int ind,int buy,int cap,vector<int>& prices){\n if(cap==0 || ind==n )\n return 0;\n \n \n\n if(dp[ind][buy][cap]!=-1) return dp[ind][buy][cap];\n\n int profit=0;\n \n if(buy){\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\nint t[1001][3][101];\nint solve(int i,vector<int>& prices,bool buy,int k){\n if(i==prices.size()||k==0)return 0;\n int profit=0;\n if(t[i][buy][k]!=-1)return t[i][buy][k];\n if(buy==1){\n profit=max(-prices[i]+solve(i+1,prices,0,k),solve(i+1,prices,1,k));\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n vector<vector<int>> dp(k+1,vector<int>(prices.size(),0));\n int ebp;\n for(int ii = 1; ii<=k; ii++)\n {\n ebp = prices[0]; // ebp = price - profit so far\n for(int jj = 1; jj<price...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n if (prices.size() < 2)\n return 0; \n vector<vector<int>> dp (k+1, vector<int> (prices.size())); \n \n for (int i=1; i<=k; i++) {\n int buy = -prices[0]; \n for (int j=1...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n vector<vector<int>> dp(prices.size(), vector<int>(k + 1, 0));\n \n for(int i = 0; i < prices.size(); i++) {\n int b = i;\n\n for(int j = i - 1; j >= 0; j--) {\n for(int p =...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n // dp[index][ith] -> max \n // index=0, return 0, 1 based indexing \n // this is an interesting problem\n // alright bitch\n\n int rec(int index,int k,vector<int>&prices,vector<vector<int>>&dp){\n if(index<0){\n return 0;\n }\n if(k...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/\n\nclass Solution {\n\npublic:\n\n int f(const vector<int>& prices, int N, int i, int k) {\n\n // base case\n\n if (i == N || k == 0) {\n return 0;\n }\n\n // recursive case\n\n // f(i, k) ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n int arr[105][2][1005];\n int help(vector<int>& prices,int k,bool can, int idx){\n //if((! can) && idx== prices.size()) return INT_MIN;// if at end and still holding on a stock\n if (k==0 || (idx==prices.size())) return 0;// if can is false we need to sell fir...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n int arr[105][2][1005];\n int help(vector<int>& prices,int k,bool can, int idx){\n //if((! can) && idx== prices.size()) return INT_MIN;// if at end and still holding on a stock\n if (k==0 || (idx==prices.size())) return 0;// if can is false we need to sell fir...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n int dp[1005][105][2];\n int helper(vector<int>&prices,int n,int idx,int k,int on){\n if(idx==n || k==0) return 0;\n if(dp[idx][k][on]!=-1) return dp[idx][k][on];\n int buy=0;\n int sell=0;\n int leave=0;\n if(on==0){\n b...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n int dp[1003][2+3][100+3];\n int f(vector<int>& prices, int idx, int buy_sell, int transcation_number)\n {\n if(idx==prices.size() || transcation_number==0) return 0; //once the transcation limit is over -> No further Call\n if(dp[idx][buy_sell][transcation...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n long long vis[1004][3][103];\n vector<int>arr;int n;\n long long recru(int i,int bought,int k){\n if(i==n)return 0;\n if(k==0)return 0;\n if(vis[i][bought][k]!=-1) return vis[i][bought][k];\n long long ans;\n long long skip=recru(i+1,b...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int n=prices.size();\n // vector<vector<int>> dp(n,vector<int>(4,0));\n // return memo(dp,prices,0,4);\n\n vector<int> prev(2*k+1,0);\n...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution\n{\npublic:\n int maxProfit(int k, const vector<int>& P)\n {\n int n = P.size();\n vector<vector<int>> F(k, vector<int>(n));\n vector<vector<int>> M(k, vector<int>(n));\n M[0][n - 1] = P[n - 1];\n for (int j = n - 2; j >= 0; --j)\n {\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int n = prices.size();\n\n vector<int> hold(k+1, INT_MIN / 2);\n vector<int> sold(k+1, INT_MIN / 2);\n\n hold[0] = 0;\n sold[0] = 0;\n\n for (int i = 0; i < n; i++)\n {\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\nprivate:\n int solveMem(int index, int operationNo, int k, vector<int>& prices, vector<vector<int>>& dp) {\n if (index == prices.size())\n return 0;\n if (operationNo == 2 * k)\n return 0;\n\n if (dp[index][operationNo] != -1)\n ret...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\nprivate:\n int solveTab(int k, vector<int>& prices) {\n int n = prices.size();\n vector<vector<int>> dp(n + 1, vector<int>(2 * k + 1, 0));\n\n for (int index = n-1; index >= 0; index--) {\n for (int operationNo = 0; operationNo < 2 * k; operationNo++) {\...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n int f(int ind, int transno, vector<int> &prices, int n, int k, vector<vector<int>> &dp){\n if(ind==n || transno==2*k) return 0;\n if(dp[ind][transno]!=-1) return dp[ind][transno];\n if(transno%2==0){\n return dp[ind][transno] = max(-prices[ind]...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n int f(int ind, int transno, vector<int> &prices, int n, int k, vector<vector<int>> &dp){\n if(ind==n || transno==2*k) return 0;\n if(dp[ind][transno]!=-1) return dp[ind][transno];\n if(transno%2==0){\n return dp[ind][transno] = max(-prices[ind]...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int n = prices.size();\n vector<vector<vector<int>>> dp(n+1, vector<vector<int>>(2, vector<int>(k+1, 0)));\n int profit = 0;\n for(int i=n-1;i>=0;i--)\n {\n for(int buy=0;buy<=1;buy++)...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
1
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int n = prices.size();\n\n vector<vector<int>> sell(n, vector<int>(k + 1, 0));\n vector<vector<int>> buy(n, vector<int>(k + 1, 0));\n\n buy[0][0] = -prices[0];\n sell[0][0] = 0;\n\n for (i...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\nint solve(vector<int>&prices,int buy,int ind,int k, vector<vector<vector<int>>>&dp){\n int n=prices.size();\n if(k==0){\n return 0;\n }\n if(ind>=n){\n return 0;\n }\n if(dp[buy][ind][k]!=-1){\n return dp[buy][ind][k];\n }\n int profi...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n // Recursive + Memoization Solution: T.C = O(n), S.C = O(n)\n int solveMemo(int index, int isBuy, int transactions, vector<int>& prices, vector<vector<vector<int>>>& dp) {\n // Base Condition\n if ( index == prices.size() || transactions == 0 )\n r...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\nprivate:\n int solve(int i, int n, vector<int>& prices, bool canBuy, int k1, vector<vector<vector<int>>>& dp,int k) {\n if (i >= n || k1 == k) \n return 0;\n \n if (dp[i][canBuy][k1] != -1) \n return dp[i][canBuy][k1];\n \n int...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n // Recursive + Memoization Solution: T.C = O(n), S.C = O(n)\n int solveMemo(int index, int isBuy, int transactions, vector<int>& prices, vector<vector<vector<int>>>& dp) {\n // Base Condition\n if ( index == prices.size() || transactions == 0 )\n r...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\n int solve(vector<int>&prices,int d,bool ans,int k,vector<vector<vector<int>>>&v){\nif(d==prices.size()){\n return 0;\n}\nif(v[d][ans][k]!=-1){\n return v[d][ans][k];\n}\nint g=0;\nint h=0;\nif(ans==false and k>0){\ng=max(-prices[d]+solve(prices,d+1,true,k-1,v),solve(prices,d+1...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n\n int Solve(int ind,int trans,int n,vector<int> &prices,vector<vector<int>> &dp)\n {\n if(ind==n || trans==0) return 0;\n if(dp[ind][trans]!=-1) return dp[ind][trans];\n if(trans%2==0) \n {\n int take = -prices[ind]+Solve(ind+1,trans-1,n...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int Solve(int ind, int trans, int n, vector<int>& prices,\n vector<vector<int>>& dp) {\n if (ind == n || trans == 0)\n return 0;\n if (dp[ind][trans] != -1)\n return dp[ind][trans];\n if (trans % 2 == 0) {\n i...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int Solve(int ind, int trans, int n, vector<int>& prices,\n vector<vector<int>>& dp) {\n if (ind == n || trans == 0)\n return 0;\n if (dp[ind][trans] != -1)\n return dp[ind][trans];\n if (trans % 2 == 0) {\n i...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int n=prices.size();\n vector<vector<vector<int>>>dp(n+1,vector<vector<int>>(2,vector<int>(2*k,0)));\n for(int ind=n-1;ind>=0;ind--){\n for(int buy=0;buy<=1;buy++){\n for(int cap=1;ca...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\nlong f(int ind,int buy ,int n , vector<int>&prices ,vector<vector<vector<int>>>&dp,int cap){\n if(ind==n || cap==0)return 0;\n if(dp[ind][buy][cap]!=-1)return dp[ind][buy][cap];\n long profit=0;\n if(buy){\n profit=max(-prices[ind] + f(ind+1,0,n,prices,dp,cap) ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\nint f(int ind,int buy,int cap,vector<int>& prices, vector<vector<vector<int>>>&dp){\n int n=prices.size();\n if(ind==n || 2*cap==buy)\n return 0;\n if(dp[ind][buy][cap]!=-1)\n return dp[ind][buy][cap];\n int profit;\n if(buy){\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int findProfit(int i, int sell, int remaining, vector<int> &prices, vector<vector<vector<int>>> &dp){\n if(i == prices.size() || remaining == 0){\n return 0;\n }\n if(dp[i][sell][remaining] != INT_MIN){\n return dp[i][sell][remaining...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\n int f(int index, int buy, vector<int>& prices,int cap, vector<vector<vector<long>>> &dp) {\n if(cap==0) return 0;\n if (index == prices.size())\n return 0;\n if(dp[index][buy][cap]!=-1) return dp[index][buy][cap];\n long profit = 0;\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int f(int ind, int buy, int trans, vector<int>& prices,vector<vector<vector<long>>>&dp){\n if(trans<0) return INT_MIN+100000;\n if(ind == prices.size()){\n return 0;\n }\n if(dp[ind][buy][trans]!=-1) return dp[ind][buy][trans];\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n long long fs(int n, int index, bool buy, int count, vector<int>& prices, vector<vector<vector<long long>>>& dp) {\n if (index == n || count == 0)\n return 0;\n \n long long profit = 0;\n \n if (dp[index][buy][count] != -1)\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n long long fs(int n, int index, bool buy, int count, vector<int>& prices, vector<vector<vector<long long>>>& dp) {\n if (index == n || count == 0)\n return 0;\n \n long long profit = 0;\n \n if (dp[index][buy][count] != -1)\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n/*\n int MaxProfitRecursion(vector<int> &prices,int index,int buy,int count){\n if(index>=prices.size()){\n return 0;\n }\n if(count==0){\n return 0;\n }\n int Profit=0;\n if(buy==1){ \n int ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int n = (int) prices.size();\n vector<vector<vector<int>>> dp(n, vector<vector<int>> (2, vector<int> (2 * k, -1)));\n function<int(int, int, int)> f = [&] (int i, int buy, int txns) {\n if (i == n |...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\nprivate:\n int tabulation(int k, int n, vector<int>& prices, vector<vector<vector<int>>>dp){\n for(int i=n-1; i>=0;i--){\n for(int buy=0; buy<2;buy++){\n for(int count=1;count<=k;count++){\n int profit = 0;\n if(buy...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\nprivate:\n int tabulation(int k, int n, vector<int>& prices, vector<vector<vector<int>>>dp){\n for(int i=n-1; i>=0;i--){\n for(int buy=0; buy<2;buy++){\n for(int count=1;count<=k;count++){\n int profit = 0;\n if(buy...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int solve(vector<int> &prices, int n, bool bought, int k, vector<vector<vector<int>>> &dp) {\n if(n == prices.size() || k == 0)\n return 0;\n \n if(dp[n][bought][k] != -1)\n return dp[n][bought][k];\n\n int buyOrSell;\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n#define ll long long\n ll recur(int k, vector<int>& prices, ll ind, bool bt, vector<vector<vector<ll>>>&dp){\n ll prf = 0;\n if(k == 0 || ind == prices.size())return 0;\n if(dp[ind][bt][k] != -1)return dp[ind][bt][k];\n if(bt){\n ll sell ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int solve(int i,vector<int> &nums,int buy,int k,vector<vector<vector<int>>>&dp){\n if(i == nums.size()){\n return 0;\n }\n if(dp[i][buy][k] != INT_MAX){\n return dp[i][buy][k];\n }\n //start exploring the options\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "const int N = 1e3+5;\nint dp[N][N][2];\n\nclass Solution {\npublic:\n int maxProfit(int L, vector<int>& a) {\n int n = a.size();\n for (int i = 0; i <= n+1; i++) {\n for (int j = 0; j <= n+1; j++) {\n for (int k = 0; k < 2; k++) {\n dp[i][j][k] ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int n = prices.size();\n vector<vector<vector<long long>>> memo(2, vector<vector<long long>>(k + 1, vector<long long>(n + 1)));\n for (int index = 0 ; index <= n ; index++)\n memo[0][0][index] = mem...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n vector<vector<vector<int>>> dp(1, vector<vector<int>>(2, vector<int>(k+1, 0)));\n for(int day=prices.size()-1; day>=0; day--){ \n vector<vector<vector<int>>> ndp(1, vector<vector<int>>(2, vector<int>(k+1...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n vector<vector<vector<int>>> dp(1, vector<vector<int>>(2, vector<int>(k+1, 0)));\n for(int day=prices.size()-1; day>=0; day--){ \n vector<vector<vector<int>>> ndp(1, vector<vector<int>>(2, vector<int>(k+1...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int n = prices.size() + 1;\n int m = n < 2*k+1 ? n : 2*k+1;\n std::cout << m << '\\n';\n std::vector<std::vector<int>> profits(m, std::vector<int> (n, -99));\n std::vector<std::vector<int>> choic...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int n = prices.size();\n if (n == 0) return 0; // If no prices are available, no profit can be made\n \n vector<vector<long>> dp(n, vector<long>(2 * k + 1, -1)); // Initialize dp table with -1\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\n using ll=long long;\n ll f(ll i, ll x, ll k, ll n, vector<int>&a, vector<vector<vector<ll>>>&dp){\n if(i>=n || k<=0) return 0;\n \n\n if(dp[i][x][k]!=-1) return dp[i][x][k];\n ll ans=0;\n if(x){\n ans=max({ans,-a[i]+f(i+1, 0, k, n, a, ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\n\n int calculate(int index, int txns, vector<int>& prices, vector<unordered_map<int, int>>& dp) {\n\n if(txns == 0 || index >= prices.size())\n return 0;\n\n if(dp[txns].find(index) != dp[txns].end()) {\n return dp[txns][index];\n }\n\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\n int dp[1005][3][1005];\n int n ;\n int helper(int idx ,int buy , int cnt , vector<int>&prices ){\n if(idx>=prices.size() || cnt==0)return 0;\n if(dp[idx][buy][cnt]!=-1)return dp[idx][buy][cnt];\n if(buy ==0){\n \n int take = -prices[i...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int size = prices.size();\n int* c = new int[size * size];\n int* b = new int[size * (k+1)];\n int* d = new int[size * (k+1)];\n int diff;\n if (size == 1) return 0;\n\n for(int ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int dp[1010][1010][3];\n int rec(int level , int cnt , int buy , int k , vector<int> &prices){\n if(cnt >= k)return 0;\n if(level >= prices.size())return 0;\n if(dp[level][cnt][buy] != -1)return dp[level][cnt][buy];\n\n int ans = 0;\n if(...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n template <typename T> class Array2D {\n T* data;\n unsigned N, M;\n\n public:\n Array2D(size_t n, size_t m) : N(n), M(m) { data = new T[N * M]; }\n ~Array2D() { delete[] data; }\n inline T* operator[](size_t i) { return &data[i * M]; }\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int dp[10005][3][105];\n int func(int idx,int buy,int k,vector<int>& prices){\n if(idx==prices.size())return 0;\n if(k==0)return 0;\n\n if(dp[idx][buy][k]!=-1)return dp[idx][buy][k];\n\n\n if(buy){\n int buyprofit=-prices[idx]+func(id...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "struct Key {\n size_t i;\n// size_t j;\n size_t k;\n bool operator==(const Key &other) const\n {\n return (i == other.i) && (k == other.k);\n// return (i == other.i) && (j == other.j) && (k == other.k);\n }\n};\n\nstruct KeyHash {\n std::size_t operator() (const Key& key)...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int n = prices.size();\n vector<vector<vector<int>>>dp(k,vector<vector<int>>(n,vector<int>(2,0)));\n for(int i =n-1;i>=0;i--){\n if(i==n-1){\n for(int j=0;j<k;j++){\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int n = prices.size();\n vector<vector<vector<int>>>dp(k,vector<vector<int>>(n,vector<int>(2,0)));\n for(int i =n-1;i>=0;i--){\n if(i==n-1){\n for(int j=0;j<k;j++){\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int solve(int k, vector<int>& prices,int index,int buy,vector<vector<vector<int>>> &dp){\n int n=prices.size();\n if(index==n){\n return 0;\n }\n if(k==0){\n return 0;\n }\n if(dp[k][index][buy]!=-1){\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int func(int k, vector<int> &prices, int i, bool buyFlag, int count, vector<vector<vector<int>>> &dp){\n if(i==prices.size() || count==k){\n return 0;\n }\n if(dp[i][count][buyFlag]!=-1){\n return dp[i][count][buyFlag];\n }\n ...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int util(int k, vector<int>& prices, int i, int t, bool flag, vector<vector<vector<int>>>& dp) {\n if (i == prices.size()) {\n return 0;\n }\n if (t == k) {\n return 0;\n }\n if (dp[i][t][flag] != -1) return dp[i][t][fl...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "//JAI SIYA RAM\nclass Solution {\npublic:\n int maxProfit(int k, vector<int>& nums) {\n int n=nums.size();\n vector<vector<vector<int>>> dp(n+1,vector<vector<int>>(k+1,vector<int>(2,0)));\n for(int i=n-1;i>=0;i--){\n for(int buy=0;buy<2;buy++){\n for(int tr...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int N = prices.size();\n vector<vector<int>> val(N, vector<int>(N,0));\n int dp[N][k+1];\n int best = 0;\n \n for (int i=0; i<N; i++) \n dp[i][0] = 0;\n for (int i=0; i<N; i++)...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int N = prices.size();\n vector<vector<int>> val(N, vector<int>(N,0));\n int dp[N][k+1];\n int best = 0;\n \n for (int i=0; i<N; i++) \n dp[i][0] = 0;\n for (int i=0; i<N; i++)...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int n=prices.size();\n vector<vector<vector<int>>> dp(n,vector<vector<int>>(k+1,vector<int>(2,-1)));\n return f(0,0,k,prices,dp);\n \n }\n int f(int ind,int bought,int k,vector<int>& prices,vector...
188
<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, and an integer <code>k</code>.</p> <p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k<...
3
{ "code": "class Solution {\npublic:\n int maxProfit(int k, vector<int>& prices) {\n int n = prices.size();\n\n // Solve special cases\n if (n <= 0 || k <= 0) {\n return 0;\n }\n\n if (k * 2 >= n) {\n int res = 0;\n for (int i = 1; i < n; i++) {\n...
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Ex...
0
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n int n = nums.size();\n k = k % n; // Normalize k to avoid unnecessary rotations\n \n // Reverse the entire array\n reverse(nums.begin(), nums.end());\n // Reverse the first k elements\n ...
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Ex...
2
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n int n = nums.size();\n k = k%n;\n if(k==0){\n return;\n }\n int d=k;\n int temp[k];\n for(int i=0;i<k;i++){\n temp[i] = nums[n-d];\n d--;\n }\n ...