Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
541 B
2
Indexable
Never
public int findMax(List<Integer> prices) {
        int currentMax = prices.get(prices.size() - 1);
        List<Integer> maxes = new ArrayList<>();
        for (int i = prices.size() - 1; i >= 0; i--) {
            if (currentMax < prices.get(i)) {
                currentMax = prices.get(i);
            }
            maxes.add(currentMax);
        }
        int sum = 0;
        for (int i = 0; i < prices.size(); i++) {
            sum += maxes.get(prices.size() - i - 1) - prices.get(i);
        }
        return sum;
    }