Skip to content

Commit 2fc8b48

Browse files
committed
feat: add solutions to lc problem: No.0780
No.0780.Reaching Points
1 parent 6bed2dd commit 2fc8b48

File tree

8 files changed

+235
-2
lines changed

8 files changed

+235
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
- [最长递增子序列](/solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md) - 线性 DP、最长上升子序列模型
7070
- [删列造序 III](/solution/0900-0999/0960.Delete%20Columns%20to%20Make%20Sorted%20III/README.md) - 线性 DP、最长上升子序列模型
7171
- [俄罗斯套娃信封问题](/solution/0300-0399/0354.Russian%20Doll%20Envelopes/README.md) - 线性 DP、最长上升子序列模型、贪心优化
72+
- [堆叠长方体的最大高度](/solution/1600-1699/1691.Maximum%20Height%20by%20Stacking%20Cuboids/README.md) - 排序、线性 DP、最长上升子序列模型
7273

7374
### 4. 高级数据结构
7475

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
6868
- [Longest Increasing Subsequence](/solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README_EN.md) - Linear problem, LIS
6969
- [Delete Columns to Make Sorted III](/solution/0900-0999/0960.Delete%20Columns%20to%20Make%20Sorted%20III/README_EN.md) - Linear problem, LIS
7070
- [Russian Doll Envelopes](/solution/0300-0399/0354.Russian%20Doll%20Envelopes/README_EN.md) - Linear problem, LIS
71+
- [Maximum Height by Stacking Cuboids](/solution/1600-1699/1691.Maximum%20Height%20by%20Stacking%20Cuboids/README_EN.md) - Sort, Linear problem, LIS
7172

7273
### 4. Advanced Data Structures
7374

solution/0700-0799/0780.Reaching Points/README.md

+88-1
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,109 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:逆向计算**
54+
55+
`(tx, ty)` 开始逆向计算,判断是否可以到达状态 `(sx, sy)`。由于逆向计算是将 tx, ty 中的较大值减少,因此当 `tx > ty` 时可以直接将 tx 的值更新为 `tx % ty`,当 `tx < ty` 时可以将 ty 的值更新为 `ty % tx`。逆向计算需要满足 `tx > sx && ty > sy && tx != ty`
56+
57+
当条件不成立时,根据此时 tx 和 ty 判断是否可以从起点转换到终点。
58+
59+
- 如果 `tx == sx && ty == sy`,说明此时已经到达起点状态,返回 true;
60+
- 如果 `tx == sx`,若 `ty > sy && (ty - sy) % tx == 0`,返回 true,否则返回 false;
61+
- 如果 `ty == sy`,若 `tx > sx && (tx - sx) % ty == 0`,返回 true,否则返回 false;
62+
- 如果 `tx ≠ sx && ty ≠ sy`,则不可以从起点转换到终点。
63+
5364
<!-- tabs:start -->
5465

5566
### **Python3**
5667

5768
<!-- 这里可写当前语言的特殊实现逻辑 -->
5869

5970
```python
60-
71+
class Solution:
72+
def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:
73+
while tx > sx and ty > sy and tx != ty:
74+
if tx > ty:
75+
tx %= ty
76+
else:
77+
ty %= tx
78+
if tx == sx and ty == sy:
79+
return True
80+
if tx == sx:
81+
return ty > sy and (ty - sy) % tx == 0
82+
if ty == sy:
83+
return tx > sx and (tx - sx) % ty == 0
84+
return False
6185
```
6286

6387
### **Java**
6488

6589
<!-- 这里可写当前语言的特殊实现逻辑 -->
6690

