-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0496-next-greater-element-I.rs
61 lines (52 loc) · 1.67 KB
/
0496-next-greater-element-I.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::collections::HashMap;
impl Solution {
pub fn next_greater_element(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
let map: HashMap<i32, usize> = nums1
.iter()
.cloned()
.enumerate()
.map(Self::reverse_tuple)
.collect();
let mut result = vec![0; nums1.len()];
for (index, val) in nums2.iter().enumerate() {
if map.contains_key(val) {
let idx = *map.get(val).unwrap();
let next_greater = nums2.iter().skip(index).find(|&x| x > val).unwrap_or(&-1);
result[idx] = *next_greater;
}
}
result
}
// O(nums1.length + nums2.length)
pub fn other_next_greater_element(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
let map: HashMap<i32, usize> = nums1
.iter()
.cloned()
.enumerate()
.map(Self::reverse_tuple)
.collect();
let mut result = vec![-1; nums1.len()];
let mut stack = Vec::new();
for (_, val) in nums2.iter().enumerate() {
while !stack.is_empty() && val > stack.last().unwrap() {
let value = stack.pop().unwrap();
let idx = *map.get(&value).unwrap();
result[idx] = value;
}
if map.contains_key(val) {
stack.push(*val);
}
}
result
}
pub fn reverse_tuple<T, U>(tup: (T, U)) -> (U, T)
where
T: Default,
U: Default,
{
let mut new_tup: (U, T) = (U::default(), T::default());
new_tup.0 = tup.1;
new_tup.1 = tup.0;
new_tup
}
}