Matej Focko
52a993ea87
URL: https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/ Signed-off-by: Matej Focko <me@mfocko.xyz>
16 lines
353 B
Java
16 lines
353 B
Java
class Solution {
|
|
public int[] finalPrices(int[] prices) {
|
|
int[] result = prices.clone();
|
|
|
|
var stack = new Stack<Integer>();
|
|
for (int i = 0; i < prices.length; ++i) {
|
|
while (!stack.isEmpty() && prices[stack.peek()] >= prices[i]) {
|
|
result[stack.pop()] -= prices[i];
|
|
}
|
|
|
|
stack.add(i);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|