Skip to content

Commit 76c8942

Browse files
committed
✨feat: add 1475
1 parent 1d9071c commit 76c8942

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

Index/单调栈.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
| [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/) | 中等 | 🤩🤩🤩🤩 |
66
| [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/) | 中等 | 🤩🤩🤩 |
77
| [654. 最大二叉树](https://leetcode.cn/problems/maximum-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-binary-tree/solution/by-ac_oier-s0wc/) | 中等 | 🤩🤩🤩🤩🤩 |
8+
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
89
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
910

Index/模拟.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
| [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/) | 简单 | 🤩🤩🤩🤩 |
156156
| [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/) | 简单 | 🤩🤩🤩 |
157157
| [1470. 重新排列数组](https://leetcode.cn/problems/shuffle-the-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/shuffle-the-array/solution/by-ac_oier-3lck/) | 简单 | 🤩🤩🤩🤩🤩 |
158+
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
158159
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
159160
| [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/) | 简单 | 🤩🤩🤩 |
160161
| [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/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1475. 商品折扣后的最终价格](https://leetcode.cn/problems/final-prices-with-a-special-discount-in-a-shop/solution/by-ac_oier-hw5b/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」、「单调栈」
6+
7+
8+
9+
给你一个数组 `prices`,其中 `prices[i]` 是商店里第 `i` 件商品的价格。
10+
11+
商店里正在进行促销活动,如果你要买第 `i` 件商品,那么你可以得到与 `prices[j]` 相等的折扣,其中 `j` 是满足 `j > i` 且 `prices[j] <= prices[i]` 的 最小下标 ,如果没有满足条件的 `j` ,你将没有任何折扣。
12+
13+
请你返回一个数组,数组中第 `i` 个元素是折扣后你购买商品 `i` 最终需要支付的价格。
14+
15+
示例 1:
16+
```
17+
输入:prices = [8,4,6,2,3]
18+
19+
输出:[4,2,4,2,3]
20+
21+
解释:
22+
商品 0 的价格为 price[0]=8 ,你将得到 prices[1]=4 的折扣,所以最终价格为 8 - 4 = 4 。
23+
商品 1 的价格为 price[1]=4 ,你将得到 prices[3]=2 的折扣,所以最终价格为 4 - 2 = 2 。
24+
商品 2 的价格为 price[2]=6 ,你将得到 prices[3]=2 的折扣,所以最终价格为 6 - 2 = 4 。
25+
商品 3 和 4 都没有折扣。
26+
```
27+
示例 2:
28+
```
29+
输入:prices = [1,2,3,4,5]
30+
31+
输出:[1,2,3,4,5]
32+
33+
解释:在这个例子中,所有商品都没有折扣。
34+
```
35+
示例 3:
36+
```
37+
输入:prices = [10,1,1,6]
38+
39+
输出:[9,0,1,6]
40+
```
41+
42+
提示:
43+
* $1 <= prices.length <= 500$
44+
* $1 <= prices[i] <= 10^3$
45+
46+
---
47+
48+
### 模拟
49+
50+
数据范围为 $10^3$,可直接模拟。
51+
52+
Java 代码:
53+
```Java
54+
class Solution {
55+
public int[] finalPrices(int[] ps) {
56+
int n = ps.length;
57+
int[] ans = new int[n];
58+
for (int i = 0; i < n; i++) {
59+
int k = 0;
60+
for (int j = i + 1; j < n && k == 0; j++) {
61+
if (ps[j] <= ps[i]) k = ps[j];
62+
}
63+
ans[i] = ps[i] - k;
64+
}
65+
return ans;
66+
}
67+
}
68+
```
69+
Typescript 代码:
70+
```Typescript
71+
function finalPrices(ps: number[]): number[] {
72+
const n = ps.length
73+
const ans = new Array<number>(n).fill(0)
74+
for (let i = 0; i < n; i++) {
75+
let k = 0
76+
for (let j = i + 1; j < n && k == 0; j++) {
77+
if (ps[j] <= ps[i]) k = ps[j]
78+
}
79+
ans[i] = ps[i] - k
80+
}
81+
return ans
82+
};
83+
```
84+
* 时间复杂度:$O(n^2)$
85+
* 空间复杂度:$O(n)$
86+
87+
---
88+
89+
### 单调栈
90+
91+
根据题意,我们知道若 $nums[i]$ 存在折扣,必然是被其右边最近一个满足「$nums[j] <= nums[i]$」的 $nums[j]$ 所更新。反过来说,给定一个 $nums[j]$,其所能更新的是其左边最近一个满足「$nums[i] >= nums[j]$」的 $nums[i]$。
92+
93+
因此我们可以使用「单调栈」来进行求解。
94+
95+
当然,我们也能不依靠经验,而从问题的本身出发,逐步分析出该做法。
96+
97+
具体的,我们可以从前往后处理所有的 $nums[i]$,使用某类容器装载我们所有的「待打折」的商品下标。假设当前处理到的是 $nums[i]$:
98+
* 若其比容器内的任意商品价格要高,其必然不能更新任何一个待打折商品的价格,将其也加入容器尾部(此时我们发现,若有一个新的商品加入容器,其必然是当前所有待打折商品中的价格最高的,即容器内的商品价格单调递增);
99+
* 若其价格不高于容器内的商品价格,其能够更新容器内待打折的商品价格,并且由于我们容器满足单调递增特性,我们必然能够从尾部开始取出待打折商品来进行更新,直到处理完成或遇到第一个无法更新价格的商品。
100+
101+
由于我们需要往尾部添加和取出元素,因此容器可使用「栈」。
102+
103+
**至此,我们分析了单调栈做法的两大重点:为何单调 & 为何用栈。若对「单调栈」仍不了解的同学,可以看前置 🧀 : [关于 RMQ 的若干解法](https://mp.weixin.qq.com/s?__biz=MzU4NDE3MTEyMA==&mid=2247493262&idx=1&sn=2d8e192a5767b49b9a13a6192ab3b833) 中的第四种解法 🎉🎉🎉。**
104+
105+
Java 代码:
106+
```Java
107+
class Solution {
108+
public int[] finalPrices(int[] ps) {
109+
int n = ps.length;
110+
int[] ans = new int[n];
111+
Deque<Integer> d = new ArrayDeque<>();
112+
for (int i = 0; i < n; i++) {
113+
while (!d.isEmpty() && ps[d.peekLast()] >= ps[i]) {
114+
int idx = d.pollLast();
115+
ans[idx] = ps[idx] - ps[i];
116+
}
117+
d.addLast(i); ans[i] = ps[i];
118+
}
119+
return ans;
120+
}
121+
}
122+
```
123+
TypeScript 代码:
124+
```TypeScript
125+
function finalPrices(ps: number[]): number[] {
126+
let n = ps.length, he = 0, ta = 0
127+
const ans = new Array<number>(n).fill(0)
128+
const stk = new Array<number>(n).fill(0)
129+
for (let i = 0; i < n; i++) {
130+
while (he < ta && ps[stk[ta - 1]] >= ps[i]) {
131+
const idx = stk[--ta]
132+
ans[idx] = ps[idx] - ps[i]
133+
}
134+
stk[ta++] = i; ans[i] = ps[i]
135+
}
136+
return ans
137+
};
138+
```
139+
* 时间复杂度:$O(n)$
140+
* 空间复杂度:$O(n)$
141+
142+
---
143+
144+
### 最后
145+
146+
这是我们「刷穿 LeetCode」系列文章的第 `No.1475` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
147+
148+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
149+
150+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
151+
152+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
153+

0 commit comments

Comments
 (0)