java: add «1475. Final Prices With a Special Discount in a Shop»
URL: https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
9a510e9fb9
commit
52a993ea87
1 changed files with 16 additions and 0 deletions
16
java/final-prices-with-a-special-discount-in-a-shop.java
Normal file
16
java/final-prices-with-a-special-discount-in-a-shop.java
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue