Skip to content

Commit 0e01030

Browse files
committed
feat: add solutions to lc problem: No.1475
No.1475.Final Prices With a Special Discount in a Shop
1 parent 95034df commit 0e01030

File tree

2 files changed

+197
-1
lines changed

2 files changed

+197
-1
lines changed

solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/README.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,19 @@
5353

5454
**方法一:单调栈**
5555

56-
时间复杂度 $O(n)$,其中 $n$ 表示 $prices$ 的长度。
56+
单调栈常见模型:找出每个数左/右边**离它最近的****比它大/小的数**。模板:
57+
58+
```python
59+
stk = []
60+
for i in range(n):
61+
while stk and check(stk[-1], i):
62+
stk.pop()
63+
stk.append(i)
64+
```
65+
66+
本题我们可以采用正序、逆序两种方式遍历数组 `prices`
67+
68+
时间复杂度 $O(n)$,其中 $n$ 表示数组 `prices` 的长度。
5769

5870
<!-- tabs:start -->
5971

@@ -73,6 +85,20 @@ class Solution:
7385
return ans
7486
```
7587

88+
```python
89+
class Solution:
90+
def finalPrices(self, prices: List[int]) -> List[int]:
91+
stk = []
92+
ans = prices[:]
93+
for i in range(len(prices) - 1, -1, -1):
94+
while stk and prices[stk[-1]] > prices[i]:
95+
stk.pop()
96+
if stk:
97+
ans[i] -= prices[stk[-1]]
98+
stk.append(i)
99+
return ans
100+
```
101+
76102
### **Java**
77103

78104
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -95,6 +121,27 @@ class Solution {
95121
}
96122
```
97123

124+
```java
125+
class Solution {
126+
public int[] finalPrices(int[] prices) {
127+
Deque<Integer> stk = new ArrayDeque<>();
128+
int n = prices.length;
129+
int[] ans = new int[n];
130+
for (int i = n - 1; i >= 0; --i) {
131+
ans[i] = prices[i];
132+
while (!stk.isEmpty() && prices[stk.peek()] > prices[i]) {
133+
stk.pop();
134+
}
135+
if (!stk.isEmpty()) {
136+
ans[i] -= prices[stk.peek()];
137+
}
138+
stk.push(i);
139+
}
140+
return ans;
141+
}
142+
}
143+
```
144+
98145
### **C++**
99146

100147
```cpp
@@ -115,6 +162,28 @@ public:
115162
};
116163
```
117164
165+
```cpp
166+
class Solution {
167+
public:
168+
vector<int> finalPrices(vector<int>& prices) {
169+
stack<int> stk;
170+
int n = prices.size();
171+
vector<int> ans(n);
172+
for (int i = n - 1; i >= 0; --i) {
173+
ans[i] = prices[i];
174+
while (!stk.empty() && prices[stk.top()] > prices[i]) {
175+
stk.pop();
176+
}
177+
if (!stk.empty()) {
178+
ans[i] -= prices[stk.top()];
179+
}
180+
stk.push(i);
181+
}
182+
return ans;
183+
}
184+
};
185+
```
186+
118187
### **Go**
119188

120189
```go
@@ -134,8 +203,43 @@ func finalPrices(prices []int) []int {
134203
}
135204
```
136205

206+
```go
207+
func finalPrices(prices []int) []int {
208+
stk := []int{}
209+
n := len(prices)
210+
ans := make([]int, n)
211+
for i := n - 1; i >= 0; i-- {
212+
ans[i] = prices[i]
213+
for len(stk) > 0 && prices[stk[len(stk)-1]] > prices[i] {
214+
stk = stk[:len(stk)-1]
215+
}
216+
if len(stk) > 0 {
217+
ans[i] -= prices[stk[len(stk)-1]]
218+
}
219+
stk = append(stk, i)
220+
}
221+
return ans
222+
}
223+
```
224+
137225
### **TypeScript**
138226

