Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Next Greater Element I
Bookmark
Brute Force
Stack + Hash Map
Input
Example 1
Example 2
Example 3
Custom
nums1
=
[4, 1, 2]
,
nums2
=
[1, 3, 4, 2]
nums1
4
1
2
nums2
1
0
3
1
4
2
2
3
result
-1
-1
-1
nums1
4
1
2
nums2
1
0
3
1
4
2
2
3
result
-1
-1
-1
nums1
4
1
2
j
nums2
1
0
3
1
4
2
2
3
result
-1
-1
-1
looking for the next greater of
4
nums1
4
1
2
j
nums2
1
0
3
1
4
2
2
3
result
-1
-1
-1
looking for the next greater of
4
nums1
4
1
2
j
nums2
1
0
3
1
4
2
2
3
k
result
-1
-1
-1
nothing bigger to the right →
-1
nums1
4
1
2
j
nums2
1
0
3
1
4
2
2
3
result
-1
-1
-1
looking for the next greater of
1
nums1
4
1
2
j
nums2
1
0
3
1
4
2
2
3
result
-1
-1
-1
looking for the next greater of
1
nums1
4
1
2
j
nums2
1
0
3
1
4
2
2
3
k
result
-1
3
-1
result[1] =
3
nums1
4
1
2
j
nums2
1
0
3
1
4
2
2
3
result
-1
3
-1
looking for the next greater of
2
nums1
4
1
2
j
nums2
1
0
3
1
4
2
2
3
result
-1
3
-1
nothing bigger to the right →
-1
nums1
4
1
2
nums2
1
0
3
1
4
2
2
3
result
-1
3
-1
return
[-1, 3, -1]
algo
master
.
io
Step:
Brute force: for each nums1 value, scan nums2 to its right for a bigger one.
0 / 15
Input
Example 1
Example 2
Example 3
Custom
nums1
=
[4, 1, 2]
,
nums2
=
[1, 3, 4, 2]
0 / 15
algo
master
.
io
Step:
Brute force: for each nums1 value, scan nums2 to its right for a bigger one.