20240418 Finished
This commit is contained in:
parent
65dd1924ed
commit
1fc3cd8e46
|
@ -105,3 +105,4 @@ mod p274_h_index;
|
|||
mod p380_insert_delete_getrandom_o1;
|
||||
mod p238_product_of_array_except_self;
|
||||
mod p134_gas_station;
|
||||
mod p135_candy;
|
40
src/problem/p135_candy.rs
Normal file
40
src/problem/p135_candy.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
/**
|
||||
* [135] Candy
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn candy(ratings: Vec<i32>) -> i32 {
|
||||
let n = ratings.len();
|
||||
let mut children = vec![1; n];
|
||||
|
||||
for i in 1..n {
|
||||
if ratings[i] > ratings[i - 1] {
|
||||
children[i] = children[i].max(children[i - 1] + 1);
|
||||
}
|
||||
}
|
||||
|
||||
for i in (0..n - 1).rev() {
|
||||
if ratings[i] > ratings[i + 1] {
|
||||
children[i] = children[i].max(children[i + 1] + 1);
|
||||
}
|
||||
}
|
||||
|
||||
children.iter().sum()
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_135() {
|
||||
assert_eq!(5, Solution::candy(vec![1, 0, 2]));
|
||||
assert_eq!(4, Solution::candy(vec![1, 2, 2]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user