Skip to content

Commit 260aee9

Browse files
committed
Time: 176 ms (95.63%), Space: 30.5 MB (27.5%) - LeetHub
1 parent a004c85 commit 260aee9

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# time complexity: O(n)
2+
# space complexity: O(n)
3+
from typing import List
4+
5+
6+
class Solution:
7+
def minOperations(self, nums: List[int]) -> int:
8+
monoStack = [-1]
9+
result = 0
10+
for num in nums:
11+
while num < monoStack[-1]:
12+
monoStack.pop()
13+
if num > monoStack[-1]:
14+
result += (num > 0)
15+
monoStack.append(num)
16+
return result
17+
18+
19+
nums = [0, 2]
20+
print(Solution().minOperations(nums))
21+
nums = [3, 1, 2, 1]
22+
print(Solution().minOperations(nums))
23+
nums = [1, 2, 1, 2, 1, 2]
24+
print(Solution().minOperations(nums))
25+
nums = [1, 0, 2, 0, 3]
26+
print(Solution().minOperations(nums))

0 commit comments

Comments
 (0)