6791
```java
92+
class Solution {
93+
public boolean reachingPoints(int sx, int sy, int tx, int ty) {
94+
while (tx > sx && ty > sy && tx != ty) {
95+
if (tx > ty) {
96+
tx %= ty;
97+
} else {
98+
ty %= tx;
99+
}
100+
}
101+
if (tx == sx && ty == sy) {
102+
return true;
103+
}
104+
if (tx == sx) {
105+
return ty > sy && (ty - sy) % tx == 0;
106+
}
107+
if (ty == sy) {
108+
return tx > sx && (tx - sx) % ty == 0;
109+
}
110+
return false;
111+
}
112+
}
113+
```
114+
115+
### **C++**
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
bool reachingPoints(int sx, int sy, int tx, int ty) {
121+
while (tx > sx && ty > sy && tx != ty)
122+
{
123+
if (tx > ty) tx %= ty;
124+
else ty %= tx;
125+
}
126+
if (tx == sx && ty == sy) return true;
127+
if (tx == sx) return ty > sy && (ty - sy) % tx == 0;
128+
if (ty == sy) return tx > sx && (tx - sx) % ty == 0;
129+
return false;
130+
}
131+
};
132+
```
68133
134+
### **Go**
135+
136+
```go
137+
func reachingPoints(sx int, sy int, tx int, ty int) bool {
138+
for tx > sx && ty > sy && tx != ty {
139+
if tx > ty {
140+
tx %= ty
141+
} else {
142+
ty %= tx
143+
}
144+
}
145+
if tx == sx && ty == sy {
146+
return true
147+
}
148+
if tx == sx {
149+
return ty > sy && (ty-sy)%tx == 0
150+
}
151+
if ty == sy {
152+
return tx > sx && (tx-sx)%ty == 0
153+
}
154+
return false
155+
}
69156
```
70157

71158
### **...**

solution/0700-0799/0780.Reaching Points/README_EN.md

+77-1
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,89 @@ One series of moves that transforms the starting point to the target is:
4949
### **Python3**
5050

5151
```python
52-
52+
class Solution:
53+
def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:
54+
while tx > sx and ty > sy and tx != ty:
55+
if tx > ty:
56+
tx %= ty
57+
else:
58+
ty %= tx
59+
if tx == sx and ty == sy:
60+
return True
61+
if tx == sx:
62+
return ty > sy and (ty - sy) % tx == 0
63+
if ty == sy:
64+
return tx > sx and (tx - sx) % ty == 0
65+
return False
5366
```
5467

5568
### **Java**
5669

5770
```java
71+
class Solution {
72+
public boolean reachingPoints(int sx, int sy, int tx, int ty) {
73+
while (tx > sx && ty > sy && tx != ty) {
74+
if (tx > ty) {
75+
tx %= ty;
76+
} else {
77+
ty %= tx;
78+
}
79+
}
80+
if (tx == sx && ty == sy) {
81+
return true;
82+
}
83+
if (tx == sx) {
84+
return ty > sy && (ty - sy) % tx == 0;
85+
}
86+
if (ty == sy) {
87+
return tx > sx && (tx - sx) % ty == 0;
88+
}
89+
return false;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
bool reachingPoints(int sx, int sy, int tx, int ty) {
100+
while (tx > sx && ty > sy && tx != ty)
101+
{
102+
if (tx > ty) tx %= ty;
103+
else ty %= tx;
104+
}
105+
if (tx == sx && ty == sy) return true;
106+
if (tx == sx) return ty > sy && (ty - sy) % tx == 0;
107+
if (ty == sy) return tx > sx && (tx - sx) % ty == 0;
108+
return false;
109+
}
110+
};
111+
```
58112
113+
### **Go**
114+
115+
```go
116+
func reachingPoints(sx int, sy int, tx int, ty int) bool {
117+
for tx > sx && ty > sy && tx != ty {
118+
if tx > ty {
119+
tx %= ty
120+
} else {
121+
ty %= tx
122+
}
123+
}
124+
if tx == sx && ty == sy {
125+
return true
126+
}
127+
if tx == sx {
128+
return ty > sy && (ty-sy)%tx == 0
129+
}
130+
if ty == sy {
131+
return tx > sx && (tx-sx)%ty == 0
132+
}
133+
return false
134+
}
59135
```
60136

61137
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
bool reachingPoints(int sx, int sy, int tx, int ty) {
4+
while (tx > sx && ty > sy && tx != ty)
5+
{
6+
if (tx > ty) tx %= ty;
7+
else ty %= tx;
8+
}
9+
if (tx == sx && ty == sy) return true;
10+
if (tx == sx) return ty > sy && (ty - sy) % tx == 0;
11+
if (ty == sy) return tx > sx && (tx - sx) % ty == 0;
12+
return false;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func reachingPoints(sx int, sy int, tx int, ty int) bool {
2+
for tx > sx && ty > sy && tx != ty {
3+
if tx > ty {
4+
tx %= ty
5+
} else {
6+
ty %= tx
7+
}
8+
}
9+
if tx == sx && ty == sy {
10+
return true
11+
}
12+
if tx == sx {
13+
return ty > sy && (ty-sy)%tx == 0
14+
}
15+
if ty == sy {
16+
return tx > sx && (tx-sx)%ty == 0
17+
}
18+
return false
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public boolean reachingPoints(int sx, int sy, int tx, int ty) {
3+
while (tx > sx && ty > sy && tx != ty) {
4+
if (tx > ty) {
5+
tx %= ty;
6+
} else {
7+
ty %= tx;
8+
}
9+
}
10+
if (tx == sx && ty == sy) {
11+
return true;
12+
}
13+
if (tx == sx) {
14+
return ty > sy && (ty - sy) % tx == 0;
15+
}
16+
if (ty == sy) {
17+
return tx > sx && (tx - sx) % ty == 0;
18+
}
19+
return false;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:
3+
while tx > sx and ty > sy and tx != ty:
4+
if tx > ty:
5+
tx %= ty
6+
else:
7+
ty %= tx
8+
if tx == sx and ty == sy:
9+
return True
10+
if tx == sx:
11+
return ty > sy and (ty - sy) % tx == 0
12+
if ty == sy:
13+
return tx > sx and (tx - sx) % ty == 0
14+
return False

0 commit comments

Comments
 (0)