-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path0780-reaching-points.js
41 lines (38 loc) · 1003 Bytes
/
0780-reaching-points.js
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
/**
* 780. Reaching Points
* https://leetcode.com/problems/reaching-points/
* Difficulty: Hard
*
* Given four integers sx, sy, tx, and ty, return true if it is possible to convert the point
* (sx, sy) to the point (tx, ty) through some operations, or false otherwise.
*
* The allowed operation on some point (x, y) is to convert it to either (x, x + y) or (x + y, y).
*/
/**
* @param {number} sx
* @param {number} sy
* @param {number} tx
* @param {number} ty
* @return {boolean}
*/
var reachingPoints = function(sx, sy, tx, ty) {
while (tx >= sx && ty >= sy) {
if (tx === sx && ty === sy) {
return true;
}
if (tx > ty) {
// If we can directly reach sx by making proper subtractions
if (ty === sy) {
return (tx - sx) % ty === 0;
}
tx %= ty;
} else {
// If we can directly reach sy by making proper subtractions
if (tx === sx) {
return (ty - sy) % tx === 0;
}
ty %= tx;
}
}
return false;
};