1475. 商品折扣后的最终价格

Approach 1,直接遍历

class Solution {
    public int[] finalPrices(int[] prices) {
        int n = prices.length;
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            int pi = prices[i];
            res[i] = pi;
            for (int j = i+1; j < n; j++) {
                if (prices[j] <= pi) {
                    res[i] = pi - prices[j];
                    break;
                }
            }
        }
        return res;
    }
}

Approach 2,单调栈

Last Updated:
Contributors: jesse