-
Notifications
You must be signed in to change notification settings - Fork 0
/
daily_temperatures.rs
62 lines (51 loc) · 1.68 KB
/
daily_temperatures.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
62
/// Given an array of integers `temperatures` represents the daily
/// temperatures, return an array `answer` such that `answer[i]` is the number
/// of days you have to wait after the `ith` day to get a warmer temperature.
/// If there is no future day for which this is possible, keep answer[i] == 0
/// instead.
struct Solution;
impl Solution {
pub fn daily_temperatures(temperatures: Vec<i32>) -> Vec<i32> {
let n = temperatures.len();
let mut stack: Vec<usize> = Vec::new();
let mut result = vec![0; n];
for i in 0..n {
let current = temperatures[i];
while !stack.is_empty() {
let n = stack.len();
let index = stack[n-1];
let previous = temperatures[index];
if current > previous {
result[index] = (i - index) as i32;
stack.pop();
} else {
break;
}
}
stack.push(i);
}
result
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let temperatures = vec![73, 74, 75, 71, 69, 72, 76, 73];
let result = Solution::daily_temperatures(temperatures);
assert_eq!(result, vec![1, 1, 4, 2, 1, 1, 0, 0]);
}
#[test]
fn example_2() {
let temperatures = vec![30, 40, 50, 60];
let result = Solution::daily_temperatures(temperatures);
assert_eq!(result, vec![1, 1, 1, 0]);
}
#[test]
fn example_3() {
let temperatures = vec![30, 60, 90];
let result = Solution::daily_temperatures(temperatures);
assert_eq!(result, vec![1, 1, 0]);
}
}