File tree 7 files changed +196
-11
lines changed
1400-1499/1475.Final Prices With a Special Discount in a Shop
7 files changed +196
-11
lines changed Original file line number Diff line number Diff line change 51
51
52
52
<!-- 这里可写通用的实现逻辑 -->
53
53
54
+ ** 方法一:单调栈**
55
+
56
+ 时间复杂度 $O(n)$,其中 $n$ 表示 $prices$ 的长度。
57
+
54
58
<!-- tabs:start -->
55
59
56
60
### ** Python3**
57
61
58
62
<!-- 这里可写当前语言的特殊实现逻辑 -->
59
63
60
64
``` 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
62
74
```
63
75
64
76
### ** Java**
65
77
66
78
<!-- 这里可写当前语言的特殊实现逻辑 -->
67
79
68
80
``` 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
+ ```
69
97
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
+ }
70
137
```
71
138
72
139
### ** TypeScript**
Original file line number Diff line number Diff line change @@ -51,13 +51,76 @@ For items 3 and 4 you will not receive any discount at all.
51
51
### ** Python3**
52
52
53
53
``` 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
55
63
```
56
64
57
65
### ** Java**
58
66
59
67
``` 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
+ ```
60
84
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
+ }
61
124
```
62
125
63
126
### ** TypeScript**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -206,17 +206,17 @@ def save(result):
206
206
207
207
208
208
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 )
214
214
215
215
with open ('./result.json' , 'r' , encoding = 'utf-8' ) as f :
216
216
res = f .read ()
217
217
res = json .loads (res )
218
218
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)
You can’t perform that action at this time.
0 commit comments