-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcheck-if-point-is-reachable.rs
52 lines (50 loc) · 1.41 KB
/
check-if-point-is-reachable.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
51
52
// 2543. Check if Point Is Reachable
// 🔴 Hard
//
// https://leetcode.com/problems/check-if-point-is-reachable/
//
// Tags: Greedy - Dynamic Programming - Math
struct Solution;
impl Solution {
// Simulate the movements that are allowed but in reverse, starting at
// the target point and trying to reach (1, 1).
//
// Time complexity: O(log(max(m, n))) - At each step we divide by 2
// approximately, if one of the values is not divisible one loop, it will
// became divisible in the next loop.
// Space complexity: O(1) - We only store two integers and one tuple with
// two elements.
//
// Runtime 1 ms Beats 100%
// Memory 2.2 MB Beats 100%
pub fn is_reachable(target_x: i32, target_y: i32) -> bool {
let mut last = (-1, -1);
let mut x = target_x;
let mut y = target_y;
while (x, y) != last {
if x == 1 && y == 1 {
return true;
}
last = (x, y);
while x % 2 == 0 {
x /= 2;
}
while y % 2 == 0 {
y /= 2;
}
if x > y {
x -= y;
}
if y > x {
y -= x
}
}
false
}
}
// Tests.
fn main() {
assert_eq!(Solution::is_reachable(6, 9), false);
assert_eq!(Solution::is_reachable(4, 7), true);
println!("All tests passed!")
}