forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
57 lines (44 loc) · 1.18 KB
/
main.cpp
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
53
54
55
56
57
/// Source : https://leetcode.com/problems/reaching-points/description/
/// Author : liuyubobobo
/// Time : 2018-02-11
#include <iostream>
#include <cassert>
using namespace std;
/// Work Backwards
///
/// Time Complexity: O(log(max(tx, ty)))
/// Space Complexity: O(1)
class Solution {
public:
bool reachingPoints(int sx, int sy, int tx, int ty) {
if(tx == sx && ty == sy)
return true;
if(tx < sx || ty < sy)
return false;
assert(tx >= sx && ty >= sy);
while(true){
if(tx > ty){
if(ty > sy)
tx %= ty;
else //
return ty == sy && (tx - sx) % ty == 0;
}
else{ // tx <= ty
if(tx > sx)
ty %= tx;
else
return tx == sx && (ty - sy) % tx == 0;
}
}
assert(false);
}
};
void printBool(bool res){
cout << (res ? "True" : "False") << endl;
}
int main() {
printBool(Solution().reachingPoints(1, 1, 3, 5));
printBool(Solution().reachingPoints(1, 1, 2, 2));
printBool(Solution().reachingPoints(1, 1, 1, 1));
return 0;
}