-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmax-consecutive-ones-iii.rs
50 lines (44 loc) · 1.09 KB
/
max-consecutive-ones-iii.rs
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
#![allow(dead_code, unused, unused_variables)]
fn main() {
assert_eq!(
6,
Solution::longest_ones(vec![1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0], 2)
);
assert_eq!(
10,
Solution::longest_ones(
vec![0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1],
3
)
);
}
struct Solution;
impl Solution {
pub fn longest_ones(a: Vec<i32>, mut k: i32) -> i32 {
let (mut slow, mut fast, mut count) = (0, 0, 0);
while fast < a.len() {
if k > 0 {
if a[fast] == 0 {
k -= 1;
}
fast += 1;
} else {
if a[fast] == 1 {
fast += 1;
} else {
if a[slow] == 0 {
fast += 1
}
slow += 1;
}
}
if fast < slow {
fast = slow;
}
if fast - slow > count {
count = fast - slow;
}
}
count as i32
}
}