Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ea6473

Browse files
committed
更新题解列表
1 parent 5a34693 commit 8ea6473

File tree

5 files changed

+247
-2
lines changed

5 files changed

+247
-2
lines changed

‎Contents/00.Introduction/04.Solutions-List.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LeetCode 题解(已完成 847 道)
1+
# LeetCode 题解(已完成 850 道)
22

33
| 题号 | 标题 | 题解 | 标签 | 难度 |
44
| :------ | :------ | :------ | :------ | :------ |
@@ -600,13 +600,15 @@
600600
| 1617 | [统计子树中城市之间最大距离](https://leetcode.cn/problems/count-subtrees-with-max-distance-between-cities/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1617.%20%E7%BB%9F%E8%AE%A1%E5%AD%90%E6%A0%91%E4%B8%AD%E5%9F%8E%E5%B8%82%E4%B9%8B%E9%97%B4%E6%9C%80%E5%A4%A7%E8%B7%9D%E7%A6%BB.md) | 位运算、树、动态规划、状态压缩、枚举 | 困难 |
601601
| 1631 | [最小体力消耗路径](https://leetcode.cn/problems/path-with-minimum-effort/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1631.%20%E6%9C%80%E5%B0%8F%E4%BD%93%E5%8A%9B%E6%B6%88%E8%80%97%E8%B7%AF%E5%BE%84.md) | 深度优先搜索、广度优先搜索、并查集、数组、二分查找、矩阵、堆(优先队列) | 中等 |
602602
| 1646 | [获取生成数组中的最大值](https://leetcode.cn/problems/get-maximum-in-generated-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1646.%20%E8%8E%B7%E5%8F%96%E7%94%9F%E6%88%90%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC.md) | 数组、动态规划、模拟 | 简单 |
603+
| 1657 | [确定两个字符串是否接近](https://leetcode.cn/problems/determine-if-two-strings-are-close/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1657.%20%E7%A1%AE%E5%AE%9A%E4%B8%A4%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E6%8E%A5%E8%BF%91.md) | 哈希表、字符串、排序 | 中等 |
603604
| 1658 | [将 x 减到 0 的最小操作数](https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1658.%20%E5%B0%86%20x%20%E5%87%8F%E5%88%B0%200%20%E7%9A%84%E6%9C%80%E5%B0%8F%E6%93%8D%E4%BD%9C%E6%95%B0.md) | 数组、哈希表、二分查找、前缀和、滑动窗口 | 中等 |
604605
| 1672 | [最富有客户的资产总量](https://leetcode.cn/problems/richest-customer-wealth/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1672.%20%E6%9C%80%E5%AF%8C%E6%9C%89%E5%AE%A2%E6%88%B7%E7%9A%84%E8%B5%84%E4%BA%A7%E6%80%BB%E9%87%8F.md) | 数组、矩阵 | 简单 |
605606
| 1695 | [删除子数组的最大得分](https://leetcode.cn/problems/maximum-erasure-value/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1695.%20%E5%88%A0%E9%99%A4%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E6%9C%80%E5%A4%A7%E5%BE%97%E5%88%86.md) | 数组、哈希表、滑动窗口 | 中等 |
606607
| 1698 | [字符串的不同子字符串个数](https://leetcode.cn/problems/number-of-distinct-substrings-in-a-string/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1698.%20%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E4%B8%8D%E5%90%8C%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AA%E6%95%B0.md) | 字典树、字符串、后缀数组、哈希函数、滚动哈希 | 中等 |
607608
| 1710 | [卡车上的最大单元数](https://leetcode.cn/problems/maximum-units-on-a-truck/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1710.%20%E5%8D%A1%E8%BD%A6%E4%B8%8A%E7%9A%84%E6%9C%80%E5%A4%A7%E5%8D%95%E5%85%83%E6%95%B0.md) | 贪心、数组、排序 | 简单 |
608609
| 1716 | [计算力扣银行的钱](https://leetcode.cn/problems/calculate-money-in-leetcode-bank/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1716.%20%E8%AE%A1%E7%AE%97%E5%8A%9B%E6%89%A3%E9%93%B6%E8%A1%8C%E7%9A%84%E9%92%B1.md) | 数学 | 简单 |
609610
| 1720 | [解码异或后的数组](https://leetcode.cn/problems/decode-xored-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1720.%20%E8%A7%A3%E7%A0%81%E5%BC%82%E6%88%96%E5%90%8E%E7%9A%84%E6%95%B0%E7%BB%84.md) | 位运算、数组 | 简单 |
611+
| 1726 | [同积元组](https://leetcode.cn/problems/tuple-with-same-product/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1726.%20%E5%90%8C%E7%A7%AF%E5%85%83%E7%BB%84.md) | 数组、哈希表 | 中等 |
610612
| 1736 | [替换隐藏数字得到的最晚时间](https://leetcode.cn/problems/latest-time-by-replacing-hidden-digits/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1736.%20%E6%9B%BF%E6%8D%A2%E9%9A%90%E8%97%8F%E6%95%B0%E5%AD%97%E5%BE%97%E5%88%B0%E7%9A%84%E6%9C%80%E6%99%9A%E6%97%B6%E9%97%B4.md) | 贪心、字符串 | 简单 |
611613
| 1742 | [盒子中小球的最大数量](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1742.%20%E7%9B%92%E5%AD%90%E4%B8%AD%E5%B0%8F%E7%90%83%E7%9A%84%E6%9C%80%E5%A4%A7%E6%95%B0%E9%87%8F.md) | 哈希表、数学、计数 | 简单 |
612614
| 1749 | [任意子数组和的绝对值的最大值](https://leetcode.cn/problems/maximum-absolute-sum-of-any-subarray/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1749.%20%E4%BB%BB%E6%84%8F%E5%AD%90%E6%95%B0%E7%BB%84%E5%92%8C%E7%9A%84%E7%BB%9D%E5%AF%B9%E5%80%BC%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC.md) | 数组、动态规划 | 中等 |
@@ -625,6 +627,7 @@
625627
| 1893 | [检查是否区域内所有整数都被覆盖](https://leetcode.cn/problems/check-if-all-the-integers-in-a-range-are-covered/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1893.%20%E6%A3%80%E6%9F%A5%E6%98%AF%E5%90%A6%E5%8C%BA%E5%9F%9F%E5%86%85%E6%89%80%E6%9C%89%E6%95%B4%E6%95%B0%E9%83%BD%E8%A2%AB%E8%A6%86%E7%9B%96.md) | 数组、哈希表、前缀和 | 简单 |
626628
| 1897 | [重新分配字符使所有字符串都相等](https://leetcode.cn/problems/redistribute-characters-to-make-all-strings-equal/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1897.%20%E9%87%8D%E6%96%B0%E5%88%86%E9%85%8D%E5%AD%97%E7%AC%A6%E4%BD%BF%E6%89%80%E6%9C%89%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%83%BD%E7%9B%B8%E7%AD%89.md) | 哈希表、字符串、计数 | 简单 |
627629
| 1903 | [字符串中的最大奇数](https://leetcode.cn/problems/largest-odd-number-in-string/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1903.%20%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%9C%80%E5%A4%A7%E5%A5%87%E6%95%B0.md) | 贪心、数学、字符串 | 简单 |
630+
| 1921 | [消灭怪物的最大数量](https://leetcode.cn/problems/eliminate-maximum-number-of-monsters/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1921.%20%E6%B6%88%E7%81%AD%E6%80%AA%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E6%95%B0%E9%87%8F.md) | 贪心、数组、排序 | 中等 |
628631
| 1925 | [统计平方和三元组的数目](https://leetcode.cn/problems/count-square-sum-triples/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1925.%20%E7%BB%9F%E8%AE%A1%E5%B9%B3%E6%96%B9%E5%92%8C%E4%B8%89%E5%85%83%E7%BB%84%E7%9A%84%E6%95%B0%E7%9B%AE.md) | 数学、枚举 | 简单 |
629632
| 1929 | [数组串联](https://leetcode.cn/problems/concatenation-of-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1929.%20%E6%95%B0%E7%BB%84%E4%B8%B2%E8%81%94.md) | 数组 | 简单 |
630633
| 1930 | [长度为 3 的不同回文子序列](https://leetcode.cn/problems/unique-length-3-palindromic-subsequences/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1930.%20%E9%95%BF%E5%BA%A6%E4%B8%BA%203%20%E7%9A%84%E4%B8%8D%E5%90%8C%E5%9B%9E%E6%96%87%E5%AD%90%E5%BA%8F%E5%88%97.md) | 哈希表、字符串、前缀和 | 中等 |

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,4 @@
255255
- [动态规划优化题目](./Contents/10.Dynamic-Programming/11.DP-Optimization/04.DP-Optimization-List.md)
256256

257257
## 11. 附加内容
258-
## [12. LeetCode 题解(已完成 847 道)](./Contents/00.Introduction/04.Solutions-List.md)
258+
## [12. LeetCode 题解(已完成 850 道)](./Contents/00.Introduction/04.Solutions-List.md)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# [1657. 确定两个字符串是否接近](https://leetcode.cn/problems/determine-if-two-strings-are-close/)
2+
3+
- 标签:哈希表、字符串、排序
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [1657. 确定两个字符串是否接近 - 力扣](https://leetcode.cn/problems/determine-if-two-strings-are-close/)
9+
10+
## 题目大意
11+
12+
**描述**:如果可以使用以下操作从一个字符串得到另一个字符串,则认为两个字符串 接近 :
13+
14+
- 操作 1:交换任意两个现有字符。
15+
- 例如,`abcde` -> `aecdb`
16+
- 操作 2:将一个 现有 字符的每次出现转换为另一个现有字符,并对另一个字符执行相同的操作。
17+
- 例如,`aacabb` -> `bbcbaa`(所有 `a` 转化为 `b`,而所有的 `b` 转换为 `a` )。
18+
19+
给定两个字符串,$word1$ 和 $word2$。
20+
21+
**要求**:如果 $word1$ 和 $word2$ 接近 ,就返回 $True$;否则,返回 $False$。
22+
23+
**说明**:
24+
25+
- 1ドル \le word1.length, word2.length \le 10^5$。
26+
- $word1$ 和 $word2$ 仅包含小写英文字母。
27+
28+
**示例**:
29+
30+
- 示例 1:
31+
32+
```python
33+
输入:word1 = "abc", word2 = "bca"
34+
输出:True
35+
解释:2 次操作从 word1 获得 word2 。
36+
执行操作 1:"abc" -> "acb"
37+
执行操作 1:"acb" -> "bca"
38+
```
39+
40+
- 示例 2:
41+
42+
```python
43+
输入:word1 = "a", word2 = "aa"
44+
输出:False
45+
解释:不管执行多少次操作,都无法从 word1 得到 word2 ,反之亦然。
46+
```
47+
48+
## 解题思路
49+
50+
### 思路 1:模拟
51+
52+
无论是操作 1,还是操作 2,只是对字符位置进行交换,而不会产生或者删除字符。
53+
54+
则我们只需要检查两个字符串的字符种类以及每种字符的个数是否相同即可。
55+
56+
具体步骤如下:
57+
58+
1. 分别使用哈希表 $cnts1$、$cnts2$ 统计每个字符串中的字符种类,每种字符的个数。
59+
2. 判断两者的字符种类是否相等,并且判断每种字符的个数是否相同。
60+
3. 如果字符种类相同,且每种字符的个数完全相同,则返回 $True,ドル否则,返回 $False$。
61+
62+
### 思路 1:代码
63+
64+
```Python
65+
class Solution:
66+
def closeStrings(self, word1: str, word2: str) -> bool:
67+
cnts1 = Counter(word1)
68+
cnts2 = Counter(word2)
69+
70+
return cnts1.keys() == cnts2.keys() and sorted(cnts1.values()) == sorted(cnts2.values())
71+
```
72+
73+
### 思路 1:复杂度分析
74+
75+
- **时间复杂度**:$O(max(n1, n2) + |\sum| \times \log | \sum |),ドル其中 $n1$、$n2$ 分别为字符串 $word1$、$word2$ 的长度,$\sum$ 为字符集,本题中 $| \sum | = 26$。
76+
- **空间复杂度**:$O(| \sum |)$。
77+

‎Solutions/1726. 同积元组.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# [1726. 同积元组](https://leetcode.cn/problems/tuple-with-same-product/)
2+
3+
- 标签:数组、哈希表
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [1726. 同积元组 - 力扣](https://leetcode.cn/problems/tuple-with-same-product/)
9+
10+
## 题目大意
11+
12+
**描述**:给定一个由不同正整数组成的数组 $nums$。
13+
14+
**要求**:返回满足 $a \times b = c \times d$ 的元组 $(a, b, c, d)$ 的数量。其中 $a$、$b$、$c$ 和 $d$ 都是 $nums$ 中的元素,且 $a \ne b \ne c \ne d$。
15+
16+
**说明**:
17+
18+
- 1ドル \le nums.length \le 1000$。
19+
- 1ドル \le nums[i] \le 10^4$。
20+
- $nums$ 中的所有元素互不相同。
21+
22+
**示例**:
23+
24+
- 示例 1:
25+
26+
```python
27+
输入:nums = [2,3,4,6]
28+
输出:8
29+
解释:存在 8 个满足题意的元组:
30+
(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3)
31+
(3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2)
32+
```
33+
34+
- 示例 2:
35+
36+
```python
37+
输入:nums = [1,2,4,5,10]
38+
输出:16
39+
解释:存在 16 个满足题意的元组:
40+
(1,10,2,5) , (1,10,5,2) , (10,1,2,5) , (10,1,5,2)
41+
(2,5,1,10) , (2,5,10,1) , (5,2,1,10) , (5,2,10,1)
42+
(2,10,4,5) , (2,10,5,4) , (10,2,4,5) , (10,2,5,4)
43+
(4,5,2,10) , (4,5,10,2) , (5,4,2,10) , (5,4,10,2)
44+
```
45+
46+
## 解题思路
47+
48+
### 思路 1:哈希表 + 数学
49+
50+
1. 二重循环遍历数组 $nums,ドル使用哈希表 $cnts$ 记录下所有不同 $nums[i] \times nums[j]$ 的结果。
51+
2. 因为满足 $a \times b = c \times d$ 的元组 $(a, b, c, d)$ 可以按照不同顺序进行组和,所以对于 $x$ 个 $nums[i] \times nums[j],ドル就有 $C_x^2$ 种组和方法。
52+
3. 遍历哈希表 $cnts$ 中所有值 $value,ドル将不同组和的方法数累积到答案 $ans$ 中。
53+
4. 遍历完返回答案 $ans$。
54+
55+
### 思路 1:代码
56+
57+
```Python
58+
class Solution:
59+
def tupleSameProduct(self, nums: List[int]) -> int:
60+
cnts = Counter()
61+
size = len(nums)
62+
for i in range(size):
63+
for j in range(i + 1, size):
64+
product = nums[i] * nums[j]
65+
cnts[product] += 1
66+
67+
ans = 0
68+
for key, value in cnts.items():
69+
ans += value * (value - 1) * 4
70+
71+
return ans
72+
```
73+
74+
### 思路 1:复杂度分析
75+
76+
- **时间复杂度**:$O(n^2),ドル其中 $n$ 表示数组 $nums$ 的长度。
77+
- **空间复杂度**:$O(n^2)$。
78+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# [1921. 消灭怪物的最大数量](https://leetcode.cn/problems/eliminate-maximum-number-of-monsters/)
2+
3+
- 标签:贪心、数组、排序
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [1921. 消灭怪物的最大数量 - 力扣](https://leetcode.cn/problems/eliminate-maximum-number-of-monsters/)
9+
10+
## 题目大意
11+
12+
**描述**:你正在玩一款电子游戏,在游戏中你需要保护城市免受怪物侵袭。给定一个下标从 0ドル$ 开始且大小为 $n$ 的整数数组 $dist,ドル其中 $dist[i]$ 是第 $i$ 个怪物与城市的初始距离(单位:米)。
13+
14+
怪物以恒定的速度走向城市。每个怪物的速度都以一个长度为 $n$ 的整数数组 $speed$ 表示,其中 $speed[i]$ 是第 $i$ 个怪物的速度(单位:千米/分)。
15+
16+
你有一种武器,一旦充满电,就可以消灭 一个 怪物。但是,武器需要 一分钟 才能充电。武器在游戏开始时是充满电的状态,怪物从 第 0ドル$ 分钟时开始移动。
17+
18+
一旦任一怪物到达城市,你就输掉了这场游戏。如果某个怪物 恰好 在某一分钟开始时到达城市(距离表示为 0ドル$),这也会被视为输掉 游戏,在你可以使用武器之前,游戏就会结束。
19+
20+
**要求**:返回在你输掉游戏前可以消灭的怪物的最大数量。如果你可以在所有怪物到达城市前将它们全部消灭,返回 $n$。
21+
22+
**说明**:
23+
24+
-
25+
26+
**示例**:
27+
28+
- 示例 1:
29+
30+
```python
31+
输入:dist = [1,3,4], speed = [1,1,1]
32+
输出:3
33+
解释:
34+
0 分钟开始时,怪物的距离是 [1,3,4],你消灭了第一个怪物。
35+
1 分钟开始时,怪物的距离是 [X,2,3],你消灭了第二个怪物。
36+
3 分钟开始时,怪物的距离是 [X,X,2],你消灭了第三个怪物。
37+
所有 3 个怪物都可以被消灭。
38+
```
39+
40+
- 示例 2:
41+
42+
```python
43+
输入:dist = [1,1,2,3], speed = [1,1,1,1]
44+
输出:1
45+
解释:
46+
0 分钟开始时,怪物的距离是 [1,1,2,3],你消灭了第一个怪物。
47+
1 分钟开始时,怪物的距离是 [X,0,1,2],所以你输掉了游戏。
48+
你只能消灭 1 个怪物。
49+
```
50+
51+
## 解题思路
52+
53+
### 思路 1:排序 + 贪心算法
54+
55+
对于第 $i$ 个怪物,最晚可被消灭的时间为 $times[i] = \lfloor \frac{dist[i] - 1}{speed[i]} \rfloor$。我们可以根据以上公式,将所有怪物最晚可被消灭时间存入数组 $times$ 中,然后对 $times$ 进行升序排序。
56+
57+
然后遍历数组 $times,ドル对于第 $i$ 个怪物:
58+
59+
1. 如果 $times[i] < i,ドル则说明第 $i$ 个怪物无法被消灭,直接返回 $i$ 即可。
60+
2. 如果 $times[i] \ge i,ドル则说明第 $i$ 个怪物可以被消灭,继续向下遍历。
61+
62+
如果遍历完数组 $times,ドル则说明所有怪物都可以被消灭,则返回 $n$。
63+
64+
### 思路 1:代码
65+
66+
```Python
67+
class Solution:
68+
def eliminateMaximum(self, dist: List[int], speed: List[int]) -> int:
69+
times = []
70+
for d, s in zip(dist, speed):
71+
time = (d - 1) // s
72+
times.append(time)
73+
times.sort()
74+
75+
size = len(times)
76+
for i in range(size):
77+
if times[i] < i:
78+
return i
79+
80+
return size
81+
```
82+
83+
### 思路 1:复杂度分析
84+
85+
- **时间复杂度**:$O(n \times \log n),ドル其中 $n$ 为数组 $dist$ 的长度。
86+
- **空间复杂度**:$O(n)$。
87+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /