Skip to content

Commit

Permalink
更新题解列表
Browse files Browse the repository at this point in the history
  • Loading branch information
itcharge committed Jan 15, 2024
1 parent 5a34693 commit 8ea6473
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Contents/00.Introduction/04.Solutions-List.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# LeetCode 题解(已完成 847 道)
# LeetCode 题解(已完成 850 道)

| 题号 | 标题 | 题解 | 标签 | 难度 |
| :------ | :------ | :------ | :------ | :------ |
Expand Down Expand Up @@ -600,13 +600,15 @@
| 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) | 位运算、树、动态规划、状态压缩、枚举 | 困难 |
| 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) | 深度优先搜索、广度优先搜索、并查集、数组、二分查找、矩阵、堆(优先队列) | 中等 |
| 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) | 数组、动态规划、模拟 | 简单 |
| 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) | 哈希表、字符串、排序 | 中等 |
| 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) | 数组、哈希表、二分查找、前缀和、滑动窗口 | 中等 |
| 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) | 数组、矩阵 | 简单 |
| 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) | 数组、哈希表、滑动窗口 | 中等 |
| 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) | 字典树、字符串、后缀数组、哈希函数、滚动哈希 | 中等 |
| 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) | 贪心、数组、排序 | 简单 |
| 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) | 数学 | 简单 |
| 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) | 位运算、数组 | 简单 |
| 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) | 数组、哈希表 | 中等 |
| 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) | 贪心、字符串 | 简单 |
| 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) | 哈希表、数学、计数 | 简单 |
| 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) | 数组、动态规划 | 中等 |
Expand All @@ -625,6 +627,7 @@
| 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) | 数组、哈希表、前缀和 | 简单 |
| 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) | 哈希表、字符串、计数 | 简单 |
| 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) | 贪心、数学、字符串 | 简单 |
| 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) | 贪心、数组、排序 | 中等 |
| 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) | 数学、枚举 | 简单 |
| 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) | 数组 | 简单 |
| 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) | 哈希表、字符串、前缀和 | 中等 |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,4 @@
- [动态规划优化题目](./Contents/10.Dynamic-Programming/11.DP-Optimization/04.DP-Optimization-List.md)

## 11. 附加内容
## [12. LeetCode 题解(已完成 847 道)](./Contents/00.Introduction/04.Solutions-List.md)
## [12. LeetCode 题解(已完成 850 道)](./Contents/00.Introduction/04.Solutions-List.md)
77 changes: 77 additions & 0 deletions Solutions/1657. 确定两个字符串是否接近.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# [1657. 确定两个字符串是否接近](https://leetcode.cn/problems/determine-if-two-strings-are-close/)

- 标签:哈希表、字符串、排序
- 难度:中等

## 题目链接

