-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonotonicStack.py
152 lines (134 loc) · 3.69 KB
/
MonotonicStack.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 402. Remove K Digits
class Solution:
def removeKdigits(self, num: str, k: int) -> str:
stk = []
for digit in num:
while stk and k and stk[-1] > digit:
stk.pop()
k -= 1
stk.append(digit)
if k:
finalstk = stk[:-k]
else:
finalstk = stk
return "".join(finalstk).lstrip('0') or '0'
# 918. Maximum Sum Circular Subarray
class Solution:
def maxSubarraySumCircular(self, nums: List[int]) -> int:
n = len(nums)
p = [0]
for i in range(2 * n):
p.append(p[-1] + nums[i % n])
d = deque([0])
ans = nums[0]
for i in range(1, 2 * n):
if i - d[0] > n:
d.popleft()
ans = max(ans, p[i] - p[d[0]])
while d and p[i] <= p[d[-1]]:
d.pop()
d.append(i)
return ans
# 739. Daily Temperatures
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
n = len(temperatures)
q = []
ans = []
for i in range(n - 1, -1, -1):
while q and temperatures[q[-1]] <= temperatures[i]:
q.pop(-1)
if q:
ans.append(q[-1] - i)
else:
ans.append(0)
q.append(i)
return ans[::-1]
# # # 84. Largest Rectangle in Histogram
# # class Solution:
# # def largestRectangleArea(self, heights: List[int]) -> int:
# # stk = []
# # ans = 0
# # n = len(heights)
# # for i, h in enumerate(heights):
# # while stk and heights[stk[-1]]>=h:
# # start = stk.pop()
# # width = i if not stk else i-stk[-1]-1
# # ans = max(ans, heights[start]*width)
# # stk.append(i)
# # while stk:
# # start = stk.pop()
# # width = n if not stk else n-stk[-1]-1
# # ans = max(ans, heights[start]*width)
# # return ans
#
# # !/bin/python
# # -*- coding: utf8 -*-
# import sys
# import os
# import re
#
#
# # 请完成下面这个函数,实现题目要求的功能
# # 当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^
# # ******************************开始写代码******************************
#
#
# def nextGreaterElements(nums):
# n = len(nums)
# i=0
# stk = []
# ans = [-1]*n
# while i<2*n:
# ii = i%n
# while stk and nums[stk[-1]]<nums[ii]:
# last = stk.pop()
# if ans[last]==-1:
# ans[last] = nums[ii]
# stk.append(ii)
# i+=1
# return ans
#
#
#
#
# # ******************************结束写代码******************************
#
#
# _nums_cnt = 0
# _nums_cnt = int(input())
# _nums_i = 0
# _nums = []
# while _nums_i < _nums_cnt:
# _nums_item = int(input())
# _nums.append(_nums_item)
# _nums_i += 1
#
# res = nextGreaterElements(_nums)
#
# for res_cur in res:
# print(str(res_cur) + "\n")
# !/bin/python
# -*- coding: utf8 -*-
import sys
import os
import re
# 请完成下面这个函数,实现题目要求的功能
# ******************************开始写代码******************************
def seeCows(cows):
n = len(cows)
ans = 0
stk = []
cows.append(float("inf"))
for i in range(n+1):
while stk and cows[stk[-1]]<=cows[i]:
top = stk.pop()
# print(stk, top, i)
ans += (i-top-1)
stk.append(i)
return ans
# ******************************结束写代码******************************
arr = input()
cows = [int(item.group(0)) for item in re.finditer(r'-?\d+', arr)]
res = seeCows(cows)
print(res)