Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Index/数学.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
| [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/) | 简单 | 🤩🤩 |
| [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/) | 简单 | 🤩🤩🤩 |
| [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/) | 中等 | 🤩🤩🤩🤩 |
| [780. 到达终点](https://leetcode-cn.com/problems/reaching-points/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reaching-points/solution/by-ac_oier-hw11/) | 困难 | 🤩🤩🤩🤩🤩 |
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
| [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/) | 困难 | 🤩🤩🤩🤩 |
| [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/) | 中等 | 🤩🤩🤩🤩 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ Tag : 「最小生成树」、「并查集」、「Kruskal」、「二分」、



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

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

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

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

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

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


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

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

代码:
```Java []
```Java
class Solution {
int n;
int[] p;
Expand Down Expand Up @@ -226,11 +224,11 @@ class Solution {

### 最后

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

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

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

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

84 changes: 84 additions & 0 deletions LeetCode/771-780/780. 到达终点(困难).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
### 题目描述

这是 LeetCode 上的 **[780. 到达终点](https://leetcode-cn.com/problems/reaching-points/solution/by-ac_oier-hw11/)** ,难度为 **困难**。

Tag : 「数学」



给定四个整数 `sx`,`sy`,`tx` 和 `ty`,如果通过一系列的转换可以从起点 $(sx, sy)$ 到达终点 $(tx, ty)$,则返回 `true`,否则返回 `false`。

从点 $(x, y)$ 可以转换到 $(x, x+y)$  或者 $(x+y, y)$。

示例 1:
```
输入: sx = 1, sy = 1, tx = 3, ty = 5

输出: true

解释:
可以通过以下一系列转换从起点转换到终点:
(1, 1) -> (1, 2)
(1, 2) -> (3, 2)
(3, 2) -> (3, 5)
```
示例 2:
```
输入: sx = 1, sy = 1, tx = 2, ty = 2

输出: false
```
示例 3:
```
输入: sx = 1, sy = 1, tx = 1, ty = 1

输出: true
```

提示:
* $1 <= sx, sy, tx, ty <= 10^9$

---

### 数学

给定的 $(sx, sy)$ 的数据范围为 $[1, 10^9]$(即均为正整数),且每次转换,只能将另外一维的数值累加到当前维,因此对于每一维的数值而言,随着转换次数的进行,呈(非严格)递增趋势,再结合起始值为正整数,可知在转换过程中均不会出现负数。

**由此得知从 $(tx, ty)$ 到 $(sx, sy)$ 的转换过程唯一确定:总是取较大数减去较小数来进行反推(否则会出现负数)。**

但即使反向转换唯一确定,数据范围为 $10^9$,线性模拟仍会超时。

我们考虑将「相同操作的连续段转换动作」进行合并,在某次反向转换中,如果有 $tx < ty$,我们会将 $(tx, ty)$ 转换为 $(tx, ty - tx)$,若相减完仍有 $tx < ty - tx$,该操作会继续进行,得到 $(tx, ty - 2 * tx)$,直到不满足 $tx < ty - k * tx$,其中 $k$ 为转换次数。

即对于一般性的情况而言,$(tx, ty)$ 中的较大数会一直消减到「与较小数的余数」为止。

因此我们可以先使用 $O(\log{max(tx, ty)})$ 的复杂度将其消减到不超过 $(sx, sy)$ 为止。此时如果消减后的结果 $(tx, ty)$ 任一维度小于 $(sx, sy)$,必然不能进行转换,返回 `False`;如果任一维度相等(假定是 $x$ 维度),则检查另一维度($y$ 维度)的差值,能够由当前维度($x$ 维度)拼凑而来。

代码:
```Java
class Solution {
public boolean reachingPoints(int sx, int sy, int tx, int ty) {
while (sx < tx && sy < ty) {
if (tx < ty) ty %= tx;
else tx %= ty;
}
if (tx < sx || ty < sy) return false;
return sx == tx ? (ty - sy) % tx == 0 : (tx - sx) % ty == 0;
}
}
```
* 时间复杂度:$O(\log{\max(tx, ty)})$
* 空间复杂度:$O(1)$

---

### 最后

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

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

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

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