-
Notifications
You must be signed in to change notification settings - Fork 7
/
Next Greater Element.py
75 lines (57 loc) · 2.6 KB
/
Next Greater Element.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
"""
Next Greater Element
Write a function that takes in an array of integers and returns a new array containing, at each index, the next element
in the input array that's greater than the element at that index in the input array. In other words, your function
should return a new array where outputArray[i] is the next element in the input array that's greater than inputArray[i]
.If there's no such next greater element for a particular index, the value at that index in the output array should be
-1 . For example, given array = [1, 2] , your function should return [2, -1] . Additionally, your function should treat
the input array as a circular array. A circular array wraps around itself as if it were connected end-to-end.
So the next index after the last index in a circular array is the first index. This means that, for our problem, given
array = [0, 0, 5, 0, 0, 3, 0 0] , the next greater element after 3 is 5 , since the array is circular.
Sample Input
array = [2, 5, -3, -4, 6, 7, 2]
Sample Output
[5, 6, 6, 6, 7, -1, 5]
"""
# SOLUTION 1
# O(n) time | O(n) space - where n is the length of the array.
def nextGreaterElement(array):
result = [-1] * len(array)
stack = []
for idx in range(2*len(array)):
circularIdx = idx % len(array)
while len(stack) > 0 and array[stack[len(stack) - 1]] < array[circularIdx]:
top = stack.pop()
result[top] = array[circularIdx]
stack.append(circularIdx)
return result
# SOLUTION 2
# O(n) time | O(n) space - where n is the length of the array.
def nextGreaterElement(array):
result = [-1] * len(array)
stack = []
for idx in range(2*len(array) - 1, -1, -1):
circularIdx = idx % len(array)
while len(stack) > 0:
if stack[len(stack) - 1] <= array[circularIdx]:
stack.pop()
else:
result[circularIdx] =stack[len(stack) - 1]
break
stack.append(array[circularIdx])
return result
# SOLUTION 3
# O(n^2) time | O(n) space - where n is the length of the array.
def nextGreaterElement(array):
greater_than = []
size = len(array)
for index in range(size):
for nextindex in range(index + 1, 2*size):
nextindex = nextindex % size
if array[nextindex] > array[index]:
greater_than.append(array[nextindex])
break
elif index == nextindex:
greater_than.append(-1)
break
return greater_than