#STK0000083. Phần tử lớn hơn tiếp theo I (Next Greater Element I)
Phần tử lớn hơn tiếp theo I (Next Greater Element I)
Next Greater Element I
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given two arrays nums1 and nums2 whose elements are pairwise distinct, and every element of nums1 occurs in nums2. For each value in nums1, locate in nums2 and find the first element to its right whose value is greater than . If none exists, the answer is .
Input
The first line contains and , the lengths of nums1 and nums2. The second line contains nums1. The third line contains nums2.
Output
Print integers in nums1 order, each being the corresponding next greater element in nums2, or .
Subtasks
- Subtask 1 (30 points): Original constraints, and
nums2is strictly increasing. - Subtask 2 (70 points): , ; mỗi mảng không có phần tử lặp và
nums1là tập con củanums2.
Examples
Input
3 4
4 1 2
1 3 4 2
Output
-1 3 -1
Explanation
Here nums1 = [4,1,2] and nums2 = [1,3,4,2].
- Value is at the third position of
nums2. Only lies to its right, so there is no next greater element: the answer is . - Value is at the first position of
nums2. The immediately following value is , so the answer is . - Value is at the last position of
nums2, so nothing lies to its right: the answer is .
In nums1 order, the program prints -1 3 -1.