Skip to content

Instantly share code, notes, and snippets.

@chrisynchen
Created February 9, 2022 16:49
Show Gist options
  • Save chrisynchen/b78e6493fb2725a63af0275004324fd6 to your computer and use it in GitHub Desktop.
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)
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