Java 6 lines, 4 lines, 2 lines solutions
I only count the new lines of code that I've written, i.e. everything except for the method definition and the method closing bracket.\n\nProper Java - 6 lines:
I only count the new lines of code that I've written, i.e. everything except for the method definition and the method closing bracket.\n\nProper Java - 6 lines:\n\n    public int maxProfit(int[] prices) {\n         int min = Integer.MAX_VALUE, max = 0;\n         for (int i = 0; i < prices.length; i++) {\n             min = Math.min(min, prices[i]);\n             max = Math.max(max, prices[i] - min);\n         }\n         return max;\n     }  \n\nProper Java with shortcuts - 4 lines:\n  \n    public int maxProfit(int[] prices) {\n        int min = Integer.MAX_VALUE, max = 0;\n        for (int i = 0; i < prices.length; i++)\n            max = Math.max(max, prices[i] - (min = Math.min(min, prices[i])));\n        return max;\n    }\n\nJava 8 streams - 2 lines:\n\n    int min = Integer.MAX_VALUE;\n    public int maxProfit(int[] prices) {\n        return Arrays.stream(prices).map(i -> i - (min = Math.min(min, i))).max().orElse(0);\n    }
[]