- [1657. 确定两个字符串是否接近 - 力扣](https://leetcode.cn/problems/determine-if-two-strings-are-close/)

## 题目大意

**描述**:如果可以使用以下操作从一个字符串得到另一个字符串,则认为两个字符串 接近 :

- 操作 1:交换任意两个现有字符。
- 例如,`abcde` -> `aecdb`
- 操作 2:将一个 现有 字符的每次出现转换为另一个现有字符,并对另一个字符执行相同的操作。
- 例如,`aacabb` -> `bbcbaa`(所有 `a` 转化为 `b`,而所有的 `b` 转换为 `a` )。

给定两个字符串,$word1$ 和 $word2$。

**要求**:如果 $word1$ 和 $word2$ 接近 ,就返回 $True$;否则,返回 $False$。

**说明**

- $1 \le word1.length, word2.length \le 10^5$。
- $word1$ 和 $word2$ 仅包含小写英文字母。

**示例**

- 示例 1:

```python
输入:word1 = "abc", word2 = "bca"
输出:True
解释:2 次操作从 word1 获得 word2 。
执行操作 1"abc" -> "acb"
执行操作 1"acb" -> "bca"
```

- 示例 2:

```python
输入:word1 = "a", word2 = "aa"
输出:False
解释:不管执行多少次操作,都无法从 word1 得到 word2 ,反之亦然。
```

## 解题思路

### 思路 1:模拟

无论是操作 1,还是操作 2,只是对字符位置进行交换,而不会产生或者删除字符。

则我们只需要检查两个字符串的字符种类以及每种字符的个数是否相同即可。

具体步骤如下:

1. 分别使用哈希表 $cnts1$、$cnts2$ 统计每个字符串中的字符种类,每种字符的个数。
2. 判断两者的字符种类是否相等,并且判断每种字符的个数是否相同。
3. 如果字符种类相同,且每种字符的个数完全相同,则返回 $True$,否则,返回 $False$。

### 思路 1:代码

```Python
class Solution:
def closeStrings(self, word1: str, word2: str) -> bool:
cnts1 = Counter(word1)
cnts2 = Counter(word2)

return cnts1.keys() == cnts2.keys() and sorted(cnts1.values()) == sorted(cnts2.values())
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(max(n1, n2) + |\sum| \times \log | \sum |)$,其中 $n1$、$n2$ 分别为字符串 $word1$、$word2$ 的长度,$\sum$ 为字符集,本题中 $| \sum | = 26$。
- **空间复杂度**:$O(| \sum |)$。

78 changes: 78 additions & 0 deletions Solutions/1726. 同积元组.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# [1726. 同积元组](https://leetcode.cn/problems/tuple-with-same-product/)

- 标签:数组、哈希表
- 难度:中等

## 题目链接

- [1726. 同积元组 - 力扣](https://leetcode.cn/problems/tuple-with-same-product/)

## 题目大意

**描述**:给定一个由不同正整数组成的数组 $nums$。

**要求**:返回满足 $a \times b = c \times d$ 的元组 $(a, b, c, d)$ 的数量。其中 $a$、$b$、$c$ 和 $d$ 都是 $nums$ 中的元素,且 $a \ne b \ne c \ne d$。

**说明**

- $1 \le nums.length \le 1000$。
- $1 \le nums[i] \le 10^4$。
- $nums$ 中的所有元素互不相同。

**示例**

- 示例 1:

```python
输入:nums = [2,3,4,6]
输出:8
解释:存在 8 个满足题意的元组:
(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3)
(3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2)
```

- 示例 2:

```python
输入:nums = [1,2,4,5,10]
输出:16
解释:存在 16 个满足题意的元组:
(1,10,2,5) , (1,10,5,2) , (10,1,2,5) , (10,1,5,2)
(2,5,1,10) , (2,5,10,1) , (5,2,1,10) , (5,2,10,1)
(2,10,4,5) , (2,10,5,4) , (10,2,4,5) , (10,2,5,4)
(4,5,2,10) , (4,5,10,2) , (5,4,2,10) , (5,4,10,2)
```

## 解题思路

### 思路 1:哈希表 + 数学

1. 二重循环遍历数组 $nums$,使用哈希表 $cnts$ 记录下所有不同 $nums[i] \times nums[j]$ 的结果。
2. 因为满足 $a \times b = c \times d$ 的元组 $(a, b, c, d)$ 可以按照不同顺序进行组和,所以对于 $x$ 个 $nums[i] \times nums[j]$,就有 $C_x^2$ 种组和方法。
3. 遍历哈希表 $cnts$ 中所有值 $value$,将不同组和的方法数累积到答案 $ans$ 中。
4. 遍历完返回答案 $ans$。

### 思路 1:代码

```Python
class Solution:
def tupleSameProduct(self, nums: List[int]) -> int:
cnts = Counter()
size = len(nums)
for i in range(size):
for j in range(i + 1, size):
product = nums[i] * nums[j]
cnts[product] += 1

ans = 0
for key, value in cnts.items():
ans += value * (value - 1) * 4

return ans
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(n^2)$,其中 $n$ 表示数组 $nums$ 的长度。
- **空间复杂度**:$O(n^2)$。

87 changes: 87 additions & 0 deletions Solutions/1921. 消灭怪物的最大数量.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# [1921. 消灭怪物的最大数量](https://leetcode.cn/problems/eliminate-maximum-number-of-monsters/)

- 标签:贪心、数组、排序
- 难度:中等

## 题目链接

- [1921. 消灭怪物的最大数量 - 力扣](https://leetcode.cn/problems/eliminate-maximum-number-of-monsters/)

## 题目大意

**描述**:你正在玩一款电子游戏,在游戏中你需要保护城市免受怪物侵袭。给定一个下标从 $0$ 开始且大小为 $n$ 的整数数组 $dist$,其中 $dist[i]$ 是第 $i$ 个怪物与城市的初始距离(单位:米)。

怪物以恒定的速度走向城市。每个怪物的速度都以一个长度为 $n$ 的整数数组 $speed$ 表示,其中 $speed[i]$ 是第 $i$ 个怪物的速度(单位:千米/分)。

你有一种武器,一旦充满电,就可以消灭 一个 怪物。但是,武器需要 一分钟 才能充电。武器在游戏开始时是充满电的状态,怪物从 第 $0$ 分钟时开始移动。

一旦任一怪物到达城市,你就输掉了这场游戏。如果某个怪物 恰好 在某一分钟开始时到达城市(距离表示为 $0$),这也会被视为输掉 游戏,在你可以使用武器之前,游戏就会结束。

**要求**:返回在你输掉游戏前可以消灭的怪物的最大数量。如果你可以在所有怪物到达城市前将它们全部消灭,返回 $n$。

**说明**

-

**示例**

- 示例 1:

```python
输入:dist = [1,3,4], speed = [1,1,1]
输出:3
解释:
0 分钟开始时,怪物的距离是 [1,3,4],你消灭了第一个怪物。
1 分钟开始时,怪物的距离是 [X,2,3],你消灭了第二个怪物。
3 分钟开始时,怪物的距离是 [X,X,2],你消灭了第三个怪物。
所有 3 个怪物都可以被消灭。
```

- 示例 2:

```python
输入:dist = [1,1,2,3], speed = [1,1,1,1]
输出:1
解释:
0 分钟开始时,怪物的距离是 [1,1,2,3],你消灭了第一个怪物。
1 分钟开始时,怪物的距离是 [X,0,1,2],所以你输掉了游戏。
你只能消灭 1 个怪物。
```

## 解题思路

### 思路 1:排序 + 贪心算法

对于第 $i$ 个怪物,最晚可被消灭的时间为 $times[i] = \lfloor \frac{dist[i] - 1}{speed[i]} \rfloor$。我们可以根据以上公式,将所有怪物最晚可被消灭时间存入数组 $times$ 中,然后对 $times$ 进行升序排序。

然后遍历数组 $times$,对于第 $i$ 个怪物:

1. 如果 $times[i] < i$,则说明第 $i$ 个怪物无法被消灭,直接返回 $i$ 即可。
2. 如果 $times[i] \ge i$,则说明第 $i$ 个怪物可以被消灭,继续向下遍历。

如果遍历完数组 $times$,则说明所有怪物都可以被消灭,则返回 $n$。

### 思路 1:代码

```Python
class Solution:
def eliminateMaximum(self, dist: List[int], speed: List[int]) -> int:
times = []
for d, s in zip(dist, speed):
time = (d - 1) // s
times.append(time)
times.sort()

size = len(times)
for i in range(size):
if times[i] < i:
return i

return size
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(n \times \log n)$,其中 $n$ 为数组 $dist$ 的长度。
- **空间复杂度**:$O(n)$。

0 comments on commit 8ea6473

Please sign in to comment.