496. Next Greater Element I Easy

@problem@discussion
#Array#Hash Table#Stack#Monotonic Stack



1/**
2 * [496] Next Greater Element I
3 *
4 * The next greater element of some element x in an array is the first greater element that is to the right of x in the same array.
5 * You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2.
6 * For each 0 <= i < nums1.length, find the index j such that nums1[i] == nums2[j] and determine the next greater element of nums2[j] in nums2. If there is no next greater element, then the answer for this query is -1.
7 * Return an array ans of length nums1.length such that ans[i] is the next greater element as described above.
8 *  
9 * Example 1:
10 * 
11 * Input: nums1 = [4,1,2], nums2 = [1,3,4,2]
12 * Output: [-1,3,-1]
13 * Explanation: The next greater element for each value of nums1 is as follows:
14 * - 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
15 * - 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
16 * - 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
17 * 
18 * Example 2:
19 * 
20 * Input: nums1 = [2,4], nums2 = [1,2,3,4]
21 * Output: [3,-1]
22 * Explanation: The next greater element for each value of nums1 is as follows:
23 * - 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
24 * - 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
25 * 
26 *  
27 * Constraints:
28 * 
29 * 1 <= nums1.length <= nums2.length <= 1000
30 * 0 <= nums1[i], nums2[i] <= 10^4
31 * All integers in nums1 and nums2 are unique.
32 * All the integers of nums1 also appear in nums2.
33 *
34 *  
35 * Follow up: Could you find an O(nums1.length + nums2.length) solution?
36 */
37use std::collections::HashMap;
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/next-greater-element-i/
41// discuss: https://leetcode.com/problems/next-greater-element-i/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45
46impl Solution {
47    pub fn next_greater_element(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
48        let mut set: HashMap<i32, i32> = HashMap::new();
49        let mut stack: Vec<i32> = vec![];
50
51        for n in nums2 {
52            while !stack.is_empty() && stack[stack.len() - 1] < n {
53                if let Some(x) = stack.pop() {
54                    set.insert(x, n);
55                }
56            }
57            stack.push(n)
58        }
59
60        let mut ans = vec![];
61        for (_i, v) in nums1.iter().enumerate() {
62            ans.push(match set.get(v) {
63                Some(value) => *value,
64                None => -1,
65            })
66        }
67
68        ans
69    }
70}
71
72// submission codes end
73
74#[cfg(test)]
75mod tests {
76    use std::vec;
77
78    use super::*;
79
80    #[test]
81    fn test_496() {
82        println!("Got {:#?}", Solution::next_greater_element(vec![4,1,2], vec![1,3,4,2]));
83    }
84}
85


Back
© 2025 bowen.ge All Rights Reserved.