Skip to content

Commit 00fbe40

Browse files
committed
Add problem 1871: Jump Game VII
1 parent b780f2b commit 00fbe40

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,7 @@ pub mod problem_1865_finding_pairs_with_a_certain_sum;
13921392
pub mod problem_1866_number_of_ways_to_rearrange_sticks_with_k_sticks_visible;
13931393
pub mod problem_1869_longer_contiguous_segments_of_ones_than_zeros;
13941394
pub mod problem_1870_minimum_speed_to_arrive_on_time;
1395+
pub mod problem_1871_jump_game_vii;
13951396
pub mod problem_1876_substrings_of_size_three_with_distinct_characters;
13961397
pub mod problem_1877_minimize_maximum_pair_sum_in_array;
13971398
pub mod problem_1880_check_if_word_equals_summation_of_two_words;

src/problem_1871_jump_game_vii/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod sliding_window;
2+
3+
pub trait Solution {
4+
fn can_reach(s: String, min_jump: i32, max_jump: i32) -> bool;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [(("011010", 2, 3), true), (("01101110", 2, 3), false)];
13+
14+
for ((s, min_jump, max_jump), expected) in test_cases {
15+
assert_eq!(S::can_reach(s.to_string(), min_jump, max_jump), expected);
16+
}
17+
}
18+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::cell::Cell;
6+
7+
impl Solution {
8+
pub fn can_reach(s: String, min_jump: i32, max_jump: i32) -> bool {
9+
let mut s = s.into_bytes();
10+
let min_jump = min_jump as u32 as usize;
11+
let max_jump = max_jump as u32 as usize;
12+
13+
s[1..min_jump].fill(b'1');
14+
15+
let mut count = 0;
16+
17+
for &c in &s[..=max_jump - min_jump] {
18+
count += u32::from(b'1' - c);
19+
}
20+
21+
let s = Cell::from_mut(s.as_mut_slice()).as_slice_of_cells();
22+
23+
for ((old, new), target) in s.iter().zip(&s[max_jump - min_jump + 1..]).zip(&s[max_jump + 1..]) {
24+
count = count.wrapping_add(i32::from(old.get().wrapping_sub(new.get()) as i8) as u32);
25+
26+
if target.get() == b'0' {
27+
target.set(b'1' - u8::from(count != 0));
28+
}
29+
}
30+
31+
s.last().unwrap().get() == b'0'
32+
}
33+
}
34+
35+
// ------------------------------------------------------ snip ------------------------------------------------------ //
36+
37+
impl super::Solution for Solution {
38+
fn can_reach(s: String, min_jump: i32, max_jump: i32) -> bool {
39+
Self::can_reach(s, min_jump, max_jump)
40+
}
41+
}
42+
43+
#[cfg(test)]
44+
mod tests {
45+
#[test]
46+
fn test_solution() {
47+
super::super::tests::run::<super::Solution>();
48+
}
49+
}

0 commit comments

Comments
 (0)