2 approaches DP & Recursion - Time O(n) Space O(1)
\n// Time - O(n) Space - O(1)\nclass Solution {\n    public int maxProfit(int[] prices) {\n        if(prices == null || prices.length == 0) return 0;\n        r
```\n// Time - O(n) Space - O(1)\nclass Solution {\n    public int maxProfit(int[] prices) {\n        if(prices == null || prices.length == 0) return 0;\n        return getMaxProfit(prices, 0, Integer.MAX_VALUE, 0);\n    }\n    \n    public int getMaxProfit(int[] prices, int index, int minStockPrice, int maxDiff) {\n        if(prices.length == index) {\n            return maxDiff;\n        }\n         int earn =  prices[index] - minStockPrice;\n         maxDiff = Math.max(earn, maxDiff);\n         minStockPrice = Math.min(minStockPrice, prices[index]);\n        return getMaxProfit(prices, ++index, minStockPrice, maxDiff);\n    }\n}\n\n// Time - O(n) Space - O(1)\nclass Solution {\n    public int maxProfit(int[] prices) {\n        if(prices == null || prices.length == 0) return 0;\n        return getMaxProfit(prices);\n    }\n    \n    public int getMaxProfit(int[] prices) {\n        int minStockPrice = Integer.MAX_VALUE, maxDiff = 0;\n        for(int i = 0; i < prices.length; i++) {\n            int earn =  prices[i] - minStockPrice;\n            maxDiff = Math.max(earn, maxDiff);\n            minStockPrice = Math.min(minStockPrice, prices[i]);\n        }\n        return maxDiff;\n    }\n}\n```
['Dynamic Programming', 'Recursion', 'Java']