Skip to content

Instantly share code, notes, and snippets.

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