From 52a993ea87a43bdc6b9bf0d1b61a655a8f51685a Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 18 Dec 2024 22:24:33 +0100 Subject: [PATCH] =?UTF-8?q?java:=20add=20=C2=AB1475.=20Final=20Prices=20Wi?= =?UTF-8?q?th=20a=20Special=20Discount=20in=20a=20Shop=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/ Signed-off-by: Matej Focko --- ...prices-with-a-special-discount-in-a-shop.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 java/final-prices-with-a-special-discount-in-a-shop.java 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; + } +}