LeetCode1475. 商品折扣后的最终价格🌟🌟🌟🌟🌟简单

课后作业

问题描述

原文链接:1475. 商品折扣后的最终价格

给你一个数组 prices ,其中 prices[i] 是商店里第 i 件商品的价格。

商店里正在进行促销活动,如果你要买第 i 件商品,那么你可以得到与 prices[j] 相等的折扣,其中 j 是满足 j > i 且 prices[j] <= prices[i] 的最小下标,如果没有满足条件的 j ,你将没有任何折扣。

请你返回一个数组,数组中第 i 个元素是折扣后你购买商品 i 最终需要支付的价格。

示例 1:

输入:prices = [8,4,6,2,3]
输出:[4,2,4,2,3]
解释:
商品 0 的价格为 price[0]=8 ,你将得到 prices[1]=4 的折扣,所以最终价格为 8 - 4 = 4 。
商品 1 的价格为 price[1]=4 ,你将得到 prices[3]=2 的折扣,所以最终价格为 4 - 2 = 2 。
商品 2 的价格为 price[2]=6 ,你将得到 prices[3]=2 的折扣,所以最终价格为 6 - 2 = 4 。
商品 3 和 4 都没有折扣。

示例 2:

输入:prices = [1,2,3,4,5]
输出:[1,2,3,4,5]
解释:在这个例子中,所有商品都没有折扣。

示例 3:

输入:prices = [10,1,1,6]
输出:[9,0,1,6]

提示:

  • 1 <= prices.length <= 500
  • 1 <= prices[i] <= 10^3

代码实现

Java

class Solution {
    public int[] finalPrices(int[] prices) {
        Stack<Integer> stack = new Stack();

        int[] res = new int[prices.length];
        for(int i = prices.length - 1; i >= 0; i--){
            while(!stack.isEmpty() && stack.peek() > prices[i]){
                stack.pop();
            }

            res[i] = stack.isEmpty() ? prices[i] : prices[i] - stack.peek();
            stack.push(prices[i]);

        }

        return res;

    }
}

Python

class Solution(object):
    def finalPrices(self, prices):
        """
        :type prices: List[int]
        :rtype: List[int]
        """
        stack = []
        res = [0] * len(prices)

        for i in range(len(prices)-1, -1, -1):
            while stack and stack[-1] > prices[i]:
                stack.pop()

            res[i] = prices[i] - stack[-1] if stack else prices[i]
            stack.append(prices[i])

        return res

C++

class Solution {
public:
    vector<int> finalPrices(vector<int>& prices) {
        stack<int> st;
        vector<int> res(prices.size());

        for(int i = prices.size()-1; i >= 0; i--){
            while(!st.empty() && st.top() > prices[i]){
                st.pop();
            }

            res[i] = st.empty() ? prices[i] : prices[i] - st.top();
            st.push(prices[i]);
        }

        return res;
    }
};

Go

func finalPrices(prices []int) []int {
    st := make([]int, 0)
    res := make([]int, len(prices))

    for i := len(prices)-1; i >= 0; i-- {
        for len(st) > 0 && st[len(st)-1] > prices[i] {
            st = st[:len(st)-1]
        }

        if len(st) == 0 {
            res[i] = prices[i]
        } else {
            res[i] = prices[i] - st[len(st)-1]
        }
        st = append(st, prices[i])
    }

    return res
}

发表回复

后才能评论

评论(5)

  • 笨笨小子 永久会员 2024年9月2日 下午12:54

    打卡

  • Mr.肖 永久会员 2024年4月5日 上午9:54
    class Solution {
    
        /**
        用单调栈的思想
        时间复杂度:遍历一遍数组 O(n)
        空间复杂度:单调栈最坏情况下,会装满 O(n)
         */
        public int[] finalPrices(int[] prices) {
            int n = prices.length;
            Stack<Integer> stack = new Stack();
    
            int[] res = new int[n];
    
            for(int i = n -1; i >= 0;i--){
                while(!stack.isEmpty() && stack.peek() > prices[i]){
                    stack.pop();
                }
                if(stack.isEmpty()){
                    res[i] = prices[i];
                }else{
                    res[i] = prices[i] - stack.peek();
                }
    
                stack.push(prices[i]);
            }
    
            return res;
        }
    }
    
  • uu 永久会员 2023年10月24日 下午2:51
    // 时间复杂度:O(n^2)
    // 空间复杂度:O(n)
    // class Solution {
    //     public int[] finalPrices(int[] prices) {
    //         int len = prices.length;
    //         int[] res = new int[len];
    //         for(int i = 0; i < len; i++) {
    //             for(int j = i+1; j < len; j++) {
    //                 if(prices[j] <= prices[i]) {
    //                     res[i] = prices[j];
    //                     break;
    //                 }
    //             }
    //         }
    //         for(int i = 0; i < len; i++) {
    //             prices[i] = prices[i] - res[i];
    //         }
    //         return prices;
    //     }
    // }
    
    // 时间复杂度:O(n)
    // 空间复杂度:O(n)
    class Solution {
        public int[] finalPrices(int[] prices) {
            Stack<Integer> stack = new Stack();
    
            int[] res = new int[prices.length];
            for(int i = prices.length - 1; i >= 0; i--){
                while(!stack.isEmpty() && stack.peek() > prices[i]){
                    stack.pop();
                }
                res[i] = stack.isEmpty() ? prices[i] : prices[i] - stack.peek();
                stack.push(prices[i]);
            }
            // prices [8,4,6,2,3]
            // stack [2,4,8]
            // res [4,2,4,2,3]
            return res;
        }
    }
    
  • 阿尚 普通 2023年8月19日 下午8:46
    class Solution {
        public int[] finalPrices(int[] prices) {
            int n = prices.length;
            int[] result = new int[n];
    
            Stack<Integer> stack = new Stack(); //单调栈存储数组中元素位置
            for(int i = 0; i < n; i++){
                while(!stack.isEmpty() && prices[i] <= prices[stack.peek()]){
                    int index = stack.pop();
                    result[index] = prices[index] - prices[i];
                }
                stack.push(i);
            }
            //出栈剩余元素
            while(!stack.isEmpty()){
                int idx = stack.pop();
                result[idx] = prices[idx];
            }
    
            return result; 
        }
    }
    
  • 帅气的冬瓜 普通 2023年7月3日 下午3:37

    打卡

如果您已经成功付款但是网站没有弹出成功提示,请联系站长提供付款信息为您处理