Next Greater Element I
You get two arrays of distinct numbers, and every number of nums1 is also somewhere in nums2. For each number of nums1, find it in nums2 and take the first number to its right that is greater; if there is none, the answer is -1. Our example: nums1 = [4, 7, 2], nums2 = [4, 1, 3, 7, 2, 5] gives [7, -1, 5].
- input
nums1,nums2: two arrays of different numbers each; every number ofnums1is also innums2.- returns
- For each number of
nums1, the first number to its right innums2that is greater, or-1if there is none. LeetCode asks for O(nums1.length + nums2.length) as a follow-up. - constraints
- 1 ≤ nums1.length ≤ nums2.length ≤ 1000
- 0 ≤ nums1[i], nums2[i] ≤ 10⁴
- the numbers of nums1 are all different, and so are those of nums2
- every number of nums1 is also in nums2
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
function nextGreaterElement(nums1, nums2) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more monotonic stack problems
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.