diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index f35a6a6f..a967f7f2 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -203,6 +203,7 @@ | [1716. 计算力扣银行的钱](https://leetcode-cn.com/problems/calculate-money-in-leetcode-bank/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/calculate-money-in-leetcode-bank/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-ifit/) | 简单 | 🤩🤩🤩🤩 | | [1720. 解码异或后的数组](https://leetcode-cn.com/problems/decode-xored-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-xored-array/solution/gong-shui-san-xie-li-yong-yi-huo-xing-zh-p1bi/) | 简单 | 🤩🤩🤩 | | [1725. 可以形成最大正方形的矩形数目](https://leetcode-cn.com/problems/number-of-rectangles-that-can-form-the-largest-square/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-rectangles-that-can-form-the-largest-square/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-7756/) | 简单 | 🤩🤩🤩🤩 | +| [1732. 找到最高海拔](https://leetcode.cn/problems/find-the-highest-altitude/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-the-highest-altitude/solution/by-ac_oier-a0j8/) | 简单 | 🤩🤩🤩🤩 | | [1736. 替换隐藏数字得到的最晚时间](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/solution/gong-shui-san-xie-ti-huan-yin-cang-shu-z-2l1h/) | 简单 | 🤩🤩🤩🤩 | | [1737. 满足三条件之一需改变的最少字符数](https://leetcode.cn/problems/change-minimum-characters-to-satisfy-one-of-three-conditions/) | [LeetCode 题解链接](https://leetcode.cn/problems/change-minimum-characters-to-satisfy-one-of-three-conditions/solution/by-ac_oier-vs5u/) | 中等 | 🤩🤩🤩🤩 | | [1743. 从相邻元素对还原数组](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/solution/gong-shui-san-xie-yi-ti-shuang-jie-dan-x-elpx/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/Index/\347\272\277\346\200\247 DP.md" "b/Index/\347\272\277\346\200\247 DP.md" index 98be4bb9..d8854dec 100644 --- "a/Index/\347\272\277\346\200\247 DP.md" +++ "b/Index/\347\272\277\346\200\247 DP.md" @@ -17,6 +17,7 @@ | [678. 有效的括号字符串](https://leetcode-cn.com/problems/valid-parenthesis-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/valid-parenthesis-string/solution/gong-shui-san-xie-yi-ti-shuang-jie-dong-801rq/) | 中等 | 🤩🤩🤩🤩🤩 | | [688. 骑士在棋盘上的概率](https://leetcode-cn.com/problems/knight-probability-in-chessboard/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/knight-probability-in-chessboard/solution/gong-shui-san-xie-jian-dan-qu-jian-dp-yu-st8l/) | 中等 | 🤩🤩🤩🤩🤩 | | [741. 摘樱桃](https://leetcode.cn/problems/cherry-pickup/) | [LeetCode 题解链接](https://leetcode.cn/problems/cherry-pickup/solution/by-ac_oier-pz7i/) | 困难 | 🤩🤩🤩🤩 | +| [799. 香槟塔](https://leetcode.cn/problems/champagne-tower/) | [LeetCode 题解链接](https://leetcode.cn/problems/champagne-tower/solution/by-ac_oier-c8jn/) | 中等 | 🤩🤩🤩 | | [828. 统计子串中的唯一字符](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/solution/by-ac_oier-922k/) | 困难 | 🤩🤩🤩🤩 | | [978. 最长湍流子数组](https://leetcode-cn.com/problems/longest-turbulent-subarray/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-turbulent-subarray/solution/xiang-jie-dong-tai-gui-hua-ru-he-cai-dp-3spgj/) | 中等 | 🤩🤩🤩🤩 | | [1137. 第 N 个泰波那契数](https://leetcode-cn.com/problems/n-th-tribonacci-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/n-th-tribonacci-number/solution/gong-shui-san-xie-yi-ti-si-jie-die-dai-d-m1ie/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/1731-1740/1732. \346\211\276\345\210\260\346\234\200\351\253\230\346\265\267\346\213\224\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/1731-1740/1732. \346\211\276\345\210\260\346\234\200\351\253\230\346\265\267\346\213\224\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..42316a2f --- /dev/null +++ "b/LeetCode/1731-1740/1732. \346\211\276\345\210\260\346\234\200\351\253\230\346\265\267\346\213\224\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,89 @@ +### 题目描述 + +这是 LeetCode 上的 **[1732. 找到最高海拔](https://leetcode.cn/problems/find-the-highest-altitude/solution/by-ac_oier-a0j8/)** ,难度为 **简单**。 + +Tag : 「模拟」 + + + +有一个自行车手打算进行一场公路骑行,这条路线总共由 $n + 1$ 个不同海拔的点组成。自行车手从海拔为 `0` 的点 `0` 开始骑行。 + +给你一个长度为 `n` 的整数数组 `gain`,其中 `gain[i]` 是点 `i` 和点 `i + 1` 的 净海拔高度差($0 <= i < n$)。请你返回 最高点的海拔 。 + +示例 1: +``` +输入:gain = [-5,1,5,0,-7] + +输出:1 + +解释:海拔高度依次为 [0,-5,-4,1,1,-6] 。最高海拔为 1 。 +``` +示例 2: +``` +输入:gain = [-4,-3,-2,-1,4,3,2] + +输出:0 + +解释:海拔高度依次为 [0,-4,-7,-9,-10,-6,-3,-1] 。最高海拔为 0 。 +``` + +提示: +* $n = gain.length$ +* $1 <= n <= 100$ +* $-100 <= gain[i] <= 100$ + +--- + +### 模拟 + +根据题意进行模拟即可。 + +Java 代码: +```Java +class Solution { + public int largestAltitude(int[] g) { + int cur = 0, ans = 0; + for (int x : g) { + cur += x; + ans = Math.max(ans, cur); + } + return ans; + } +} +``` +TypeScript 代码: +```TypeScript +function largestAltitude(g: number[]): number { + let cur = 0, ans = 0 + for (let x of g) { + cur += x + ans = Math.max(ans, cur) + } + return ans +} +``` +Python3 代码: +```Python +class Solution: + def largestAltitude(self, g: List[int]) -> int: + cur, ans = 0, 0 + for x in g: + cur += x + ans = max(ans, cur) + return ans +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(1)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.1732` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 + diff --git "a/LeetCode/791-800/799. \351\246\231\346\247\237\345\241\224\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/791-800/799. \351\246\231\346\247\237\345\241\224\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..77a0a906 --- /dev/null +++ "b/LeetCode/791-800/799. \351\246\231\346\247\237\345\241\224\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,119 @@ +### 题目描述 + +这是 LeetCode 上的 **[799. 香槟塔](https://leetcode.cn/problems/champagne-tower/solution/by-ac_oier-c8jn/)** ,难度为 **中等**。 + +Tag : 「动态规划」、「线性 DP」 + + + +我们把玻璃杯摆成金字塔的形状,其中 第一层 有 `1` 个玻璃杯, 第二层 有 `2` 个,依次类推到第 `100` 层,每个玻璃杯 (`250ml`) 将盛有香槟。 + +从顶层的第一个玻璃杯开始倾倒一些香槟,当顶层的杯子满了,任何溢出的香槟都会立刻等流量的流向左右两侧的玻璃杯。当左右两边的杯子也满了,就会等流量的流向它们左右两边的杯子,依次类推。(当最底层的玻璃杯满了,香槟会流到地板上) + +例如,在倾倒一杯香槟后,最顶层的玻璃杯满了。倾倒了两杯香槟后,第二层的两个玻璃杯各自盛放一半的香槟。在倒三杯香槟后,第二层的香槟满了 - 此时总共有三个满的玻璃杯。在倒第四杯后,第三层中间的玻璃杯盛放了一半的香槟,他两边的玻璃杯各自盛放了四分之一的香槟,如下图所示。 + +![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/09/tower.png) + +现在当倾倒了非负整数杯香槟后,返回第 i 行 j 个玻璃杯所盛放的香槟占玻璃杯容积的比例( i 和 j 都从0开始)。 + +示例 1: +``` +输入: poured(倾倒香槟总杯数) = 1, query_glass(杯子的位置数) = 1, query_row(行数) = 1 + +输出: 0.00000 + +解释: 我们在顶层(下标是(0,0))倒了一杯香槟后,没有溢出,因此所有在顶层以下的玻璃杯都是空的。 +``` +示例 2: +``` +输入: poured(倾倒香槟总杯数) = 2, query_glass(杯子的位置数) = 1, query_row(行数) = 1 + +输出: 0.50000 + +解释: 我们在顶层(下标是(0,0)倒了两杯香槟后,有一杯量的香槟将从顶层溢出,位于(1,0)的玻璃杯和(1,1)的玻璃杯平分了这一杯香槟,所以每个玻璃杯有一半的香槟。 +``` +示例 3: +``` +输入: poured = 100000009, query_row = 33, query_glass = 17 + +输出: 1.00000 +``` + +提示: +* $0 <= poured <= 10^9$ +* $0 <= query_glass <= query_row < 100$ + +--- + +### 线性 DP + +为了方便,我们令 `poured` 为 `k`,`query_row` 和 `query_glass` 分别为 $n$ 和 $m$。 + +定义 $f[i][j]$ 为第 $i$ 行第 $j$ 列杯子所经过的水的流量(而不是最终剩余的水量)。 + +起始我们有 $f[0][0] = k$,最终答案为 $\min(f[n][m], 1)$。 + +不失一般性考虑 $f[i][j]$ 能够更新哪些状态:显然当 $f[i][j]$ 不足 $1$ 的时候,不会有水从杯子里溢出,即 $f[i][j]$ 将不能更新其他状态;当 $f[i][j]$ 大于 $1$ 时,将会有 $f[i][j] - 1$ 的水会等量留到下一行的杯子里,所流向的杯子分别是「第 $i + 1$ 行第 $j$ 列的杯子」和「第 $i + 1$ 行第 $j + 1$ 列的杯子」,增加流量均为 $\frac{f[i][j] - 1}{2}$,即有 $f[i + 1][j] += \frac{f[i][j] - 1}{2}$ 和 $f[i + 1][j + 1] += \frac{f[i][j] - 1}{2}$。 + +Java 代码: +```Java +class Solution { + public double champagneTower(int k, int n, int m) { + double[][] f = new double[n + 10][n + 10]; + f[0][0] = k; + for (int i = 0; i <= n; i++) { + for (int j = 0; j <= i; j++) { + if (f[i][j] <= 1) continue; + f[i + 1][j] += (f[i][j] - 1) / 2; + f[i + 1][j + 1] += (f[i][j] - 1) / 2; + } + } + return Math.min(f[n][m], 1); + } +} +``` +TypeScript 代码: +```TypeScript +function champagneTower(k: number, n: number, m: number): number { + const f = new Array>() + for (let i = 0; i < n + 10; i++) f.push(new Array(n + 10).fill(0)) + f[0][0] = k + for (let i = 0; i <= n; i++) { + for (let j = 0; j <= i; j++) { + if (f[i][j] <= 1) continue + f[i + 1][j] += (f[i][j] - 1) / 2 + f[i + 1][j + 1] += (f[i][j] - 1) / 2 + } + } + return Math.min(f[n][m], 1) +} +``` +Python3 代码: +```Python +class Solution: + def champagneTower(self, k: int, n: int, m: int) -> float: + f = [[0] * (n + 10) for _ in range(n + 10)] + f[0][0] = k + for i in range(n + 1): + for j in range(i + 1): + if f[i][j] <= 1: + continue + f[i + 1][j] += (f[i][j] - 1) / 2 + f[i + 1][j + 1] += (f[i][j] - 1) / 2 + return min(f[n][m], 1) +``` +* 时间复杂度:$O(n^2)$ +* 空间复杂度:$O(n^2)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.799` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +