Leetcode Problem 1475. Final Prices With a Special Discount in a Shop
1475. Final Prices With a Special Discount in a Shop
Leetcode Solutions
Monotonic Stack Approach
Initialize an empty stack and an array answer of the same length as prices to store the final prices.
Iterate through the prices array from right to left.
For each price, while the stack is not empty and the top of the stack is greater than the current price, pop from the stack.
If the stack is not empty, the top of the stack is the discount for the current price. Subtract it from the current price and store the result in answer.
If the stack is empty, there is no discount, and the current price is the final price. Store it in answer.