594. Next Greater Element I

EasyStackMonotonic StackStack

Given two arrays nums1 and nums2 where nums1 is a subset of the distinct array nums2, for each value in nums1 find the next greater element to its right within nums2, or -1 if none. Return the answers in the order of nums1. The input is JSON {nums1, nums2}.

Input: JSON {nums1, nums2}.

Output: Array — the next greater element for each value in nums1.

Examples

Example 1
Input: {"nums1":[4,1,2],"nums2":[1,3,4,2]}
Output: [-1,3,-1]
Explanation: 4 has none; 1 -> 3; 2 has none.
Example 2
Input: {"nums1":[2,4],"nums2":[1,2,3,4]}
Output: [3,-1]
Explanation: 2 -> 3; 4 has none.
Example 3
Input: {"nums1":[1],"nums2":[1]}
Output: [-1]
Explanation: No element to the right.

Constraints

Asked by

SwiggyBloombergAmazonFlipkartAccentureMicrosoft
Solve this problem in the editor →