Last active
February 9, 2022 16:45
-
-
Save chrisynchen/7013c6979a2c7dd64e72c08a7ce5b030 to your computer and use it in GitHub Desktop.
Leetcode 1475. Final Prices With a Special Discount in a Shop (next less)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int[] finalPrices(int[] prices) { | |
Stack<Integer> stack = new Stack<>(); | |
for(int i = 0; i < prices.length; i++) { | |
while(!stack.isEmpty() && prices[stack.peek()] >= prices[i]) { | |
int prevIndex = stack.pop(); | |
prices[prevIndex] -= prices[i]; | |
} | |
stack.push(i); | |
} | |
return prices; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment