-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "lc-0739-daily-temperatures" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
# | ||
|
||
[问题描述](../problems/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
// Brute force | ||
pub fn daily_temperatures1(temperatures: Vec<i32>) -> Vec<i32> { | ||
assert!(!temperatures.is_empty()); | ||
|
||
let mut out = Vec::new(); | ||
let len = temperatures.len(); | ||
for i in 0..len { | ||
let mut diff = 0; | ||
for j in i..len { | ||
if temperatures[j] > temperatures[i] { | ||
diff = j - i; | ||
break; | ||
} | ||
} | ||
out.push(diff as i32); | ||
} | ||
out | ||
} | ||
|
||
// Stack | ||
// 单调递增栈 | ||
pub fn daily_temperatures2(temperatures: Vec<i32>) -> Vec<i32> { | ||
assert!(!temperatures.is_empty()); | ||
|
||
let len = temperatures.len(); | ||
let mut out = vec![0; len]; | ||
// 存储温度值所在数组的下标 | ||
let mut stack: Vec<usize> = Vec::new(); | ||
|
||
// 遍历数组 | ||
for (index, &num) in temperatures.iter().enumerate() { | ||
// 如果栈不为空, 且当前温度比栈顶的温度值高, 则先将栈顶元素出栈. | ||
while !stack.is_empty() && num > temperatures[*stack.last().unwrap()] { | ||
let top_index = stack.pop().unwrap(); | ||
// 计算下标的偏移量. | ||
out[top_index] = (index - top_index) as i32; | ||
} | ||
// 将当前温度值的下标入栈 | ||
stack.push(index); | ||
} | ||
|
||
out | ||
} | ||
|
||
pub type SolutionFn = fn(Vec<i32>) -> Vec<i32>; | ||
|
||
fn check_solution(func: SolutionFn) { | ||
let temperatures = vec![73, 74, 75, 71, 69, 72, 76, 73]; | ||
assert_eq!(&func(temperatures), &[1, 1, 4, 2, 1, 1, 0, 0]); | ||
|
||
let temperatures = vec![30, 40, 50, 60]; | ||
assert_eq!(&func(temperatures), &[1, 1, 1, 0]); | ||
|
||
let temperatures = vec![30, 60, 90]; | ||
assert_eq!(&func(temperatures), &[1, 1, 0]); | ||
} | ||
|
||
fn main() { | ||
check_solution(daily_temperatures1); | ||
check_solution(daily_temperatures2); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{check_solution, daily_temperatures1, daily_temperatures2}; | ||
|
||
#[test] | ||
fn test_daily_temperatures1() { | ||
check_solution(daily_temperatures1); | ||
} | ||
|
||
#[test] | ||
fn test_daily_temperatures2() { | ||
check_solution(daily_temperatures2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
# 单调栈 Monotonic Stack |