Skip to content

Commit

Permalink
leetcode: Add 0739
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed May 8, 2024
1 parent e066fd1 commit dcce06b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [跳跃表 SkipList](list/skiplist/index.md)
- [链表相关的问题列表](leetcode/tags/linked-list.md)
- [栈 Stack](stack/index.md)
- [单调栈 Monotonic Stack](stack/monotonic-stack.md)
- [栈相关的问题列表](leetcode/tags/stack.md)
- [队列 Queue](queue/index.md)
- [优先级队列 Priority Queue](priority-queue/index.md)
Expand Down
7 changes: 7 additions & 0 deletions src/leetcode/0739.daily-temperatures/Cargo.toml
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]
4 changes: 4 additions & 0 deletions src/leetcode/0739.daily-temperatures/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#

[问题描述](../problems/)
80 changes: 80 additions & 0 deletions src/leetcode/0739.daily-temperatures/src/main.rs
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);
}
}
2 changes: 2 additions & 0 deletions src/stack/monotonic-stack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

# 单调栈 Monotonic Stack

0 comments on commit dcce06b

Please sign in to comment.