Skip to content

Commit 19caf3b

Browse files
committed
feat: add solutions to lc problem: No.01475
No.1475.Final Prices With a Special Discount in a Shop
1 parent 7c9cbad commit 19caf3b

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

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

+40
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,46 @@
6969

7070
```
7171

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+
72112
### **...**
73113

74114
```

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

+40
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,46 @@ For items 3 and 4 you will not receive any discount at all.
6060

6161
```
6262

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+
63103
### **...**
64104

65105
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}

0 commit comments

Comments
 (0)