Skip to content

Commit d3adbcc

Browse files
committed
Add solution #780
1 parent c95626b commit d3adbcc

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@
590590
777|[Swap Adjacent in LR String](./0777-swap-adjacent-in-lr-string.js)|Medium|
591591
778|[Swim in Rising Water](./0778-swim-in-rising-water.js)|Hard|
592592
779|[K-th Symbol in Grammar](./0779-k-th-symbol-in-grammar.js)|Medium|
593+
780|[Reaching Points](./0780-reaching-points.js)|Hard|
593594
783|[Minimum Distance Between BST Nodes](./0783-minimum-distance-between-bst-nodes.js)|Easy|
594595
784|[Letter Case Permutation](./0784-letter-case-permutation.js)|Medium|
595596
790|[Domino and Tromino Tiling](./0790-domino-and-tromino-tiling.js)|Medium|

solutions/0780-reaching-points.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 780. Reaching Points
3+
* https://leetcode.com/problems/reaching-points/
4+
* Difficulty: Hard
5+
*
6+
* Given four integers sx, sy, tx, and ty, return true if it is possible to convert the point
7+
* (sx, sy) to the point (tx, ty) through some operations, or false otherwise.
8+
*
9+
* The allowed operation on some point (x, y) is to convert it to either (x, x + y) or (x + y, y).
10+
*/
11+
12+
/**
13+
* @param {number} sx
14+
* @param {number} sy
15+
* @param {number} tx
16+
* @param {number} ty
17+
* @return {boolean}
18+
*/
19+
var reachingPoints = function(sx, sy, tx, ty) {
20+
while (tx >= sx && ty >= sy) {
21+
if (tx === sx && ty === sy) {
22+
return true;
23+
}
24+
25+
if (tx > ty) {
26+
// If we can directly reach sx by making proper subtractions
27+
if (ty === sy) {
28+
return (tx - sx) % ty === 0;
29+
}
30+
tx %= ty;
31+
} else {
32+
// If we can directly reach sy by making proper subtractions
33+
if (tx === sx) {
34+
return (ty - sy) % tx === 0;
35+
}
36+
ty %= tx;
37+
}
38+
}
39+
40+
return false;
41+
};

0 commit comments

Comments
 (0)