Skip to content

Commit c56fa13

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

File tree

7 files changed

+196
-11
lines changed

7 files changed

+196
-11
lines changed

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

+68-1
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,89 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
**方法一:单调栈**
55+
56+
时间复杂度 $O(n)$,其中 $n$ 表示 $prices$ 的长度。
57+
5458
<!-- tabs:start -->
5559

5660
### **Python3**
5761

5862
<!-- 这里可写当前语言的特殊实现逻辑 -->
5963

6064
```python
61-
65+
class Solution:
66+
def finalPrices(self, prices: List[int]) -> List[int]:
67+
stk = []
68+
ans = prices[:]
69+
for i, v in enumerate(prices):
70+
while stk and prices[stk[-1]] >= v:
71+
ans[stk.pop()] -= v
72+
stk.append(i)
73+
return ans
6274
```
6375

6476
### **Java**
6577

6678
<!-- 这里可写当前语言的特殊实现逻辑 -->
6779

6880
```java
81+
class Solution {
82+
public int[] finalPrices(int[] prices) {
83+
Deque<Integer> stk = new ArrayDeque<>();
84+
int n = prices.length;
85+
int[] ans = new int[n];
86+
for (int i = 0; i < n; ++i) {
87+
ans[i] = prices[i];
88+
while (!stk.isEmpty() && prices[stk.peek()] >= prices[i]) {
89+
ans[stk.pop()] -= prices[i];
90+
}
91+
stk.push(i);
92+
}
93+
return ans;
94+
}
95+
}
96+
```
6997

98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
vector<int> finalPrices(vector<int>& prices) {
104+
stack<int> stk;
105+
vector<int> ans = prices;
106+
for (int i = 0; i < prices.size(); ++i)
107+
{
108+
while (!stk.empty() && prices[stk.top()] >= prices[i])
109+
{
110+
ans[stk.top()] -= prices[i];
111+
stk.pop();
112+
}
113+
stk.push(i);
114+
}
115+
return ans;
116+
}
117+
};
118+
```
119+
120+
### **Go**
121+
122+
```go
123+
func finalPrices(prices []int) []int {
124+
var stk []int
125+
n := len(prices)
126+
ans := make([]int, n)
127+
for i, v := range prices {
128+
ans[i] = v
129+
for len(stk) > 0 && prices[stk[len(stk)-1]] >= v {
130+
ans[stk[len(stk)-1]] -= v
131+
stk = stk[:len(stk)-1]
132+
}
133+
stk = append(stk, i)
134+
}
135+
return ans
136+
}
70137
```
71138

72139
### **TypeScript**

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

+64-1
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,76 @@ For items 3 and 4 you will not receive any discount at all.
5151
### **Python3**
5252

5353
```python
54-
54+
class Solution:
55+
def finalPrices(self, prices: List[int]) -> List[int]:
56+
stk = []
57+
ans = prices[:]
58+
for i, v in enumerate(prices):
59+
while stk and prices[stk[-1]] >= v:
60+
ans[stk.pop()] -= v
61+
stk.append(i)
62+
return ans
5563
```
5664

5765
### **Java**
5866

5967
```java
68+
class Solution {
69+
public int[] finalPrices(int[] prices) {
70+
Deque<Integer> stk = new ArrayDeque<>();
71+
int n = prices.length;
72+
int[] ans = new int[n];
73+
for (int i = 0; i < n; ++i) {
74+
ans[i] = prices[i];
75+
while (!stk.isEmpty() && prices[stk.peek()] >= prices[i]) {
76+
ans[stk.pop()] -= prices[i];
77+
}
78+
stk.push(i);
79+
}
80+
return ans;
81+
}
82+
}
83+
```
6084

85+
### **C++**
86+
87+
```cpp
88+
class Solution {
89+
public:
90+
vector<int> finalPrices(vector<int>& prices) {
91+
stack<int> stk;
92+
vector<int> ans = prices;
93+
for (int i = 0; i < prices.size(); ++i)
94+
{
95+
while (!stk.empty() && prices[stk.top()] >= prices[i])
96+
{
97+
ans[stk.top()] -= prices[i];
98+
stk.pop();
99+
}
100+
stk.push(i);
101+
}
102+
return ans;
103+
}
104+
};
105+
```
106+
107+
### **Go**
108+
109+
```go
110+
func finalPrices(prices []int) []int {
111+
var stk []int
112+
n := len(prices)
113+
ans := make([]int, n)
114+
for i, v := range prices {
115+
ans[i] = v
116+
for len(stk) > 0 && prices[stk[len(stk)-1]] >= v {
117+
ans[stk[len(stk)-1]] -= v
118+
stk = stk[:len(stk)-1]
119+
}
120+
stk = append(stk, i)
121+
}
122+
return ans
123+
}
61124
```
62125

63126
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> finalPrices(vector<int>& prices) {
4+
stack<int> stk;
5+
vector<int> ans = prices;
6+
for (int i = 0; i < prices.size(); ++i)
7+
{
8+
while (!stk.empty() && prices[stk.top()] >= prices[i])
9+
{
10+
ans[stk.top()] -= prices[i];
11+
stk.pop();
12+
}
13+
stk.push(i);
14+
}
15+
return ans;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func finalPrices(prices []int) []int {
2+
var stk []int
3+
n := len(prices)
4+
ans := make([]int, n)
5+
for i, v := range prices {
6+
ans[i] = v
7+
for len(stk) > 0 && prices[stk[len(stk)-1]] >= v {
8+
ans[stk[len(stk)-1]] -= v
9+
stk = stk[:len(stk)-1]
10+
}
11+
stk = append(stk, i)
12+
}
13+
return ans
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int[] finalPrices(int[] prices) {
3+
Deque<Integer> stk = new ArrayDeque<>();
4+
int n = prices.length;
5+
int[] ans = new int[n];
6+
for (int i = 0; i < n; ++i) {
7+
ans[i] = prices[i];
8+
while (!stk.isEmpty() && prices[stk.peek()] >= prices[i]) {
9+
ans[stk.pop()] -= prices[i];
10+
}
11+
stk.push(i);
12+
}
13+
return ans;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def finalPrices(self, prices: List[int]) -> List[int]:
3+
stk = []
4+
ans = prices[:]
5+
for i, v in enumerate(prices):
6+
while stk and prices[stk[-1]] >= v:
7+
ans[stk.pop()] -= v
8+
stk.append(i)
9+
return ans

solution/main.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,17 @@ def save(result):
206206

207207

208208
if __name__ == '__main__':
209-
# cookie_cn = ''
210-
# cookie_en = ''
211-
# spider = Spider(cookie_cn, cookie_en)
212-
# res = spider.run()
213-
# save(res)
209+
cookie_cn = ''
210+
cookie_en = ''
211+
spider = Spider(cookie_cn, cookie_en)
212+
res = spider.run()
213+
save(res)
214214

215215
with open('./result.json', 'r', encoding='utf-8') as f:
216216
res = f.read()
217217
res = json.loads(res)
218218

219-
# generate_readme(res)
220-
# generate_question_readme(res)
221-
# generate_summary(res)
222-
refresh(res)
219+
generate_readme(res)
220+
generate_question_readme(res)
221+
generate_summary(res)
222+
# refresh(res)

0 commit comments

Comments
 (0)