Easy
Final Prices With a Special Discount in a Shop — Python
Full explanation · Time O(n) · Space O(n)
# 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