Created
February 9, 2022 16:49
-
-
Save chrisynchen/b78e6493fb2725a63af0275004324fd6 to your computer and use it in GitHub Desktop.
Leetcode 1475. Final Prices With a Special Discount in a Shop (previous 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]) { | |
stack.pop(); | |
} | |
if(!stack.isEmpty()) { | |
prices[i] -= prices[stack.peek()]; | |
} | |
stack.push(i); | |
} | |
return prices; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment