Skip to content

Commit d74f76a

Browse files
committed
✨feat: Add 780 & Modify 778
1 parent b44a77b commit d74f76a

File tree

3 files changed

+91
-8
lines changed

3 files changed

+91
-8
lines changed

Index/数学.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
| [633. 平方数之和](https://leetcode-cn.com/problems/sum-of-square-numbers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-square-numbers/solution/gong-shui-san-xie-yi-ti-san-jie-mei-ju-s-7qi5/) | 简单 | 🤩🤩 |
4141
| [645. 错误的集合](https://leetcode-cn.com/problems/set-mismatch/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/set-mismatch/solution/gong-shui-san-xie-yi-ti-san-jie-ji-shu-s-vnr9/) | 简单 | 🤩🤩🤩 |
4242
| [650. 只有两个键的键盘](https://leetcode-cn.com/problems/2-keys-keyboard/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/2-keys-keyboard/solution/gong-shui-san-xie-yi-ti-san-jie-dong-tai-f035/) | 中等 | 🤩🤩🤩🤩 |
43+
| [780. 到达终点](https://leetcode-cn.com/problems/reaching-points/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reaching-points/solution/by-ac_oier-hw11/) | 困难 | 🤩🤩🤩🤩🤩 |
4344
| [789. 逃脱阻碍者](https://leetcode-cn.com/problems/escape-the-ghosts/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/escape-the-ghosts/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-w69gr/) | 中等 | 🤩🤩🤩🤩🤩 |
4445
| [810. 黑板异或游戏](https://leetcode-cn.com/problems/chalkboard-xor-game/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/chalkboard-xor-game/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-ges7k/) | 困难 | 🤩🤩🤩🤩 |
4546
| [869. 重新排序得到 2 的幂](https://leetcode-cn.com/problems/reordered-power-of-2/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reordered-power-of-2/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-c-3s1e/) | 中等 | 🤩🤩🤩🤩 |

LeetCode/771-780/778. 水位上升的泳池中游泳(困难).md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@ Tag : 「最小生成树」、「并查集」、「Kruskal」、「二分」、
66

77

88

9-
在一个 N x N 的坐标方格 grid 中,每一个方格的值 grid[i][j] 表示在位置 (i,j) 的平台高度。
9+
在一个 `N x N` 的坐标方格 `grid` 中,每一个方格的值 $grid[i][j]$ 表示在位置 $(i,j)$ 的平台高度。
1010

11-
现在开始下雨了。当时间为 t 时,此时雨水导致水池中任意位置的水位为 t 。
11+
现在开始下雨了。当时间为 $t$ 时,此时雨水导致水池中任意位置的水位为 $t$ 。
1212

1313
你可以从一个平台游向四周相邻的任意一个平台,但是前提是此时水位必须同时淹没这两个平台。
1414

1515
假定你可以瞬间移动无限距离,也就是默认在方格内部游动是不耗时的。
1616

1717
当然,在你游泳的时候你必须待在坐标方格里面。
1818

19-
你从坐标方格的左上平台 (0,0) 出发,最少耗时多久你才能到达坐标方格的右下平台 (N-1, N-1)?
20-
21-
 
19+
你从坐标方格的左上平台 $(0, 0)$ 出发,最少耗时多久你才能到达坐标方格的右下平台 $(N-1,N-1)$?
2220

2321
示例 1:
2422
```
@@ -71,7 +69,7 @@ Tag : 「最小生成树」、「并查集」、「Kruskal」、「二分」、
7169
你甚至可以将那题的代码拷贝过来,改一下对于 $w$ 的定义即可。
7270

7371
代码:
74-
```Java []
72+
```Java
7573
class Solution {
7674
int n;
7775
int[] p;
@@ -226,11 +224,11 @@ class Solution {
226224

227225
### 最后
228226

229-
这是我们「刷穿 LeetCode」系列文章的第 `No.788` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先将所有不带锁的题目刷完
227+
这是我们「刷穿 LeetCode」系列文章的第 `No.778` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完
230228

231229
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
232230

233-
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode。
231+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
234232

235233
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
236234

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[780. 到达终点](https://leetcode-cn.com/problems/reaching-points/solution/by-ac_oier-hw11/)** ,难度为 **困难**
4+
5+
Tag : 「数学」
6+
7+
8+
9+
给定四个整数 `sx``sy``tx` 和 `ty`,如果通过一系列的转换可以从起点 $(sx, sy)$ 到达终点 $(tx, ty)$,则返回 `true`,否则返回 `false`
10+
11+
从点 $(x, y)$ 可以转换到 $(x, x+y)$  或者 $(x+y, y)$。
12+
13+
示例 1:
14+
```
15+
输入: sx = 1, sy = 1, tx = 3, ty = 5
16+
17+
输出: true
18+
19+
解释:
20+
可以通过以下一系列转换从起点转换到终点:
21+
(1, 1) -> (1, 2)
22+
(1, 2) -> (3, 2)
23+
(3, 2) -> (3, 5)
24+
```
25+
示例 2:
26+
```
27+
输入: sx = 1, sy = 1, tx = 2, ty = 2
28+
29+
输出: false
30+
```
31+
示例 3:
32+
```
33+
输入: sx = 1, sy = 1, tx = 1, ty = 1
34+
35+
输出: true
36+
```
37+
38+
提示:
39+
* $1 <= sx, sy, tx, ty <= 10^9$
40+
41+
---
42+
43+
### 数学
44+
45+
给定的 $(sx, sy)$ 的数据范围为 $[1, 10^9]$(即均为正整数),且每次转换,只能将另外一维的数值累加到当前维,因此对于每一维的数值而言,随着转换次数的进行,呈(非严格)递增趋势,再结合起始值为正整数,可知在转换过程中均不会出现负数。
46+
47+
**由此得知从 $(tx, ty)$ 到 $(sx, sy)$ 的转换过程唯一确定:总是取较大数减去较小数来进行反推(否则会出现负数)。**
48+
49+
但即使反向转换唯一确定,数据范围为 $10^9$,线性模拟仍会超时。
50+
51+
我们考虑将「相同操作的连续段转换动作」进行合并,在某次反向转换中,如果有 $tx < ty$,我们会将 $(tx, ty)$ 转换为 $(tx, ty - tx)$,若相减完仍有 $tx < ty - tx$,该操作会继续进行,得到 $(tx, ty - 2 * tx)$,直到不满足 $tx < ty - k * tx$,其中 $k$ 为转换次数。
52+
53+
即对于一般性的情况而言,$(tx, ty)$ 中的较大数会一直消减到「与较小数的余数」为止。
54+
55+
因此我们可以先使用 $O(\log{max(tx, ty)})$ 的复杂度将其消减到不超过 $(sx, sy)$ 为止。此时如果消减后的结果 $(tx, ty)$ 任一维度小于 $(sx, sy)$,必然不能进行转换,返回 `False`;如果任一维度相等(假定是 $x$ 维度),则检查另一维度($y$ 维度)的差值,能够由当前维度($x$ 维度)拼凑而来。
56+
57+
代码:
58+
```Java
59+
class Solution {
60+
public boolean reachingPoints(int sx, int sy, int tx, int ty) {
61+
while (sx < tx && sy < ty) {
62+
if (tx < ty) ty %= tx;
63+
else tx %= ty;
64+
}
65+
if (tx < sx || ty < sy) return false;
66+
return sx == tx ? (ty - sy) % tx == 0 : (tx - sx) % ty == 0;
67+
}
68+
}
69+
```
70+
* 时间复杂度:$O(\log{\max(tx, ty)})$
71+
* 空间复杂度:$O(1)$
72+
73+
---
74+
75+
### 最后
76+
77+
这是我们「刷穿 LeetCode」系列文章的第 `No.780` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
78+
79+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
80+
81+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
82+
83+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
84+

0 commit comments

Comments
 (0)