Explain Like I'm 5: O(n) Java Solution Solved with Only Basic Intuition
Too often the solution is overcomplicated with terms like\n- DP\n- Kadane Algorithm\n\nYou can solve this problem with only simple logical reasoning to come up 
Too often the solution is overcomplicated with terms like\n- DP\n- Kadane Algorithm\n\nYou can solve this problem with only simple logical reasoning to come up with the optimal solution.\n\nOur input: [7,1,5,3,6,4]\n\nWe know:\n-  You can potentially sell stock on any day\n-  When we sell stock on a certain day, we want to have bought stock when it was at it\'s lowest prior to that day\n\nWe can simulate selling selling the stock every day and see which day would yield the max profit.\n\nWe do this by going through the array day by day while:\n- Keeping track of lowest value\n- Simulating the maximum profit if we were to sold that day\n\n```\nclass Solution {\n    public int maxProfit(int[] prices) {\n        int lowestUntilThisPoint = Integer.MAX_VALUE;\n        int maxProfit = -1;\n        \n        for (int price: prices) {\n            if (price < lowestUntilThisPoint) {\n                lowestUntilThisPoint = price;\n            }\n            int profit = price - lowestUntilThisPoint;\n            if (profit > maxProfit) \n                maxProfit = profit;\n        }\n        return Math.max(maxProfit, 0);\n    }\n}\n```\n
['Java']