Easy & Simple Recursion + Memoization [DP] Technique. || Intuition of Buy and Sell Stock
Intuition Behind this approach . Buy and Sell Stock approach\n\n\n\nclass Solution {\npublic:\n    int dp[2][2][50001];\n    int solve(vector<int>& values,int i
[**Intuition Behind this approach . Buy and Sell Stock approach**](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/900050/Fully-explained-all-buy-and-sell-problems-C%2B%2B-oror-Recursive-oror-Memoization-oror-Minor-difference)\n\n\n```\nclass Solution {\npublic:\n    int dp[2][2][50001];\n    int solve(vector<int>& values,int index,int seen,int k)\n    {\n        if(index>=values.size() or k<=0)\n        {\n            if(k<=0)     //if we have comepleted seeing two monuments ,then return 0.\n                return 0;\n            else          //this basically means we are going out of index, in that case we will return INT_MIN\n                return INT_MIN;\n        }\n        if(dp[seen][k][index]!=-1)\n            return dp[seen][k][index];\n        if(seen)\n        {\n\t\t//first we will visit the monument,therefore including "values[index]+index]" and\n\t\t//making seen as opposite value,to signify that we have already seen one monument.\n\t\t//Time to see the final monument.\n            return dp[seen][k][index] = max(values[index]+index + solve(values,index+1,!seen,k) , solve(values,index+1,seen,k));\n        }\n        else\n        {\n\t\t//Time has come to see the final monument that\'s whymaking "k-1" which suignify \n\t\t//that we can see only 2 monuments at a time.\n\t\t//Including "values[index]-index" as mentioned in the question.\n            return dp[seen][k][index] = max(values[index]-index + solve(values,index+1,!seen,k-1) , solve(values,index+1,seen,k));\n        }\n    }\n    int maxScoreSightseeingPair(vector<int>& values) {\n        int n = values.size();\n        int start=0,seen=1;//we will first see the first monument therefore "seen=1"\n        int k=1; //signifies that we can see sightsee two monument at a time.. \n\t\t//(k=0 -> see one monument ,k=1 -> see the final monument)\n        memset(dp,-1,sizeof(dp));\n        return solve(values,start,seen,k);\n    }\n};\n```\n\n***Please Upvote***
['Dynamic Programming', 'Recursion', 'Memoization']