LeetCode Entry
1871. Jump Game VII
Can reach end jumping min..max to zeros
1871. Jump Game VII medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1370
Problem TLDR
Can reach end jumping min..max to zeros
Intuition
- forward: put line sweep interval start-end events, slide
- backwards: slide window looking backwards, count reachable items inside window
Approach
- the end should not be ‘1’
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\)
Code
fun canReach(s: String, min: Int, max: Int): Boolean {
val e = IntArray(s.length+max+2); e[1] = -1
return s.last() == '0' && 0 < s.indices.fold(1) { c, i ->
if (s[i] == '0' && c+e[i] > 0) { e[i+min]++; e[i+max+1]-- }
c + e[i]
}
}
pub fn can_reach(s: String, l: i32, h: i32) -> bool {
let mut e = vec![0; s.len()+h as usize + 2]; e[1] = -1;
s.ends_with('0') && 0 < s.bytes().zip(0..).fold(1, |c, (v, i)| {
if v < 49 && c + e[i] > 0 { e[i+l as usize] += 1; e[i+h as usize+1] -= 1 }
c + e[i]
})
}