educative.io

Educative

Simplified Solution

A user suggested the following solution:

public static Tuple findBuySellStockPrices(int[] array) {
    //TODO: Write - Your - Code
    Tuple < Integer, Integer > result = new Tuple < Integer, Integer > (-1, -1);
    int buyPrice = array[0];
    int profit = Integer.MIN_VALUE;
    for (int i = 1; i < array.length; i++) {
        if (array[i] - buyPrice > profit) {
            profit = array[i] - buyPrice;
            result = new Tuple < Integer, Integer > (buyPrice, array[i]);
        }
        buyPrice = Math.min(buyPrice, array[i]);
    }
    return result;
}