diff --git "a/Index/\345\215\225\350\260\203\346\240\210.md" "b/Index/\345\215\225\350\260\203\346\240\210.md" index 7b72699e..b4d34f96 100644 --- "a/Index/\345\215\225\350\260\203\346\240\210.md" +++ "b/Index/\345\215\225\350\260\203\346\240\210.md" @@ -5,5 +5,6 @@ | [496. 下一个更大元素 I](https://leetcode-cn.com/problems/next-greater-element-i/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/next-greater-element-i/solution/gong-shui-san-xie-yi-ti-shuang-jie-bian-n6nwz/) | 中等 | 🤩🤩🤩🤩 | | [503. 下一个更大元素 II](https://leetcode-cn.com/problems/next-greater-element-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/next-greater-element-ii/solution/cong-po-su-jie-fa-de-jiao-du-qu-li-jie-d-trht/) | 中等 | 🤩🤩🤩 | | [654. 最大二叉树](https://leetcode.cn/problems/maximum-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-binary-tree/solution/by-ac_oier-s0wc/) | 中等 | 🤩🤩🤩🤩🤩 | +| [1475. 商品折扣后的最终价格](https://leetcode.cn/problems/final-prices-with-a-special-discount-in-a-shop/) | [LeetCode 题解链接](https://leetcode.cn/problems/final-prices-with-a-special-discount-in-a-shop/solution/by-ac_oier-hw5b/) | 简单 | 🤩🤩🤩🤩🤩 | | [2104. 子数组范围和](https://leetcode-cn.com/problems/sum-of-subarray-ranges/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-subarray-ranges/solution/gong-shui-san-xie-yi-ti-san-jie-qu-jian-wn84z/) | 中等 | 🤩🤩🤩🤩🤩 | diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 5805d440..a2adeae6 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -155,6 +155,7 @@ | [1460. 通过翻转子数组使两个数组相等](https://leetcode.cn/problems/make-two-arrays-equal-by-reversing-sub-arrays/) | [LeetCode 题解链接](https://leetcode.cn/problems/make-two-arrays-equal-by-reversing-sub-arrays/solution/by-ac_oier-pv38/) | 简单 | 🤩🤩🤩🤩 | | [1464. 数组中两元素的最大乘积](https://leetcode.cn/problems/maximum-product-of-two-elements-in-an-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-product-of-two-elements-in-an-array/solution/by-ac_oier-t5p3/) | 简单 | 🤩🤩🤩 | | [1470. 重新排列数组](https://leetcode.cn/problems/shuffle-the-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/shuffle-the-array/solution/by-ac_oier-3lck/) | 简单 | 🤩🤩🤩🤩🤩 | +| [1475. 商品折扣后的最终价格](https://leetcode.cn/problems/final-prices-with-a-special-discount-in-a-shop/) | [LeetCode 题解链接](https://leetcode.cn/problems/final-prices-with-a-special-discount-in-a-shop/solution/by-ac_oier-hw5b/) | 简单 | 🤩🤩🤩🤩🤩 | | [1480. 一维数组的动态和](https://leetcode-cn.com/problems/running-sum-of-1d-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/running-sum-of-1d-array/solution/gong-shui-san-xie-yi-wei-qian-zhui-he-mo-g8hn/) | 简单 | 🤩🤩🤩🤩🤩 | | [1486. 数组异或操作](https://leetcode-cn.com/problems/xor-operation-in-an-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xor-operation-in-an-array/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-dggg/) | 简单 | 🤩🤩🤩 | | [1518. 换酒问题](https://leetcode-cn.com/problems/water-bottles/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/water-bottles/solution/gong-shui-san-xie-yi-ti-shuang-jie-ji-sh-7yyo/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/1471-1480/1475. \345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/1471-1480/1475. \345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..46b8a472 --- /dev/null +++ "b/LeetCode/1471-1480/1475. \345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,153 @@ +### 题目描述 + +这是 LeetCode 上的 **[1475. 商品折扣后的最终价格](https://leetcode.cn/problems/final-prices-with-a-special-discount-in-a-shop/solution/by-ac_oier-hw5b/)** ,难度为 **简单**。 + +Tag : 「模拟」、「单调栈」 + + + +给你一个数组 `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$ + +--- + +### 模拟 + +数据范围为 $10^3$,可直接模拟。 + +Java 代码: +```Java +class Solution { + public int[] finalPrices(int[] ps) { + int n = ps.length; + int[] ans = new int[n]; + for (int i = 0; i < n; i++) { + int k = 0; + for (int j = i + 1; j < n && k == 0; j++) { + if (ps[j] <= ps[i]) k = ps[j]; + } + ans[i] = ps[i] - k; + } + return ans; + } +} +``` +Typescript 代码: +```Typescript +function finalPrices(ps: number[]): number[] { + const n = ps.length + const ans = new Array(n).fill(0) + for (let i = 0; i < n; i++) { + let k = 0 + for (let j = i + 1; j < n && k == 0; j++) { + if (ps[j] <= ps[i]) k = ps[j] + } + ans[i] = ps[i] - k + } + return ans +}; +``` +* 时间复杂度:$O(n^2)$ +* 空间复杂度:$O(n)$ + +--- + +### 单调栈 + +根据题意,我们知道若 $nums[i]$ 存在折扣,必然是被其右边最近一个满足「$nums[j] <= nums[i]$」的 $nums[j]$ 所更新。反过来说,给定一个 $nums[j]$,其所能更新的是其左边最近一个满足「$nums[i] >= nums[j]$」的 $nums[i]$。 + +因此我们可以使用「单调栈」来进行求解。 + +当然,我们也能不依靠经验,而从问题的本身出发,逐步分析出该做法。 + +具体的,我们可以从前往后处理所有的 $nums[i]$,使用某类容器装载我们所有的「待打折」的商品下标。假设当前处理到的是 $nums[i]$: +* 若其比容器内的任意商品价格要高,其必然不能更新任何一个待打折商品的价格,将其也加入容器尾部(此时我们发现,若有一个新的商品加入容器,其必然是当前所有待打折商品中的价格最高的,即容器内的商品价格单调递增); +* 若其价格不高于容器内的商品价格,其能够更新容器内待打折的商品价格,并且由于我们容器满足单调递增特性,我们必然能够从尾部开始取出待打折商品来进行更新,直到处理完成或遇到第一个无法更新价格的商品。 + +由于我们需要往尾部添加和取出元素,因此容器可使用「栈」。 + +**至此,我们分析了单调栈做法的两大重点:为何单调 & 为何用栈。若对「单调栈」仍不了解的同学,可以看前置 🧀 : [关于 RMQ 的若干解法](https://mp.weixin.qq.com/s?__biz=MzU4NDE3MTEyMA==&mid=2247493262&idx=1&sn=2d8e192a5767b49b9a13a6192ab3b833) 中的第四种解法 🎉🎉🎉。** + +Java 代码: +```Java +class Solution { + public int[] finalPrices(int[] ps) { + int n = ps.length; + int[] ans = new int[n]; + Deque d = new ArrayDeque<>(); + for (int i = 0; i < n; i++) { + while (!d.isEmpty() && ps[d.peekLast()] >= ps[i]) { + int idx = d.pollLast(); + ans[idx] = ps[idx] - ps[i]; + } + d.addLast(i); ans[i] = ps[i]; + } + return ans; + } +} +``` +TypeScript 代码: +```TypeScript +function finalPrices(ps: number[]): number[] { + let n = ps.length, he = 0, ta = 0 + const ans = new Array(n).fill(0) + const stk = new Array(n).fill(0) + for (let i = 0; i < n; i++) { + while (he < ta && ps[stk[ta - 1]] >= ps[i]) { + const idx = stk[--ta] + ans[idx] = ps[idx] - ps[i] + } + stk[ta++] = i; ans[i] = ps[i] + } + return ans +}; +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(n)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.1475` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +