diff --git a/java/final-prices-with-a-special-discount-in-a-shop.java b/java/final-prices-with-a-special-discount-in-a-shop.java new file mode 100644 index 0000000..d78f875 --- /dev/null +++ b/java/final-prices-with-a-special-discount-in-a-shop.java @@ -0,0 +1,16 @@ +class Solution { + public int[] finalPrices(int[] prices) { + int[] result = prices.clone(); + + var stack = new Stack(); + 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; + } +}