Skip to content

Commit ba29c38

Browse files
committed
1475. Final Prices With a Special Discount in a Shop.
- Language: Python. - Time complexity: O(n + n). - Space complexity: O(n). - Solution based on: https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/editorial/.
1 parent 3dd119d commit ba29c38

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

python_solutions/lc_1475_final_prices_with_a_spec_discount_in_a_shop/lc_1475.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,13 @@ def finalPricesV2(self, prices: list[int]) -> list[int]:
3636
# Add current index to stack
3737
stack.append(i)
3838
return result
39+
40+
def finalPricesV3(self, prices: list[int]) -> list[int]:
41+
# Time complexity: O(n + n). Space complexity: O(n).
42+
stack = []
43+
for idx, price in enumerate(prices):
44+
while stack and prices[stack[-1]] >= price:
45+
prev_idx = stack.pop()
46+
prices[prev_idx] = prices[prev_idx] - price
47+
stack.append(idx)
48+
return prices

python_solutions/lc_1475_final_prices_with_a_spec_discount_in_a_shop/test_lc_1475.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ def test_success_v2(lst: list[int], ans: list[int], solution: Solution):
3636
assert solution.finalPricesV2(lst) == ans
3737

3838

39+
@pytest.mark.parametrize(("lst", "ans"), test_cases)
40+
def test_success_v3(lst: list[int], ans: list[int], solution: Solution):
41+
assert solution.finalPricesV3(lst) == ans
42+
43+
3944
@pytest.fixture
4045
def solution() -> Solution:
4146
return Solution()

0 commit comments

Comments
 (0)