Simple O(n) Java solution
\n     public int maxProfit(int[] prices) {\n        int minPrice = prices[0];\n        int maxProfit = 0;\n        for(int price:prices){\n            minPrice
```\n     public int maxProfit(int[] prices) {\n        int minPrice = prices[0];\n        int maxProfit = 0;\n        for(int price:prices){\n            minPrice = Math.min(minPrice,price);\n            maxProfit = Math.max(maxProfit,price-minPrice);\n        }\n        return maxProfit;\n    }\n```
[]