-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0135_candy.rs
More file actions
54 lines (52 loc) · 1.29 KB
/
s0135_candy.rs
File metadata and controls
54 lines (52 loc) · 1.29 KB
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
#![allow(unused)]
pub struct Solution {}
// https://leetcode.com/problems/candy/description/
//
use std::cmp::max;
impl Solution {
pub fn count(n: i32) -> i32 {
(n * (n + 1)) / 2
}
pub fn candy(ratings: Vec<i32>) -> i32 {
if ratings.len() < 2 {
return ratings.len() as i32
}
let mut candies = 0;
let mut up = 0;
let mut down = 0;
let mut old_slope = 0;
for i in 1..ratings.len() {
let mut new_slope = if ratings[i] > ratings[i -1] {
1
}else if ratings[i] < ratings [i -1] {
-1
} else {
0
};
if (old_slope > 0 && new_slope == 0 ) || (old_slope < 0 && new_slope >= 0) {
candies += Solution::count(up) + Solution::count(down) + max(up, down);
up = 0;
down = 0;
}
if new_slope > 0 {
up += 1;
} else if new_slope < 0 {
down += 1;
} else {
candies += 1;
}
old_slope = new_slope;
}
candies += Solution::count(up) + Solution::count(down) + max(up , down) +1;
candies
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_135() {
assert_eq!(Solution::candy(vec![1, 0, 2]), 5);
assert_eq!(Solution::candy(vec![1, 2, 2]), 4);
}
}