1475. Final Prices With a Special Discount in a Shop
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 83% Topics: Array, Stack, Monotonic Stack
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(n)
class Solution(object):
def finalPrices(self, prices):
"""
:type prices: List[int]
:rtype: List[int]
"""
stk = []
for i, p in enumerate(prices):
while stk and prices[stk[-1]] >= p:
prices[stk.pop()] -= p
stk.append(i)
return prices
Solution from kamyu104/LeetCode-Solutions · MIT