-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextGreaterElementI_Day22.py
More file actions
57 lines (45 loc) · 1.52 KB
/
Copy pathNextGreaterElementI_Day22.py
File metadata and controls
57 lines (45 loc) · 1.52 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#Brute Force Approach
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
res = []
for num in nums1:
found = False
next_greater = -1
for i in range(len(nums2)):
if nums2[i] == num:
found = True
if found and nums2[i] > num:
next_greater = nums2[i]
break
res.append(next_greater)
return res
# Time Complexity: O(n * m)
# Space Complexity: O(1) (excluding result list)
#Better Approach
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
res = []
for num in nums1:
index = nums2.index(num)
next_greater = -1
for j in range(index + 1, len(nums2)):
if nums2[j] > num:
next_greater = nums2[j]
break
res.append(next_greater)
return res
# Time Complexity: O(n * m)
# Space Complexity: O(1)
#Optimal Approach
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
stack = []
next_greater_map = {}
for num in reversed(nums2):
while stack and stack[-1] <= num:
stack.pop()
next_greater_map[num] = stack[-1] if stack else -1
stack.append(num)
return [next_greater_map[num] for num in nums1]
# Time Complexity: O(n + m)
# Space Complexity: O(m)