2454. Next Greater Element IV Hard

@problem@discussion
#Array#Binary Search#Stack#Sorting#Heap (Priority Queue)#Monotonic Stack



1/**
2 * [2454] Next Greater Element IV
3 *
4 * You are given a 0-indexed array of non-negative integers nums. For each integer in nums, you must find its respective second greater integer.
5 * The second greater integer of nums[i] is nums[j] such that:
6 * 
7 * 	j > i
8 * 	nums[j] > nums[i]
9 * 	There exists exactly one index k such that nums[k] > nums[i] and i < k < j.
10 * 
11 * If there is no such nums[j], the second greater integer is considered to be -1.
12 * 
13 * 	For example, in the array [1, 2, 4, 3], the second greater integer of 1 is 4, 2 is 3, and that of 3 and 4 is -1.
14 * 
15 * Return an integer array answer, where answer[i] is the second greater integer of nums[i].
16 *  
17 * <strong class="example">Example 1:
18 * 
19 * Input: nums = [2,4,0,9,6]
20 * Output: [9,6,6,-1,-1]
21 * Explanation:
22 * 0th index: 4 is the first integer greater than 2, and 9 is the second integer greater than 2, to the right of 2.
23 * 1st index: 9 is the first, and 6 is the second integer greater than 4, to the right of 4.
24 * 2nd index: 9 is the first, and 6 is the second integer greater than 0, to the right of 0.
25 * 3rd index: There is no integer greater than 9 to its right, so the second greater integer is considered to be -1.
26 * 4th index: There is no integer greater than 6 to its right, so the second greater integer is considered to be -1.
27 * Thus, we return [9,6,6,-1,-1].
28 * 
29 * <strong class="example">Example 2:
30 * 
31 * Input: nums = [3,3]
32 * Output: [-1,-1]
33 * Explanation:
34 * We return [-1,-1] since neither integer has any integer greater than it.
35 * 
36 *  
37 * Constraints:
38 * 
39 * 	1 <= nums.length <= 10^5
40 * 	0 <= nums[i] <= 10^9
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/next-greater-element-iv/
46// discuss: https://leetcode.com/problems/next-greater-element-iv/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn second_greater_element(nums: Vec<i32>) -> Vec<i32> {
52        vec![]
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_2454() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.