✅ Stocks buy and sell | DP | Easy understanding  ✅
Hi,\n\nThere are lot of solutions already available, but reason behind writing this solution is this question is tagged as \'Easy\' but most of the solutions ar
Hi,\n\nThere are lot of solutions already available, but reason behind writing this solution is this question is tagged as \'Easy\' but most of the solutions are not looking easy it looks like \'Medium\'. So thought to write a easy solution which is easy for everyone.\n\nI picked up DP approach, because its easy to relate to.\n\n**Intution:**\nInorder to get maximum profit we will see what is the minimum rate before today that we can buy. \n\n**Steps:**\n1. Define a DP array of size equal to array length\n2. Iterate through the given array and see the minimum prices till that array store it in the DP array -> dp[i] = Math.min(dp[i-1], prices[i]);\n3. Find which day has maximum difference compared to the DP array\n4. Return the result\n\nKindly upvote and spread joy..\n\n```\n\tpublic int maxProfit(int[] prices) {\n\t\tint res=0;\n\t\tint[] dp = new int[prices.length];\n\t\tdp[0]=prices[0];\n\t\tfor(int i=1; i<prices.length; i++){\n\t\t\tdp[i] = Math.min(dp[i-1], prices[i]);\n\t\t}\n\t\tfor(int i=0; i<prices.length; i++){\n\t\t\tres = Math.max(res, prices[i]-dp[i]);\n\t\t}\n\t\treturn res;\n\t}
['Dynamic Programming', 'Java']