Leetcode 1570 Solution

This article provides solution to leetcode question 1570 (final-prices-with-a-special-discount-in-a-shop)

https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop

Solution

class Solution: def finalPrices(self, prices: List[int]) -> List[int]: s = []
ans = [] for i in range(len(prices) - 1, -1, -1): while s and s[-1] > prices[i]: s.pop()
if s: ans.append(prices[i] - s[-1]) else: ans.append(prices[i])
if not s or s[-1] != prices[i]: s.append(prices[i])
return list(reversed(ans))