-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path503_next_greater_element_ii.py
56 lines (41 loc) · 1.32 KB
/
503_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
# https://leetcode.com/problems/next-greater-element-ii/
from typing import List
class SolutionLeha:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
m = max(nums)
ms = []
answer = [-1]* len(nums)
ind_max = nums.index(m)
l = len(nums)
nwi = []
for i,el in enumerate(nums):
nwi.append([i,el])
for i in range(l+1):
elem = nwi[ (i + ind_max)%l ]
while ms and elem[1] > ms[-1][1]:
answer[ms[-1][0]] = elem[1]
ms.pop()
ms.append(elem)
return answer
class SolutionLexa2:
"""
2 stacks - one with elements, other with their indicies.
"""
def nextGreaterElements(self, nums: List[int]) -> List[int]:
m = max(nums)
ms = []
msind = []
answer = [-1]* len(nums)
ind_max = nums.index(m)
l = len(nums)
for i in range(l+1):
elem = nums[ (i + ind_max)%l ]
counter_del = 0
while ms and elem > ms[-1]:
answer[ msind[-1] ] = elem
ms.pop()
msind.pop()
ms.append(elem)
msind.append((i + ind_max)%l)
return answer
#TODO: my solution.