Fully explained all buy and sell problems C++ || Recursive || Memoization || Minor difference
To solve this problem just start from basic similar problem. After solving these peoblems we wil reach at final this problem.\n\nI have discussed all the proble
**To solve this problem just start from basic similar problem. After solving these peoblems we wil reach at final this problem.**\n\n``` I have discussed all the problems by recursive and memoization approach , so that everyone can figure out minor differences between the questions :)```\n\n---------------------------------------------------------------------------------------------------------------\n---------------------------------------------------------------------------------------------------------------\n**Recursive Solution and memoize solutions:**\n\n1. https://leetcode.com/problems/best-time-to-buy-and-sell-stock/\n\n   **You can do at most one transaction**\n\n\n```\nclass Solution {\npublic:\n    int find(vector<int> &prices, int i,int k,bool buy,vector<vector<int>> &v)\n    {\n        //if we have no stock or we have no chance of transaction(k=0)\n        if(i>=prices.size()||k<=0) return 0;\n        if(v[i][buy]!=-1) return v[i][buy];\n        \n        if(buy)  //if we are buying then next time we will sell else next time we will buy\n        {        //-prices[i], because bought stock of prices[i], expend money\n            \n           return v[i][buy]=max(-prices[i]+find(prices,i+1,k,!buy,v),find(prices,i+1,k,buy,v)); \n        }\n        else    //it\'s time to sell , now decrease k, we have done 1 transaction\n        {       //+prices[i], because we now gain (i.e) sell our stock at rate of prices[i]\n           return v[i][buy]=max( prices[i]+find(prices,i+1,k-1,!buy,v),find(prices,i+1,k,buy,v)); \n        }\n    }\n    int maxProfit(vector<int>& prices) {\n        \n        int n=prices.size(); \n        vector<vector<int>> v(n,vector<int> (2,-1));\n        //passing here buy=1 because we will first buy then sell\n        //we can do atmost k=1 transaction\n        return find(prices,0,1,1,v);\n    }\n};\n```\n\n---------------------------------------------------------------------------------------------------------------\n---------------------------------------------------------------------------------------------------------------\n2. https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/\n\n    **You can do as many transactions as you like**\n\n```\nclass Solution {\npublic:\nint find(int ind,vector<int> &v,bool buy,vector<vector<int>> &memo)\n{    \n    if(ind>=v.size()) return 0;\n    if(memo[ind][buy]!=-1) return memo[ind][buy];\n    \n    if(buy) //if we are buying then next time we will sell else next time we will buy\n    {      //-prices[i], because bought stock of prices[i], expend money, !buy because next time sell\n        \n       return memo[ind][buy]=max(-v[ind]+find(ind+1,v,!buy,memo),find(ind+1,v,buy,memo));  \n    }\n    else   //it\'s time to sell \n    {      //+prices[i], because we now gain (i.e) sell our stock at rate of prices[i]\n        \n       return memo[ind][buy]=max(v[ind]+find(ind+1,v,!buy,memo),find(ind+1,v,buy,memo));  \n    }\n     \n}\n    int maxProfit(vector<int>& prices) {\n        \n        int n=prices.size();\n        if(n<2) return 0;\n        vector<vector<int>> v(n+1,vector<int>(2,-1));\n        //passing here buy=1 because we will first buy then sell \n        return find(0,prices,1,v); \n    }\n};\n```\n \n---------------------------------------------------------------------------------------------------------------\n---------------------------------------------------------------------------------------------------------------\n 3.  https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/\n\n      **You may complete at most two transactions.**\n\n```\nclass Solution {\npublic:\n    int find(vector<int> &prices,int ind,bool buy,int c,vector<vector<vector<int>>> &memo)\n    {   \n        //if buy =1 means we have to buy now\n        //else we have to sell now\n        if(ind>=prices.size()||c>=2) return 0; //counter\n        if(memo[ind][buy][c]!=-1) return memo[ind][buy][c];\n        if(buy) //now we can either buy prices[i] or we can skip it and try next to buy\n        {\n            return memo[ind][buy][c]=max(-prices[ind]+find(prices,ind+1,!buy,c,memo),find(prices,ind+1,buy,c,memo));\n        }\n        else  //now we can either sell prices[i] or we can skip it and try next to sell\n        {\n            return memo[ind][buy][c]=max(prices[ind]+find(prices,ind+1,!buy,c+1,memo),find(prices,ind+1,buy,c,memo));\n        }\n        \n    }\n    int maxProfit(vector<int>& prices) {\n        //here we can do maximum two transaction\n        //Use 3-D vector because here three states i,k,buy/sell\n        vector<vector<vector<int>>> memo(prices.size(),vector<vector<int>>(2,vector<int>(2,-1)));\n          \n       return find(prices,0,1,0,memo); \n    }\n};\n \n```\n\n---------------------------------------------------------------------------------------------------------------\n---------------------------------------------------------------------------------------------------------------\n4. https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/\n\n     **You may complete at most k transactions.**\n\n```\nclass Solution {\npublic:\n    int find(vector<int> &prices,int ind,bool buy,int c,int k,vector<vector<vector<int>>> &memo)\n    {   //if buy =1 means we have to buy now\n        //else we have to sell now\n        if(ind>=prices.size()||c>=k) return 0; //counter\n        \n        else if(memo[ind][buy][c]!=-1) return memo[ind][buy][c];\n         \n        if(buy) //now we can either buy prices[i] or we can skip it and try next to buy\n        {\n        return memo[ind][buy][c]=max(-prices[ind]+find(prices,ind+1,!buy,c,k,memo),find(prices,ind+1,buy,c,k,memo));\n        }\n        else  //now we can either sell prices[i] or we can skip it and try next to sell\n        {\n        return memo[ind][buy][c]=max(prices[ind]+find(prices,ind+1,!buy,c+1,k,memo),find(prices,ind+1,buy,c,k,memo));\n        }\n        \n    }\n    int maxProfit(int k, vector<int>& prices) {\n        \n        //edge case we are not able to pick 2k points from n points, which means \n        //we will not reach the limit no matter how we try. \n        //if the price of day i arise, buy the stock in i-1th day and sell it at ith day.\n        if (2 * k > prices.size()) {\n            int res = 0;\n            for (int i = 1; i < prices.size(); i++) {\n                res += max(0, prices[i] - prices[i - 1]);\n            }\n            return res;\n        }\n        \n         //here we can do maximum k transaction\n        vector<vector<vector<int>>> memo(prices.size()+1,vector<vector<int>>(2,vector<int>(k+1,-1)));\n        return find(prices,0,1,0,k,memo); \n    }\n};\n```\n\n---------------------------------------------------------------------------------------------------------------\n---------------------------------------------------------------------------------------------------------------\n\n**Hit \uD83D\uDC4D if you like it :)**
['Dynamic Programming', 'Recursion', 'Memoization', 'C', 'C++']