Skip to content

Commit 3a799c4

Browse files
committed
2543 solved.
1 parent e25d0f4 commit 3a799c4

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.24)
2+
project(cpp_2543)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(cpp_2543 main.cpp)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// Source : https://leetcode.com/problems/check-if-point-is-reachable/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2023-01-24
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Math
12+
/// Time Complexity: O(log(max(targetX, targetY)))
13+
/// Space Complexity: O(log(max(targetX, targetY)))
14+
class Solution {
15+
public:
16+
bool isReachable(int targetX, int targetY) {
17+
18+
int g = gcd(targetX, targetY);
19+
return (g & (g - 1)) == 0;
20+
}
21+
22+
private:
23+
int gcd(int a, int b){
24+
if(a > b) swap(a, b);
25+
if (a == 0) return b;
26+
return gcd(b % a, a);
27+
}
28+
};
29+
30+
31+
int main() {
32+
33+
cout << Solution().isReachable(6, 9) << '\n';
34+
return 0;
35+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2424,7 +2424,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
24242424
| 2540 | [Minimum Common Value](https://leetcode.com/problems/minimum-common-value/) | [] | [C++](2001-2500/2540-Minimum-Common-Value/cpp-2540/) | | |
24252425
| 2541 | [Minimum Operations to Make Array Equal II](https://leetcode.com/problems/minimum-operations-to-make-array-equal-ii/) | [] | [C++](2001-2500/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/) | | |
24262426
| 2542 | [Maximum Subsequence Score](https://leetcode.com/problems/maximum-subsequence-score/) | [] | [C++](2001-2500/2542-Maximum-Subsequence-Score/cpp-2542/) | | |
2427-
| | | | | | |
2427+
| 2543 | [Check if Point Is Reachable](https://leetcode.com/problems/check-if-point-is-reachable/description/) | [] | [C++](2001-2500/2543-Check-if-Point-Is-Reachable/cpp-2543/) | | |
24282428
| 2544 | [Alternating Digit Sum](https://leetcode.com/problems/alternating-digit-sum/) | [] | [C++](2001-2500/2544-Alternating-Digit-Sum/cpp-2544/) | | |
24292429
| 2545 | [Sort the Students by Their Kth Score](https://leetcode.com/problems/sort-the-students-by-their-kth-score/) | [] | [C++](2001-2500/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/) | | |
24302430
| 2546 | [Apply Bitwise Operations to Make Strings Equal](https://leetcode.com/problems/apply-bitwise-operations-to-make-strings-equal/) | [] | [C++](2001-2500/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/) | | |

0 commit comments

Comments
 (0)