-
Notifications
You must be signed in to change notification settings - Fork 0
/
1475.py
45 lines (31 loc) · 1.12 KB
/
1475.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from typing import List
class Solution:
def finalPrices(self, prices: List[int]) -> List[int]:
# for each item in the PRICE LIST
# check if there is a discount
# the discount is calculated as:
# given position price[i] >= price[j]
# then d = price[i] - price[j] if price[j] < price[i]
# otherwise d = price[i]
size = len(prices)
monotonic_stack = []
result = [0] * size
# for i in range(size - 1, -1, -1):
for i in range(size - 1, -1, -1):
current = prices[i]
while len(monotonic_stack) > 0 and monotonic_stack[-1] > current:
monotonic_stack.pop()
if not monotonic_stack:
result[i] = current
else:
result[i] = current - monotonic_stack[-1]
monotonic_stack.append(current)
return result
import unittest
class Test(unittest.TestCase):
def test_first_case(self):
self.assertEqual(
Solution().finalPrices(prices=[8, 4, 6, 2, 3]), [4, 2, 4, 2, 3]
)
if __name__ == '__main__':
unittest.main()