-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext-generater-element-II-503.py
More file actions
41 lines (33 loc) · 1.15 KB
/
Copy pathnext-generater-element-II-503.py
File metadata and controls
41 lines (33 loc) · 1.15 KB
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
from typing import List
class Solution:
def nextGreaterElements(self, nums: List[int]) -> list[int]:
n = len(nums)
result = [-1] * n
stack = []
for i in range(2 * n):
num = nums[i % n]
while stack and nums[stack[-1]] < num:
result[stack.pop()] = num
if i < n:
stack.append(i)
return result
# example
obj = Solution()
print(obj.nextGreaterElements(nums=[1,5,3,6,8]))
# [-1 ,5, 5, 5, 5]
"""Error solution"""
# class Solution:
# def nextGreaterElements(self, nums: List[int]) -> List[int]:
# max_num = max(nums)
# max_indx = nums.index(max_num)
# nums = nums + [0]
# for i in range(len(nums)-1):
# if nums[i] == max_num:
# nums[i] = -1
# elif nums[i] < nums[i+1] and i > max_indx:
# nums[i] = nums[i+1]
# elif nums[i] > nums[i+1] and i > max_indx:
# nums[i] = nums[i]+1
# # elif nums[i] < nums[i+1] and i < max_indx:
# # nums[i] = nums[i-1]
# return nums[:len(nums)-1]