File tree 4 files changed +110
-0
lines changed
solution/1400-1499/1475.Final Prices With a Special Discount in a Shop
4 files changed +110
-0
lines changed Original file line number Diff line number Diff line change 69
69
70
70
```
71
71
72
+ ### ** TypeScript**
73
+
74
+ ``` ts
75
+ function finalPrices(prices : number []): number [] {
76
+ const n = prices .length ;
77
+ const stack = [];
78
+ const res = new Array (n );
79
+ for (let i = n - 1 ; i >= 0 ; i -- ) {
80
+ const price = prices [i ];
81
+ while (stack .length !== 0 && stack [stack .length - 1 ] > price ) {
82
+ stack .pop ();
83
+ }
84
+ res [i ] = price - (stack [stack .length - 1 ] ?? 0 );
85
+ stack .push (price );
86
+ }
87
+ return res ;
88
+ }
89
+ ```
90
+
91
+ ### ** Rust**
92
+
93
+ ``` rust
94
+ impl Solution {
95
+ pub fn final_prices (prices : Vec <i32 >) -> Vec <i32 > {
96
+ let n = prices . len ();
97
+ let mut stack = Vec :: new ();
98
+ let mut res = vec! [0 ; n ];
99
+ for i in (0 .. n ). rev () {
100
+ let price = prices [i ];
101
+ while ! stack . is_empty () && * stack . last (). unwrap () > price {
102
+ stack . pop ();
103
+ }
104
+ res [i ] = price - stack . last (). unwrap_or (& 0 );
105
+ stack . push (price );
106
+ }
107
+ res
108
+ }
109
+ }
110
+ ```
111
+
72
112
### ** ...**
73
113
74
114
```
Original file line number Diff line number Diff line change @@ -60,6 +60,46 @@ For items 3 and 4 you will not receive any discount at all.
60
60
61
61
```
62
62
63
+ ### ** TypeScript**
64
+
65
+ ``` ts
66
+ function finalPrices(prices : number []): number [] {
67
+ const n = prices .length ;
68
+ const stack = [];
69
+ const res = new Array (n );
70
+ for (let i = n - 1 ; i >= 0 ; i -- ) {
71
+ const price = prices [i ];
72
+ while (stack .length !== 0 && stack [stack .length - 1 ] > price ) {
73
+ stack .pop ();
74
+ }
75
+ res [i ] = price - (stack [stack .length - 1 ] ?? 0 );
76
+ stack .push (price );
77
+ }
78
+ return res ;
79
+ }
80
+ ```
81
+
82
+ ### ** Rust**
83
+
84
+ ``` rust
85
+ impl Solution {
86
+ pub fn final_prices (prices : Vec <i32 >) -> Vec <i32 > {
87
+ let n = prices . len ();
88
+ let mut stack = Vec :: new ();
89
+ let mut res = vec! [0 ; n ];
90
+ for i in (0 .. n ). rev () {
91
+ let price = prices [i ];
92
+ while ! stack . is_empty () && * stack . last (). unwrap () > price {
93
+ stack . pop ();
94
+ }
95
+ res [i ] = price - stack . last (). unwrap_or (& 0 );
96
+ stack . push (price );
97
+ }
98
+ res
99
+ }
100
+ }
101
+ ```
102
+
63
103
### ** ...**
64
104
65
105
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn final_prices ( prices : Vec < i32 > ) -> Vec < i32 > {
3
+ let n = prices. len ( ) ;
4
+ let mut stack = Vec :: new ( ) ;
5
+ let mut res = vec ! [ 0 ; n] ;
6
+ for i in ( 0 ..n) . rev ( ) {
7
+ let price = prices[ i] ;
8
+ while !stack. is_empty ( ) && * stack. last ( ) . unwrap ( ) > price {
9
+ stack. pop ( ) ;
10
+ }
11
+ res[ i] = price - stack. last ( ) . unwrap_or ( & 0 ) ;
12
+ stack. push ( price) ;
13
+ }
14
+ res
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ function finalPrices ( prices : number [ ] ) : number [ ] {
2
+ const n = prices . length ;
3
+ const res = new Array ( n ) ;
4
+ const stack = [ ] ;
5
+ for ( let i = n - 1 ; i >= 0 ; i -- ) {
6
+ const price = prices [ i ] ;
7
+ while ( stack . length !== 0 && stack [ stack . length - 1 ] > price ) {
8
+ stack . pop ( ) ;
9
+ }
10
+ res [ i ] = price - ( stack [ stack . length - 1 ] ?? 0 ) ;
11
+ stack . push ( price ) ;
12
+ }
13
+ return res ;
14
+ }
You can’t perform that action at this time.
0 commit comments