227+
```ts
228+
function finalPrices(prices: number[]): number[] {
229+
const n = prices.length;
230+
const stk = [];
231+
const ans = new Array(n);
232+
for (let i = 0; i < n; ++i) {
233+
ans[i] = prices[i];
234+
while (stk.length && prices[stk[stk.length - 1]] >= prices[i]) {
235+
ans[stk.pop()] -= prices[i];
236+
}
237+
stk.push(i);
238+
}
239+
return ans;
240+
}
241+
```
242+
139243
```ts
140244
function finalPrices(prices: number[]): number[] {
141245
const n = prices.length;

solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/README_EN.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ class Solution:
6262
return ans
6363
```
6464

65+
```python
66+
class Solution:
67+
def finalPrices(self, prices: List[int]) -> List[int]:
68+
stk = []
69+
ans = prices[:]
70+
for i in range(len(prices) - 1, -1, -1):
71+
while stk and prices[stk[-1]] > prices[i]:
72+
stk.pop()
73+
if stk:
74+
ans[i] -= prices[stk[-1]]
75+
stk.append(i)
76+
return ans
77+
```
78+
6579
### **Java**
6680

6781
```java
@@ -82,6 +96,27 @@ class Solution {
8296
}
8397
```
8498

99+
```java
100+
class Solution {
101+
public int[] finalPrices(int[] prices) {
102+
Deque<Integer> stk = new ArrayDeque<>();
103+
int n = prices.length;
104+
int[] ans = new int[n];
105+
for (int i = n - 1; i >= 0; --i) {
106+
ans[i] = prices[i];
107+
while (!stk.isEmpty() && prices[stk.peek()] > prices[i]) {
108+
stk.pop();
109+
}
110+
if (!stk.isEmpty()) {
111+
ans[i] -= prices[stk.peek()];
112+
}
113+
stk.push(i);
114+
}
115+
return ans;
116+
}
117+
}
118+
```
119+
85120
### **C++**
86121

87122
```cpp
@@ -102,6 +137,28 @@ public:
102137
};
103138
```
104139
140+
```cpp
141+
class Solution {
142+
public:
143+
vector<int> finalPrices(vector<int>& prices) {
144+
stack<int> stk;
145+
int n = prices.size();
146+
vector<int> ans(n);
147+
for (int i = n - 1; i >= 0; --i) {
148+
ans[i] = prices[i];
149+
while (!stk.empty() && prices[stk.top()] > prices[i]) {
150+
stk.pop();
151+
}
152+
if (!stk.empty()) {
153+
ans[i] -= prices[stk.top()];
154+
}
155+
stk.push(i);
156+
}
157+
return ans;
158+
}
159+
};
160+
```
161+
105162
### **Go**
106163

107164
```go
@@ -121,8 +178,43 @@ func finalPrices(prices []int) []int {
121178
}
122179
```
123180

181+
```go
182+
func finalPrices(prices []int) []int {
183+
stk := []int{}
184+
n := len(prices)
185+
ans := make([]int, n)
186+
for i := n - 1; i >= 0; i-- {
187+
ans[i] = prices[i]
188+
for len(stk) > 0 && prices[stk[len(stk)-1]] > prices[i] {
189+
stk = stk[:len(stk)-1]
190+
}
191+
if len(stk) > 0 {
192+
ans[i] -= prices[stk[len(stk)-1]]
193+
}
194+
stk = append(stk, i)
195+
}
196+
return ans
197+
}
198+
```
199+
124200
### **TypeScript**
125201

202+
```ts
203+
function finalPrices(prices: number[]): number[] {
204+
const n = prices.length;
205+
const stk = [];
206+
const ans = new Array(n);
207+
for (let i = 0; i < n; ++i) {
208+
ans[i] = prices[i];
209+
while (stk.length && prices[stk[stk.length - 1]] >= prices[i]) {
210+
ans[stk.pop()] -= prices[i];
211+
}
212+
stk.push(i);
213+
}
214+
return ans;
215+
}
216+
```
217+
126218
```ts
127219
function finalPrices(prices: number[]): number[] {
128220
const n = prices.length;

0 commit comments

Comments
 (0)