-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path780-ReachingPoints.go
More file actions
82 lines (72 loc) · 2.42 KB
/
780-ReachingPoints.go
File metadata and controls
82 lines (72 loc) · 2.42 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
// 780. Reaching Points
// 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).
// Example 1:
// Input: sx = 1, sy = 1, tx = 3, ty = 5
// Output: true
// Explanation:
// One series of moves that transforms the starting point to the target is:
// (1, 1) -> (1, 2)
// (1, 2) -> (3, 2)
// (3, 2) -> (3, 5)
// Example 2:
// Input: sx = 1, sy = 1, tx = 2, ty = 2
// Output: false
// Example 3:
// Input: sx = 1, sy = 1, tx = 1, ty = 1
// Output: true
// Constraints:
// 1 <= sx, sy, tx, ty <= 10^9
import "fmt"
// if tx > ty then tx = x + y, ty = y , else if ty > tx then tx = x , ty = x + y.
// trace backwards until we find a solution.
func reachingPoints1(sx int, sy int, tx int, ty int) bool {
if sx == tx && sy == ty { return true }
if sx == tx {
return ty - sy > 0 && (ty - sy) % sx == 0 // ty has to be bigger than sy
} else if sy == ty {
return tx - sx > 0 && (tx - sx) % sy == 0 // tx has to be bigger than sx
}
for tx >= sx && ty >= sy { // search backward
if sx == tx && sy == ty { return true }
if tx > ty {
tx = tx - ty
} else {
ty = ty - tx
}
}
return false
}
func reachingPoints(sx int, sy int, tx int, ty int) bool {
if tx < sx || ty < sy{ return false }
if sx == tx{
return (ty - sy) % sx == 0
} else if sy == ty {
return (tx - sx) % sy == 0
}
if tx > ty { return reachingPoints(sx, sy, tx % ty, ty) }
return reachingPoints(sx, sy, tx, ty % tx)
}
func main() {
// Example 1:
// Input: sx = 1, sy = 1, tx = 3, ty = 5
// Output: true
// Explanation:
// One series of moves that transforms the starting point to the target is:
// (1, 1) -> (1, 2)
// (1, 2) -> (3, 2)
// (3, 2) -> (3, 5)
fmt.Println(reachingPoints(1,1,3,5)) // true
// Example 2:
// Input: sx = 1, sy = 1, tx = 2, ty = 2
// Output: false
fmt.Println(reachingPoints(1,1,2,2)) // false
// Example 3:
// Input: sx = 1, sy = 1, tx = 1, ty = 1
// Output: true
fmt.Println(reachingPoints(1,1,1,1)) // true
fmt.Println(reachingPoints1(1,1,3,5)) // true
fmt.Println(reachingPoints1(1,1,2,2)) // false
fmt.Println(reachingPoints1(1,1,1,1)) // true
}