-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 0739: Daily Temperatures
- Loading branch information
Showing
3 changed files
with
60 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,38 @@ | ||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn daily_temperatures(temperatures: Vec<i32>) -> Vec<i32> { | ||
let mut result = vec![0; temperatures.len()]; | ||
'loop1: for idx in (0..temperatures.len() - 1).rev() { | ||
if temperatures[idx] < temperatures[idx + 1] { | ||
result[idx] = 1; | ||
} else { | ||
let mut offset = result[idx + 1] as usize + 1; | ||
while temperatures[idx + offset] <= temperatures[idx] { | ||
if result[idx + offset] == 0 { | ||
result[idx] = 0; | ||
continue 'loop1; | ||
} | ||
offset += result[idx + offset] as usize; | ||
} | ||
result[idx] = offset as i32; | ||
} | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
impl super::Solution for Solution { | ||
fn daily_temperatures(t: Vec<i32>) -> Vec<i32> { | ||
Self::daily_temperatures(t) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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,21 @@ | ||
pub mod dp; | ||
|
||
pub trait Solution { | ||
fn daily_temperatures(t: Vec<i32>) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [( | ||
&[73, 74, 75, 71, 69, 72, 76, 73] as &[_], | ||
&[1, 1, 4, 2, 1, 1, 0, 0] as &[_], | ||
)]; | ||
|
||
for (t, expected) in test_cases { | ||
assert_eq!(S::daily_temperatures(t.to_vec()), expected); | ||
} | ||
} | ||
} |