ans(n);
- for (int i = n - 1; i >= 0; --i) {
- ans[i] = prices[i];
- while (!stk.empty() && prices[stk.top()] > prices[i]) {
- stk.pop();
- }
- if (!stk.empty()) {
- ans[i] -= prices[stk.top()];
- }
- stk.push(i);
- }
- return ans;
- }
-};
-```
-
-#### Go
-
-```go
-func finalPrices(prices []int) []int {
- stk := []int{}
- n := len(prices)
- ans := make([]int, n)
- for i := n - 1; i >= 0; i-- {
- ans[i] = prices[i]
- for len(stk) > 0 && prices[stk[len(stk)-1]] > prices[i] {
- stk = stk[:len(stk)-1]
- }
- if len(stk) > 0 {
- ans[i] -= prices[stk[len(stk)-1]]
- }
- stk = append(stk, i)
- }
- return ans
-}
-```
-
-#### TypeScript
-
-```ts
-function finalPrices(prices: number[]): number[] {
- const n = prices.length;
- const stack = [];
- const res = new Array(n);
- for (let i = n - 1; i >= 0; i--) {
- const price = prices[i];
- while (stack.length !== 0 && stack[stack.length - 1] > price) {
- stack.pop();
- }
- res[i] = price - (stack[stack.length - 1] ?? 0);
- stack.push(price);
+ return $prices;
}
- return res;
}
```
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/README_EN.md b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/README_EN.md
index b86dea9c66387..72d7dfd45532f 100644
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/README_EN.md
+++ b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/README_EN.md
@@ -32,7 +32,7 @@ tags:
Input: prices = [8,4,6,2,3]
Output: [4,2,4,2,3]
-Explanation:
+Explanation:
For item 0 with price[0]=8 you will receive a discount equivalent to prices[1]=4, therefore, the final price you will pay is 8 - 4 = 4.
For item 1 with price[1]=4 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 4 - 2 = 2.
For item 2 with price[2]=6 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 6 - 2 = 4.
@@ -68,7 +68,13 @@ For items 3 and 4 you will not receive any discount at all.
-### Solution 1
+### Solution 1: Monotonic Stack
+
+The problem is essentially to find the first element on the right side that is smaller than each element. We can use a monotonic stack to solve this.
+
+We traverse the array $\textit{prices}$ in reverse order, using the monotonic stack to find the nearest smaller element on the left side of the current element, and then calculate the discount.
+
+The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{prices}$.
@@ -77,14 +83,15 @@ For items 3 and 4 you will not receive any discount at all.
```python
class Solution:
def finalPrices(self, prices: List[int]) -> List[int]:
- ans = []
- for i, v in enumerate(prices):
- ans.append(v)
- for j in range(i + 1, len(prices)):
- if prices[j] <= v:
- ans[-1] -= prices[j]
- break
- return ans
+ stk = []
+ for i in reversed(range(len(prices))):
+ x = prices[i]
+ while stk and x < stk[-1]:
+ stk.pop()
+ if stk:
+ prices[i] -= stk[-1]
+ stk.append(x)
+ return prices
```
#### Java
@@ -93,17 +100,18 @@ class Solution:
class Solution {
public int[] finalPrices(int[] prices) {
int n = prices.length;
- int[] ans = new int[n];
- for (int i = 0; i < n; ++i) {
- ans[i] = prices[i];
- for (int j = i + 1; j < n; ++j) {
- if (prices[j] <= prices[i]) {
- ans[i] -= prices[j];
- break;
- }
+ Deque stk = new ArrayDeque<>();
+ for (int i = n - 1; i >= 0; --i) {
+ int x = prices[i];
+ while (!stk.isEmpty() && stk.peek() > x) {
+ stk.pop();
+ }
+ if (!stk.isEmpty()) {
+ prices[i] -= stk.peek();
}
+ stk.push(x);
}
- return ans;
+ return prices;
}
}
```
@@ -114,18 +122,18 @@ class Solution {
class Solution {
public:
vector finalPrices(vector& prices) {
- int n = prices.size();
- vector ans(n);
- for (int i = 0; i < n; ++i) {
- ans[i] = prices[i];
- for (int j = i + 1; j < n; ++j) {
- if (prices[j] <= prices[i]) {
- ans[i] -= prices[j];
- break;
- }
+ stack stk;
+ for (int i = prices.size() - 1; ~i; --i) {
+ int x = prices[i];
+ while (!stk.empty() && stk.top() > x) {
+ stk.pop();
}
+ if (!stk.empty()) {
+ prices[i] -= stk.top();
+ }
+ stk.push(x);
}
- return ans;
+ return prices;
}
};
```
@@ -134,18 +142,18 @@ public:
```go
func finalPrices(prices []int) []int {
- n := len(prices)
- ans := make([]int, n)
- for i, v := range prices {
- ans[i] = v
- for j := i + 1; j < n; j++ {
- if prices[j] <= v {
- ans[i] -= prices[j]
- break
- }
+ stk := []int{}
+ for i := len(prices) - 1; i >= 0; i-- {
+ x := prices[i]
+ for len(stk) > 0 && stk[len(stk)-1] > x {
+ stk = stk[:len(stk)-1]
+ }
+ if len(stk) > 0 {
+ prices[i] -= stk[len(stk)-1]
}
+ stk = append(stk, x)
}
- return ans
+ return prices
}
```
@@ -153,18 +161,16 @@ func finalPrices(prices []int) []int {
```ts
function finalPrices(prices: number[]): number[] {
- const n = prices.length;
- const ans = new Array(n);
- for (let i = 0; i < n; ++i) {
- ans[i] = prices[i];
- for (let j = i + 1; j < n; ++j) {
- if (prices[j] <= prices[i]) {
- ans[i] -= prices[j];
- break;
- }
+ const stk: number[] = [];
+ for (let i = prices.length - 1; ~i; --i) {
+ const x = prices[i];
+ while (stk.length && stk.at(-1)! > x) {
+ stk.pop();
}
+ prices[i] -= stk.at(-1) || 0;
+ stk.push(x);
}
- return ans;
+ return prices;
}
```
@@ -172,19 +178,19 @@ function finalPrices(prices: number[]): number[] {
```rust
impl Solution {
- pub fn final_prices(prices: Vec) -> Vec {
- let n = prices.len();
- let mut stack = Vec::new();
- let mut res = vec![0; n];
- for i in (0..n).rev() {
- let price = prices[i];
- while !stack.is_empty() && *stack.last().unwrap() > price {
- stack.pop();
+ pub fn final_prices(mut prices: Vec) -> Vec {
+ let mut stk: Vec = Vec::new();
+ for i in (0..prices.len()).rev() {
+ let x = prices[i];
+ while !stk.is_empty() && x < *stk.last().unwrap() {
+ stk.pop();
+ }
+ if let Some(&top) = stk.last() {
+ prices[i] -= top;
}
- res[i] = price - stack.last().unwrap_or(&0);
- stack.push(price);
+ stk.push(x);
}
- res
+ prices
}
}
```
@@ -197,13 +203,14 @@ impl Solution {
* @return {number[]}
*/
var finalPrices = function (prices) {
- for (let i = 0; i < prices.length; i++) {
- for (let j = i + 1; j < prices.length; j++) {
- if (prices[i] >= prices[j]) {
- prices[i] -= prices[j];
- break;
- }
+ const stk = [];
+ for (let i = prices.length - 1; ~i; --i) {
+ const x = prices[i];
+ while (stk.length && stk.at(-1) > x) {
+ stk.pop();
}
+ prices[i] -= stk.at(-1) || 0;
+ stk.push(x);
}
return prices;
};
@@ -218,230 +225,22 @@ class Solution {
* @return Integer[]
*/
function finalPrices($prices) {
- for ($i = 0; $i < count($prices); $i++) {
- for ($j = $i + 1; $j < count($prices); $j++) {
- if ($prices[$i] >= $prices[$j]) {
- $prices[$i] -= $prices[$j];
- break;
- }
- }
- }
- return $prices;
- }
-}
-```
-
-
-
-
-
-
-
-### Solution 2
-
-
-
-#### Python3
+ $stk = [];
+ $n = count($prices);
-```python
-class Solution:
- def finalPrices(self, prices: List[int]) -> List[int]:
- stk = []
- ans = prices[:]
- for i, v in enumerate(prices):
- while stk and prices[stk[-1]] >= v:
- ans[stk.pop()] -= v
- stk.append(i)
- return ans
-```
-
-#### Java
-
-```java
-class Solution {
- public int[] finalPrices(int[] prices) {
- Deque stk = new ArrayDeque<>();
- int n = prices.length;
- int[] ans = new int[n];
- for (int i = 0; i < n; ++i) {
- ans[i] = prices[i];
- while (!stk.isEmpty() && prices[stk.peek()] >= prices[i]) {
- ans[stk.pop()] -= prices[i];
+ for ($i = $n - 1; $i >= 0; $i--) {
+ $x = $prices[$i];
+ while (!empty($stk) && $x < end($stk)) {
+ array_pop($stk);
}
- stk.push(i);
- }
- return ans;
- }
-}
-```
-
-#### C++
-
-```cpp
-class Solution {
-public:
- vector finalPrices(vector& prices) {
- stack stk;
- vector ans = prices;
- for (int i = 0; i < prices.size(); ++i) {
- while (!stk.empty() && prices[stk.top()] >= prices[i]) {
- ans[stk.top()] -= prices[i];
- stk.pop();
+ if (!empty($stk)) {
+ $prices[$i] -= end($stk);
}
- stk.push(i);
+ $stk[] = $x;
}
- return ans;
- }
-};
-```
-
-#### Go
-
-```go
-func finalPrices(prices []int) []int {
- var stk []int
- n := len(prices)
- ans := make([]int, n)
- for i, v := range prices {
- ans[i] = v
- for len(stk) > 0 && prices[stk[len(stk)-1]] >= v {
- ans[stk[len(stk)-1]] -= v
- stk = stk[:len(stk)-1]
- }
- stk = append(stk, i)
- }
- return ans
-}
-```
-#### TypeScript
-
-```ts
-function finalPrices(prices: number[]): number[] {
- const n = prices.length;
- const stk = [];
- const ans = new Array(n);
- for (let i = 0; i < n; ++i) {
- ans[i] = prices[i];
- while (stk.length && prices[stk[stk.length - 1]] >= prices[i]) {
- ans[stk.pop()] -= prices[i];
- }
- stk.push(i);
- }
- return ans;
-}
-```
-
-
-
-
-
-
-
-### Solution 3
-
-
-
-#### Python3
-
-```python
-class Solution:
- def finalPrices(self, prices: List[int]) -> List[int]:
- stk = []
- ans = prices[:]
- for i in range(len(prices) - 1, -1, -1):
- while stk and prices[stk[-1]] > prices[i]:
- stk.pop()
- if stk:
- ans[i] -= prices[stk[-1]]
- stk.append(i)
- return ans
-```
-
-#### Java
-
-```java
-class Solution {
- public int[] finalPrices(int[] prices) {
- Deque stk = new ArrayDeque<>();
- int n = prices.length;
- int[] ans = new int[n];
- for (int i = n - 1; i >= 0; --i) {
- ans[i] = prices[i];
- while (!stk.isEmpty() && prices[stk.peek()] > prices[i]) {
- stk.pop();
- }
- if (!stk.isEmpty()) {
- ans[i] -= prices[stk.peek()];
- }
- stk.push(i);
- }
- return ans;
- }
-}
-```
-
-#### C++
-
-```cpp
-class Solution {
-public:
- vector finalPrices(vector& prices) {
- stack stk;
- int n = prices.size();
- vector ans(n);
- for (int i = n - 1; i >= 0; --i) {
- ans[i] = prices[i];
- while (!stk.empty() && prices[stk.top()] > prices[i]) {
- stk.pop();
- }
- if (!stk.empty()) {
- ans[i] -= prices[stk.top()];
- }
- stk.push(i);
- }
- return ans;
- }
-};
-```
-
-#### Go
-
-```go
-func finalPrices(prices []int) []int {
- stk := []int{}
- n := len(prices)
- ans := make([]int, n)
- for i := n - 1; i >= 0; i-- {
- ans[i] = prices[i]
- for len(stk) > 0 && prices[stk[len(stk)-1]] > prices[i] {
- stk = stk[:len(stk)-1]
- }
- if len(stk) > 0 {
- ans[i] -= prices[stk[len(stk)-1]]
- }
- stk = append(stk, i)
- }
- return ans
-}
-```
-
-#### TypeScript
-
-```ts
-function finalPrices(prices: number[]): number[] {
- const n = prices.length;
- const stack = [];
- const res = new Array(n);
- for (let i = n - 1; i >= 0; i--) {
- const price = prices[i];
- while (stack.length !== 0 && stack[stack.length - 1] > price) {
- stack.pop();
- }
- res[i] = price - (stack[stack.length - 1] ?? 0);
- stack.push(price);
+ return $prices;
}
- return res;
}
```
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.cpp b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.cpp
index a27c24247cb10..b443773fc8bdd 100644
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.cpp
+++ b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.cpp
@@ -1,17 +1,17 @@
class Solution {
public:
vector finalPrices(vector& prices) {
- int n = prices.size();
- vector ans(n);
- for (int i = 0; i < n; ++i) {
- ans[i] = prices[i];
- for (int j = i + 1; j < n; ++j) {
- if (prices[j] <= prices[i]) {
- ans[i] -= prices[j];
- break;
- }
+ stack stk;
+ for (int i = prices.size() - 1; ~i; --i) {
+ int x = prices[i];
+ while (!stk.empty() && stk.top() > x) {
+ stk.pop();
}
+ if (!stk.empty()) {
+ prices[i] -= stk.top();
+ }
+ stk.push(x);
}
- return ans;
+ return prices;
}
-};
\ No newline at end of file
+};
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.go b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.go
index 6d9911f2dd0e4..d240432f9dea1 100644
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.go
+++ b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.go
@@ -1,14 +1,14 @@
func finalPrices(prices []int) []int {
- n := len(prices)
- ans := make([]int, n)
- for i, v := range prices {
- ans[i] = v
- for j := i + 1; j < n; j++ {
- if prices[j] <= v {
- ans[i] -= prices[j]
- break
- }
+ stk := []int{}
+ for i := len(prices) - 1; i >= 0; i-- {
+ x := prices[i]
+ for len(stk) > 0 && stk[len(stk)-1] > x {
+ stk = stk[:len(stk)-1]
}
+ if len(stk) > 0 {
+ prices[i] -= stk[len(stk)-1]
+ }
+ stk = append(stk, x)
}
- return ans
-}
\ No newline at end of file
+ return prices
+}
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.java b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.java
index 1f9c12d04cea6..6fdb1d9ac8f34 100644
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.java
+++ b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.java
@@ -1,16 +1,17 @@
class Solution {
public int[] finalPrices(int[] prices) {
int n = prices.length;
- int[] ans = new int[n];
- for (int i = 0; i < n; ++i) {
- ans[i] = prices[i];
- for (int j = i + 1; j < n; ++j) {
- if (prices[j] <= prices[i]) {
- ans[i] -= prices[j];
- break;
- }
+ Deque stk = new ArrayDeque<>();
+ for (int i = n - 1; i >= 0; --i) {
+ int x = prices[i];
+ while (!stk.isEmpty() && stk.peek() > x) {
+ stk.pop();
}
+ if (!stk.isEmpty()) {
+ prices[i] -= stk.peek();
+ }
+ stk.push(x);
}
- return ans;
+ return prices;
}
-}
\ No newline at end of file
+}
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.js b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.js
index 9eff2851ec96d..6088fbe23d290 100644
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.js
+++ b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.js
@@ -3,13 +3,14 @@
* @return {number[]}
*/
var finalPrices = function (prices) {
- for (let i = 0; i < prices.length; i++) {
- for (let j = i + 1; j < prices.length; j++) {
- if (prices[i] >= prices[j]) {
- prices[i] -= prices[j];
- break;
- }
+ const stk = [];
+ for (let i = prices.length - 1; ~i; --i) {
+ const x = prices[i];
+ while (stk.length && stk.at(-1) > x) {
+ stk.pop();
}
+ prices[i] -= stk.at(-1) || 0;
+ stk.push(x);
}
return prices;
};
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.php b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.php
index 3347b98a08557..6f913e37049eb 100644
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.php
+++ b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.php
@@ -4,14 +4,20 @@ class Solution {
* @return Integer[]
*/
function finalPrices($prices) {
- for ($i = 0; $i < count($prices); $i++) {
- for ($j = $i + 1; $j < count($prices); $j++) {
- if ($prices[$i] >= $prices[$j]) {
- $prices[$i] -= $prices[$j];
- break;
- }
+ $stk = [];
+ $n = count($prices);
+
+ for ($i = $n - 1; $i >= 0; $i--) {
+ $x = $prices[$i];
+ while (!empty($stk) && $x < end($stk)) {
+ array_pop($stk);
}
+ if (!empty($stk)) {
+ $prices[$i] -= end($stk);
+ }
+ $stk[] = $x;
}
+
return $prices;
}
}
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.py b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.py
index 7942f1115c027..9d0aa3ef0c898 100644
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.py
+++ b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.py
@@ -1,10 +1,11 @@
class Solution:
def finalPrices(self, prices: List[int]) -> List[int]:
- ans = []
- for i, v in enumerate(prices):
- ans.append(v)
- for j in range(i + 1, len(prices)):
- if prices[j] <= v:
- ans[-1] -= prices[j]
- break
- return ans
+ stk = []
+ for i in reversed(range(len(prices))):
+ x = prices[i]
+ while stk and x < stk[-1]:
+ stk.pop()
+ if stk:
+ prices[i] -= stk[-1]
+ stk.append(x)
+ return prices
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.rs b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.rs
index 909993b1afbd6..85929a70c29cf 100644
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.rs
+++ b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.rs
@@ -1,16 +1,16 @@
impl Solution {
- pub fn final_prices(prices: Vec) -> Vec {
- let n = prices.len();
- let mut stack = Vec::new();
- let mut res = vec![0; n];
- for i in (0..n).rev() {
- let price = prices[i];
- while !stack.is_empty() && *stack.last().unwrap() > price {
- stack.pop();
+ pub fn final_prices(mut prices: Vec) -> Vec {
+ let mut stk: Vec = Vec::new();
+ for i in (0..prices.len()).rev() {
+ let x = prices[i];
+ while !stk.is_empty() && x < *stk.last().unwrap() {
+ stk.pop();
}
- res[i] = price - stack.last().unwrap_or(&0);
- stack.push(price);
+ if let Some(&top) = stk.last() {
+ prices[i] -= top;
+ }
+ stk.push(x);
}
- res
+ prices
}
}
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.ts b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.ts
index dfaead9442012..91a01620fcd36 100644
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.ts
+++ b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.ts
@@ -1,14 +1,12 @@
function finalPrices(prices: number[]): number[] {
- const n = prices.length;
- const ans = new Array(n);
- for (let i = 0; i < n; ++i) {
- ans[i] = prices[i];
- for (let j = i + 1; j < n; ++j) {
- if (prices[j] <= prices[i]) {
- ans[i] -= prices[j];
- break;
- }
+ const stk: number[] = [];
+ for (let i = prices.length - 1; ~i; --i) {
+ const x = prices[i];
+ while (stk.length && stk.at(-1)! > x) {
+ stk.pop();
}
+ prices[i] -= stk.at(-1) || 0;
+ stk.push(x);
}
- return ans;
+ return prices;
}
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.cpp b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.cpp
deleted file mode 100644
index c3ffc6b6269e6..0000000000000
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.cpp
+++ /dev/null
@@ -1,15 +0,0 @@
-class Solution {
-public:
- vector finalPrices(vector& prices) {
- stack stk;
- vector ans = prices;
- for (int i = 0; i < prices.size(); ++i) {
- while (!stk.empty() && prices[stk.top()] >= prices[i]) {
- ans[stk.top()] -= prices[i];
- stk.pop();
- }
- stk.push(i);
- }
- return ans;
- }
-};
\ No newline at end of file
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.go b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.go
deleted file mode 100644
index 45a1268ec3f07..0000000000000
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.go
+++ /dev/null
@@ -1,14 +0,0 @@
-func finalPrices(prices []int) []int {
- var stk []int
- n := len(prices)
- ans := make([]int, n)
- for i, v := range prices {
- ans[i] = v
- for len(stk) > 0 && prices[stk[len(stk)-1]] >= v {
- ans[stk[len(stk)-1]] -= v
- stk = stk[:len(stk)-1]
- }
- stk = append(stk, i)
- }
- return ans
-}
\ No newline at end of file
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.java b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.java
deleted file mode 100644
index d921c3896b78b..0000000000000
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.java
+++ /dev/null
@@ -1,15 +0,0 @@
-class Solution {
- public int[] finalPrices(int[] prices) {
- Deque stk = new ArrayDeque<>();
- int n = prices.length;
- int[] ans = new int[n];
- for (int i = 0; i < n; ++i) {
- ans[i] = prices[i];
- while (!stk.isEmpty() && prices[stk.peek()] >= prices[i]) {
- ans[stk.pop()] -= prices[i];
- }
- stk.push(i);
- }
- return ans;
- }
-}
\ No newline at end of file
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.py b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.py
deleted file mode 100644
index 5b205f2d148c3..0000000000000
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.py
+++ /dev/null
@@ -1,9 +0,0 @@
-class Solution:
- def finalPrices(self, prices: List[int]) -> List[int]:
- stk = []
- ans = prices[:]
- for i, v in enumerate(prices):
- while stk and prices[stk[-1]] >= v:
- ans[stk.pop()] -= v
- stk.append(i)
- return ans
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.ts b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.ts
deleted file mode 100644
index 8f59cd019eae0..0000000000000
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution2.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-function finalPrices(prices: number[]): number[] {
- const n = prices.length;
- const stk = [];
- const ans = new Array(n);
- for (let i = 0; i < n; ++i) {
- ans[i] = prices[i];
- while (stk.length && prices[stk[stk.length - 1]] >= prices[i]) {
- ans[stk.pop()] -= prices[i];
- }
- stk.push(i);
- }
- return ans;
-}
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.cpp b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.cpp
deleted file mode 100644
index 31676616def76..0000000000000
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.cpp
+++ /dev/null
@@ -1,19 +0,0 @@
-class Solution {
-public:
- vector finalPrices(vector& prices) {
- stack stk;
- int n = prices.size();
- vector ans(n);
- for (int i = n - 1; i >= 0; --i) {
- ans[i] = prices[i];
- while (!stk.empty() && prices[stk.top()] > prices[i]) {
- stk.pop();
- }
- if (!stk.empty()) {
- ans[i] -= prices[stk.top()];
- }
- stk.push(i);
- }
- return ans;
- }
-};
\ No newline at end of file
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.go b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.go
deleted file mode 100644
index 0e2ed7f1c6429..0000000000000
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.go
+++ /dev/null
@@ -1,16 +0,0 @@
-func finalPrices(prices []int) []int {
- stk := []int{}
- n := len(prices)
- ans := make([]int, n)
- for i := n - 1; i >= 0; i-- {
- ans[i] = prices[i]
- for len(stk) > 0 && prices[stk[len(stk)-1]] > prices[i] {
- stk = stk[:len(stk)-1]
- }
- if len(stk) > 0 {
- ans[i] -= prices[stk[len(stk)-1]]
- }
- stk = append(stk, i)
- }
- return ans
-}
\ No newline at end of file
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.java b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.java
deleted file mode 100644
index bbba5ea49ba52..0000000000000
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.java
+++ /dev/null
@@ -1,18 +0,0 @@
-class Solution {
- public int[] finalPrices(int[] prices) {
- Deque stk = new ArrayDeque<>();
- int n = prices.length;
- int[] ans = new int[n];
- for (int i = n - 1; i >= 0; --i) {
- ans[i] = prices[i];
- while (!stk.isEmpty() && prices[stk.peek()] > prices[i]) {
- stk.pop();
- }
- if (!stk.isEmpty()) {
- ans[i] -= prices[stk.peek()];
- }
- stk.push(i);
- }
- return ans;
- }
-}
\ No newline at end of file
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.py b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.py
deleted file mode 100644
index 4de446e25eb2d..0000000000000
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.py
+++ /dev/null
@@ -1,11 +0,0 @@
-class Solution:
- def finalPrices(self, prices: List[int]) -> List[int]:
- stk = []
- ans = prices[:]
- for i in range(len(prices) - 1, -1, -1):
- while stk and prices[stk[-1]] > prices[i]:
- stk.pop()
- if stk:
- ans[i] -= prices[stk[-1]]
- stk.append(i)
- return ans
diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.ts b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.ts
deleted file mode 100644
index a784c3155edea..0000000000000
--- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution3.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-function finalPrices(prices: number[]): number[] {
- const n = prices.length;
- const stack = [];
- const res = new Array(n);
- for (let i = n - 1; i >= 0; i--) {
- const price = prices[i];
- while (stack.length !== 0 && stack[stack.length - 1] > price) {
- stack.pop();
- }
- res[i] = price - (stack[stack.length - 1] ?? 0);
- stack.push(price);
- }
- return res;
-}