LeetCode Entry
3665. Twisted Mirror Path Count
Count ways to move in grid with 1-mirrors
3665. Twisted Mirror Path Count medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1528
Problem TLDR
Count ways to move in grid with 1-mirrors
Intuition
DFS DP: cache by position and direction Bottom up DP: keep previous row as direction down and previos value as direction right, swap on mirrors
Approach
- Rust allows to modify array values while iterating it
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\)
Code
fun uniquePaths(g:Array<IntArray>): Int {
val d = IntArray(501); d[0] = 1; var a = 0
for (r in g) {
var R = 0
for (x in r.indices) {
a = (R + d[x]) % 1000000007
if (r[x] < 1) { d[x] = a; R = a } else R = d[x].also { d[x] = R }
}
}
return a
}
pub fn unique_paths(g: Vec<Vec<i32>>) -> i32 {
let (mut d, mut a) = ([0; 501], 0); d[0] = 1;
for r in g {
let mut R = 0;
for (D, v) in d.iter_mut().zip(r) {
a = (R + *D) % 1000000007;
(*D, R) = if v < 1 { (a, a) } else { (R, *D) }
}
} a
}
Comments