Input:
sx = 2, sy = 4, fx = 7, fy = 7, t = 6
Output:
true
Explanation: Starting at cell (2, 4), we can reach cell (7, 7) in exactly 6 seconds by going through the cells depicted in the picture above.
Example 2:
1
2
3
4
5
Input:
sx = 3, sy = 1, fx = 7, fy = 3, t = 3
Output:
false
Explanation: Starting at cell (3, 1), it takes at least 4 seconds to reach cell (7, 3) by going through the cells depicted in the picture above. Hence, we cannot reach cell (7, 3) at the third second.
On an infinite grid, you can move to any of the 8 adjacent cells in one second (like a king in chess). The minimum number of steps to reach (fx, fy) from (sx, sy) is the Chebyshev distance: max(|sx - fx|, |sy - fy|). You can always reach the target in exactly t steps if t is at least this distance, except for the special case where you start and end at the same cell and t == 1 (since you must move at least once).
classSolution {
public:bool isReachableAtTime(int sx, int sy, int fx, int fy, int t) {
if (sx == fx && sy == fy) return t !=1;
int d = max(abs(sx - fx), abs(sy - fy));
return d <= t;
}
};
classSolution {
publicbooleanisReachableAtTime(int sx, int sy, int fx, int fy, int t) {
if (sx == fx && sy == fy) return t != 1;
int d = Math.max(Math.abs(sx - fx), Math.abs(sy - fy));
return d <= t;
}
}
1
2
3
4
5
6
7
classSolution {
funisReachableAtTime(sx: Int, sy: Int, fx: Int, fy: Int, t: Int): Boolean {
if (sx == fx && sy == fy) return t !=1val d = maxOf(kotlin.math.abs(sx - fx), kotlin.math.abs(sy - fy))
return d <= t
}
}
1
2
3
4
5
6
classSolution:
defisReachableAtTime(self, sx: int, sy: int, fx: int, fy: int, t: int) -> bool:
if sx == fx and sy == fy:
return t !=1 d = max(abs(sx - fx), abs(sy - fy))
return d <= t
1
2
3
4
5
6
7
impl Solution {
pubfnis_reachable_at_time(sx: i32, sy: i32, fx: i32, fy: i32, t: i32) -> bool {
if sx == fx && sy == fy { return t !=1; }
let d = (sx - fx).abs().max((sy - fy).abs());
d <= t
}
}