Created
July 2, 2020 11:22
-
-
Save alldroll/851e9e7ae927afd12f978f6c79377961 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func nextGreaterElement(nums1 []int, nums2 []int) []int { | |
stack := []int{} | |
table := make(map[int]int) | |
for _, num := range nums2 { | |
for len(stack) > 0 && stack[len(stack) - 1] < num { | |
item := stack[len(stack) - 1] | |
stack = stack[:len(stack) - 1] | |
table[item] = num | |
} | |
stack = append(stack, num) | |
} | |
for len(stack) > 0 { | |
item := stack[len(stack) - 1] | |
stack = stack[:len(stack) - 1] | |
table[item] = -1 | |
} | |
result := []int{} | |
for _, num := range nums1 { | |
result = append(result, table[num]) | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment