Skip to content

Commit 6e17d93

Browse files
authored
Added task 2543
1 parent 3286926 commit 6e17d93

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.21'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2543 |[Check if Point Is Reachable](src/main/java/g2501_2600/s2543_check_if_point_is_reachable/Solution.java)| Hard | Math, Number_Theory | 0 | 100.00
18511852
| 2542 |[Maximum Subsequence Score](src/main/java/g2501_2600/s2542_maximum_subsequence_score/Solution.java)| Medium | Array, Sorting, Greedy, Heap_(Priority_Queue) | 94 | 84.75
18521853
| 2541 |[Minimum Operations to Make Array Equal II](src/main/java/g2501_2600/s2541_minimum_operations_to_make_array_equal_ii/Solution.java)| Medium | Array, Math, Greedy | 3 | 100.00
18531854
| 2540 |[Minimum Common Value](src/main/java/g2501_2600/s2540_minimum_common_value/Solution.java)| Easy | Array, Hash_Table, Binary_Search, Two_Pointers | 0 | 100.00
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g2501_2600.s2543_check_if_point_is_reachable;
2+
3+
// #Hard #Math #Number_Theory #2023_05_11_Time_0_ms_(100.00%)_Space_39.3_MB_(70.37%)
4+
5+
public class Solution {
6+
public boolean isReachable(int x, int y) {
7+
int g = gcd(x, y);
8+
return (g & (g - 1)) == 0;
9+
}
10+
11+
private int gcd(int x, int y) {
12+
while (x != 0) {
13+
int tmp = x;
14+
x = y % x;
15+
y = tmp;
16+
}
17+
return y;
18+
}
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2543\. Check if Point Is Reachable
2+
3+
Hard
4+
5+
There exists an infinitely large grid. You are currently at point `(1, 1)`, and you need to reach the point `(targetX, targetY)` using a finite number of steps.
6+
7+
In one **step**, you can move from point `(x, y)` to any one of the following points:
8+
9+
* `(x, y - x)`
10+
* `(x - y, y)`
11+
* `(2 * x, y)`
12+
* `(x, 2 * y)`
13+
14+
Given two integers `targetX` and `targetY` representing the X-coordinate and Y-coordinate of your final position, return `true` _if you can reach the point from_ `(1, 1)` _using some number of steps, and_ `false` _otherwise_.
15+
16+
**Example 1:**
17+
18+
**Input:** targetX = 6, targetY = 9
19+
20+
**Output:** false
21+
22+
**Explanation:** It is impossible to reach (6,9) from (1,1) using any sequence of moves, so false is returned.
23+
24+
**Example 2:**
25+
26+
**Input:** targetX = 4, targetY = 7
27+
28+
**Output:** true
29+
30+
**Explanation:** You can follow the path (1,1) -> (1,2) -> (1,4) -> (1,8) -> (1,7) -> (2,7) -> (4,7).
31+
32+
**Constraints:**
33+
34+
* <code>1 <= targetX, targetY <= 10<sup>9</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2501_2600.s2543_check_if_point_is_reachable;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void isReachable() {
11+
assertThat(new Solution().isReachable(6, 9), equalTo(false));
12+
}
13+
14+
@Test
15+
void isReachable2() {
16+
assertThat(new Solution().isReachable(4, 7), equalTo(true));
17+
}
18+
}

0 commit comments

Comments
 (0)