Skip to content

Instantly share code, notes, and snippets.

@alldroll
Created July 2, 2020 11:22
Show Gist options
  • Save alldroll/851e9e7ae927afd12f978f6c79377961 to your computer and use it in GitHub Desktop.
Save alldroll/851e9e7ae927afd12f978f6c79377961 to your computer and use it in GitHub Desktop.
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