Skip to content

Commit 2797272

Browse files
committed
add 2849. Determine if a Cell Is Reachable at a Given Time
https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/
1 parent a6f0186 commit 2797272

6 files changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public boolean isReachableAtTime(int sx, int sy, int fx, int fy, int t) {
3+
int dx = Math.abs(sx - fx);
4+
int dy = Math.abs(sy - fy);
5+
6+
if (dx == 0 && dy == 0) {
7+
return t != 1;
8+
} else {
9+
return Math.max(dx, dy) <= t;
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Determines if a cell is reachable at a given time.
3+
*
4+
* @param {number} sx - Starting x-coordinate
5+
* @param {number} sy - Starting y-coordinate
6+
* @param {number} fx - Ending x-coordinate
7+
* @param {number} fy - Ending y-coordinate
8+
* @param {number} t - Time in seconds
9+
* @return {boolean} - Returns true if the cell can be reached in exactly t seconds, or false otherwise
10+
*/
11+
var isReachableAtTime = function (sx, sy, fx, fy, t) {
12+
const dx = Math.abs(sx - fx);
13+
const dy = Math.abs(sy - fy);
14+
15+
if (dx === 0 && dy === 0) {
16+
return t !== 1;
17+
} else {
18+
return Math.max(dx, dy) <= t;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def isReachableAtTime(self, sx: int, sy: int, fx: int, fy: int, t: int) -> bool:
3+
dx = abs(sx - fx)
4+
dy = abs(sy - fy)
5+
6+
if dx == 0 and dy == 0:
7+
return t != 1
8+
else:
9+
return max(dx, dy) <= t
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
impl Solution {
2+
pub fn is_reachable_at_time(sx: i32, sy: i32, fx: i32, fy: i32, t: i32) -> bool {
3+
let dx = (sx - fx).abs();
4+
let dy = (sy - fy).abs();
5+
if dx == 0 && dy == 0 {
6+
t != 1
7+
} else {
8+
dx.max(dy) <= t
9+
}
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function isReachableAtTime(
2+
sx: number,
3+
sy: number,
4+
fx: number,
5+
fy: number,
6+
t: number
7+
): boolean {
8+
const dx = Math.abs(sx - fx);
9+
const dy = Math.abs(sy - fy);
10+
11+
if (dx === 0 && dy === 0) {
12+
return t !== 1;
13+
} else {
14+
return Math.max(dx, dy) <= t;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## [2849. Determine if a Cell Is Reachable at a Given Time](https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/)
2+
3+
You are given four integers `sx`, `sy`, `fx`, `fy`, and a **non-negative** integer `t`.
4+
5+
In an infinite 2D grid, you start at the cell `(sx, sy)`. Each second, you **must** move to any of its adjacent cells.
6+
7+
Return `true` _if you can reach cell_ `(fx, fy)` \*after **exactly\*** `t` **\*seconds**, or* `false` *otherwise\*.
8+
9+
A cell's **adjacent cells** are the 8 cells around it that share at least one corner with it. You can visit the same cell several times.
10+
11+
#### Example 1:
12+
13+
![example1](https://assets.leetcode.com/uploads/2023/08/05/example2.svg)
14+
15+
<pre>
16+
<strong>Input:</strong> sx = 3, sy = 1, fx = 7, fy = 3, t = 3
17+
<strong>Output:</strong> false
18+
<strong>Explanation:</strong> 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.
19+
</pre>
20+
21+
#### Constraints:
22+
23+
- <code>1 <= sx, sy, fx, fy <= 10<sup>9</sup></code>
24+
- <code>0 <= t <= 10<sup>9</sup></code>

0 commit comments

Comments
 (0)