-
Notifications
You must be signed in to change notification settings - Fork 43
/
next_greater_element_ii.py
311 lines (281 loc) · 9.61 KB
/
next_greater_element_ii.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""
503. Next Greater Element II
Medium
Given a circular integer array nums (i.e., the next element of nums[nums.length - 1] is nums[0]), return the next greater number for every element in nums.
The next greater number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, return -1 for this number.
Example 1:
Input: nums = [1,2,1]
Output: [2,-1,2]
Explanation: The first 1's next greater number is 2;
The number 2 can't find next greater number.
The second 1's next greater number needs to search circularly, which is also 2.
Example 2:
Input: nums = [1,2,3,4,3]
Output: [2,3,4,-1,4]
Constraints:
1 <= nums.length <= 104
-109 <= nums[i] <= 109
"""
# V0
# IDEA : LC 739
class Solution(object):
def nextGreaterElements(self, nums):
# edge case
if not nums:
return
_len = len(nums)
# note : we init res as [-1] * _len
res = [-1] * _len
# note : we use "nums = 2 * nums" to simuldate "circular array"
nums = 2 * nums
stack = [] # [[idx, val]]
for idx, val in enumerate(nums):
while stack and stack[-1][1] < val:
_idx, _val = stack.pop(-1)
"""
NOTE !!!
-> we get remainder via "_idx % _len" for handling idx issue
(since we made nums = 2 * nums earlier)
"""
res[_idx % _len] = val
stack.append([idx, val])
return res
# V0'
# IDEA : STACK + circular loop handling
class Solution:
def nextGreaterElements(self, nums):
stack = []
res = [-1] * len(nums)
for i in range(2*len(nums)-1, -1, -1):
index = i % len(nums)
while stack and stack[-1] <= nums[index]:
stack.pop()
if stack:
res[index] = stack[-1]
stack.append(nums[index])
return res
# V0'
# IDEA : STACK + circular loop handling
class Solution(object):
def nextGreaterElements(self, nums):
# edge case
if not nums:
return
_len = len(nums)
res = [-1] * _len
nums2 = nums * 2
stack = []
# # NOTE this !!! we loop from "nums2" with inverse ordering
for i in range(len(nums2)-1, -1, -1):
while stack and stack[-1] <= nums2[i]:
stack.pop(-1)
if stack:
# NOTE this !!! (handling cyclic array)
res[ i % _len ] = stack[-1]
stack.append(nums2[i])
return res
# V0'
# IDEA : STACK + circular loop handling
class Solution:
def nextGreaterElements(self, nums):
### NOTE : since we can search nums circurly,
# -> so here we make a new array (augLst = nums + nums) for that
augLst = nums + nums
stack = []
# init ans
res = [-1] * len(nums)
### NOTE : we looping augLst with inverse order
for i in range(len(augLst)-1, -1, -1):
### NOTE : if stack and last value in stack smaller than augLst[i], we pop last value from stack
while stack and stack[-1] <= augLst[i]:
stack.pop()
### NOTE : the remaining element in stack must fit the condition, so we append it to res
# -> note : append to `i % len(nums)` idx in res
if stack:
res[i % len(nums)] = stack[-1]
### NOTE : we also need to append augLst[i] to stack
stack.append(augLst[i])
return res
# V0''
# IDER : brute force (for + while loop) : TLE
class Solution(object):
def nextGreaterElements(self, nums):
# edge case
if not nums:
return
res = []
nums2 = nums * 2
for i in range(len(nums)):
found = False
tmp_nums = nums2[i+1:]
while tmp_nums:
cur = tmp_nums.pop(0)
if cur > nums[i]:
res.append(cur)
found = True
break
if not found:
res.append(-1)
#print ("")
return res
# V1
# https://leetcode.com/problems/next-greater-element-ii/discuss/184046/Python-solution
class Solution:
def nextGreaterElements(self, nums):
augLst = nums + nums
stack = []
res = [-1] * len(nums)
for i in range(len(augLst)-1, -1, -1):
while stack and stack[-1] <= augLst[i]:
stack.pop()
if stack:
res[i % len(nums)] = stack[-1]
stack.append(augLst[i])
return res
# V1'
# https://leetcode.com/problems/next-greater-element-ii/discuss/184046/Python-solution
class Solution:
def nextGreaterElements(self, nums):
stack = []
res = [-1] * len(nums)
for i in range(2*len(nums)-1, -1, -1):
index = i % len(nums)
while stack and stack[-1] <= nums[index]:
stack.pop()
if stack:
res[index] = stack[-1]
stack.append(nums[index])
return res
# V1''
# https://leetcode.com/problems/next-greater-element-ii/discuss/295380/Python-Solution
# IDEA
# I performed Next Greater Element I from right to left twice
# The double pass allows you to simulate circulation
# I came up with this during an onsite and amazed myself that this actually worked when I came here to test it
class Solution:
def nextGreaterElements(self, nums):
st = []
nlen = len(nums)
res = [-1] * nlen
for times in range(2):
for idx in range(nlen - 1, -1, -1):
while st and st[-1] <= nums[idx]:
st.pop()
res[idx] = st[-1] if st else res[idx]
st.append(nums[idx])
return res
# v1''''
# https://leetcode.com/problems/next-greater-element-ii/discuss/743506/Python
class Solution:
def nextGreaterElements(self, nums):
st = []
n = len(nums)
res = [-1]*n
for i in range(2*n-1,-1,-1):
num = nums[i%n]
if not st:
st.append(num)
else:
while st and st[-1]<=num:
st.pop()
if not st:
res[i%n] = -1
else:
res[i%n] = st[-1]
st.append(num)
return res
# V1'''''
# IDEA : Brute Force (using Double Length Array) [Time Limit Exceeded]
# https://leetcode.com/problems/next-greater-element-ii/solution/
# JAVA
# public class Solution {
#
# public int[] nextGreaterElements(int[] nums) {
# int[] res = new int[nums.length];
# int[] doublenums = new int[nums.length * 2];
# System.arraycopy(nums, 0, doublenums, 0, nums.length);
# System.arraycopy(nums, 0, doublenums, nums.length, nums.length);
# for (int i = 0; i < nums.length; i++) {
# res[i]=-1;
# for (int j = i + 1; j < doublenums.length; j++) {
# if (doublenums[j] > doublenums[i]) {
# res[i] = doublenums[j];
# break;
# }
# }
# }
# return res;
# }
# }
# V1''''''
# IDEA : Brute Force (using Double Length Array) (ACCEPTED)
# https://leetcode.com/problems/next-greater-element-ii/solution/
# JAVA
# public class Solution {
# public int[] nextGreaterElements(int[] nums) {
# int[] res = new int[nums.length];
# for (int i = 0; i < nums.length; i++) {
# res[i] = -1;
# for (int j = 1; j < nums.length; j++) {
# if (nums[(i + j) % nums.length] > nums[i]) {
# res[i] = nums[(i + j) % nums.length];
# break;
# }
# }
# }
# return res;
# }
# }
# V1''''''
# IDEA : Stack (ACCEPTED)
# https://leetcode.com/problems/next-greater-element-ii/solution/
# JAVA
# public class Solution {
#
# public int[] nextGreaterElements(int[] nums) {
# int[] res = new int[nums.length];
# Stack<Integer> stack = new Stack<>();
# for (int i = 2 * nums.length - 1; i >= 0; --i) {
# while (!stack.empty() && nums[stack.peek()] <= nums[i % nums.length]) {
# stack.pop();
# }
# res[i % nums.length] = stack.empty() ? -1 : nums[stack.peek()];
# stack.push(i % nums.length);
# }
# return res;
# }
# }
# V1'''''''
# https://blog.techbridge.cc/2019/10/26/leetcode-pattern-next-greater-element/
# C++
# class Solution {
# public:
# vector<int> nextGreaterElements(vector<int>& nums) {
# // Use a stack to get next greater element efficiently
# vector<int> ans(nums.size(), -1);
# stack< pair<int, int> > st; // store <value, index> to deal with duplicate values
#
# // Go through array twice to handle circular property
# for(int i = 0; i < 2 * nums.size(); ++i) {
# int idx = i % nums.size();
# while( !st.empty() and st.top().first < nums[idx] ) {
# pair<int, int> cur = st.top();
# st.pop();
#
# // Because we go through nums twice
# // we might update some ans twice (which we do not desire)
# // so we only update if ans[cur.second] == -1
# if(ans[cur.second] == -1) {
# ans[cur.second] = nums[idx];
# }
# }
#
# if(ans[idx] == -1) {
# st.push( make_pair(nums[idx], idx) );
# }
# }
#
# return ans;
# }
# };
# V2