Skip to content

Commit

Permalink
更新题解列表
Browse files Browse the repository at this point in the history
  • Loading branch information
itcharge committed Jan 11, 2024
1 parent 02fec6c commit fd1e206
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 23 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 题解(已完成 841 道)
# LeetCode 题解(已完成 844 道)

| 题号 | 标题 | 题解 | 标签 | 难度 |
| :------ | :------ | :------ | :------ | :------ |
Expand Down Expand Up @@ -555,9 +555,11 @@
| 1313 | [解压缩编码列表](https://leetcode.cn/problems/decompress-run-length-encoded-list/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1313.%20%E8%A7%A3%E5%8E%8B%E7%BC%A9%E7%BC%96%E7%A0%81%E5%88%97%E8%A1%A8.md) | 数组 | 简单 |
| 1317 | [将整数转换为两个无零整数的和](https://leetcode.cn/problems/convert-integer-to-the-sum-of-two-no-zero-integers/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1317.%20%E5%B0%86%E6%95%B4%E6%95%B0%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%B8%A4%E4%B8%AA%E6%97%A0%E9%9B%B6%E6%95%B4%E6%95%B0%E7%9A%84%E5%92%8C.md) | 数学 | 简单 |
| 1319 | [连通网络的操作次数](https://leetcode.cn/problems/number-of-operations-to-make-network-connected/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1319.%20%E8%BF%9E%E9%80%9A%E7%BD%91%E7%BB%9C%E7%9A%84%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0.md) | 深度优先搜索、广度优先搜索、并查集、图 | 中等 |
| 1324 | [竖直打印单词](https://leetcode.cn/problems/print-words-vertically/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1324.%20%E7%AB%96%E7%9B%B4%E6%89%93%E5%8D%B0%E5%8D%95%E8%AF%8D.md) | 数组、字符串、模拟 | 中等 |
| 1338 | [数组大小减半](https://leetcode.cn/problems/reduce-array-size-to-the-half/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1338.%20%E6%95%B0%E7%BB%84%E5%A4%A7%E5%B0%8F%E5%87%8F%E5%8D%8A.md) | 贪心、数组、哈希表、排序、堆(优先队列) | 中等 |
| 1343 | [大小为 K 且平均值大于等于阈值的子数组数目](https://leetcode.cn/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1343.%20%E5%A4%A7%E5%B0%8F%E4%B8%BA%20K%20%E4%B8%94%E5%B9%B3%E5%9D%87%E5%80%BC%E5%A4%A7%E4%BA%8E%E7%AD%89%E4%BA%8E%E9%98%88%E5%80%BC%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E6%95%B0%E7%9B%AE.md) | 数组、滑动窗口 | 中等 |
| 1344 | [时钟指针的夹角](https://leetcode.cn/problems/angle-between-hands-of-a-clock/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1344.%20%E6%97%B6%E9%92%9F%E6%8C%87%E9%92%88%E7%9A%84%E5%A4%B9%E8%A7%92.md) | 数学 | 中等 |
| 1347 | [制造字母异位词的最小步骤数](https://leetcode.cn/problems/minimum-number-of-steps-to-make-two-strings-anagram/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1347.%20%E5%88%B6%E9%80%A0%E5%AD%97%E6%AF%8D%E5%BC%82%E4%BD%8D%E8%AF%8D%E7%9A%84%E6%9C%80%E5%B0%8F%E6%AD%A5%E9%AA%A4%E6%95%B0.md) | 哈希表、字符串、计数 | 中等 |
| 1349 | [参加考试的最大学生数](https://leetcode.cn/problems/maximum-students-taking-exam/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1349.%20%E5%8F%82%E5%8A%A0%E8%80%83%E8%AF%95%E7%9A%84%E6%9C%80%E5%A4%A7%E5%AD%A6%E7%94%9F%E6%95%B0.md) | 位运算、数组、动态规划、状态压缩、矩阵 | 困难 |
| 1358 | [包含所有三种字符的子字符串数目](https://leetcode.cn/problems/number-of-substrings-containing-all-three-characters/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1358.%20%E5%8C%85%E5%90%AB%E6%89%80%E6%9C%89%E4%B8%89%E7%A7%8D%E5%AD%97%E7%AC%A6%E7%9A%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%95%B0%E7%9B%AE.md) | 哈希表、字符串、滑动窗口 | 中等 |
| 1381 | [设计一个支持增量操作的栈](https://leetcode.cn/problems/design-a-stack-with-increment-operation/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1381.%20%E8%AE%BE%E8%AE%A1%E4%B8%80%E4%B8%AA%E6%94%AF%E6%8C%81%E5%A2%9E%E9%87%8F%E6%93%8D%E4%BD%9C%E7%9A%84%E6%A0%88.md) | 栈、设计、数组 | 中等 |
Expand All @@ -572,6 +574,7 @@
| 1450 | [在既定时间做作业的学生人数](https://leetcode.cn/problems/number-of-students-doing-homework-at-a-given-time/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1450.%20%E5%9C%A8%E6%97%A2%E5%AE%9A%E6%97%B6%E9%97%B4%E5%81%9A%E4%BD%9C%E4%B8%9A%E7%9A%84%E5%AD%A6%E7%94%9F%E4%BA%BA%E6%95%B0.md) | 数组 | 简单 |
| 1451 | [重新排列句子中的单词](https://leetcode.cn/problems/rearrange-words-in-a-sentence/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1451.%20%E9%87%8D%E6%96%B0%E6%8E%92%E5%88%97%E5%8F%A5%E5%AD%90%E4%B8%AD%E7%9A%84%E5%8D%95%E8%AF%8D.md) | 字符串、排序 | 中等 |
| 1456 | [定长子串中元音的最大数目](https://leetcode.cn/problems/maximum-number-of-vowels-in-a-substring-of-given-length/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1456.%20%E5%AE%9A%E9%95%BF%E5%AD%90%E4%B8%B2%E4%B8%AD%E5%85%83%E9%9F%B3%E7%9A%84%E6%9C%80%E5%A4%A7%E6%95%B0%E7%9B%AE.md) | 字符串、滑动窗口 | 中等 |
| 1476 | [子矩形查询](https://leetcode.cn/problems/subrectangle-queries/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1476.%20%E5%AD%90%E7%9F%A9%E5%BD%A2%E6%9F%A5%E8%AF%A2.md) | 设计、数组、矩阵 | 中等 |
| 1480 | [一维数组的动态和](https://leetcode.cn/problems/running-sum-of-1d-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1480.%20%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84%E7%9A%84%E5%8A%A8%E6%80%81%E5%92%8C.md) | 数组、前缀和 | 简单 |
| 1482 | [制作 m 束花所需的最少天数](https://leetcode.cn/problems/minimum-number-of-days-to-make-m-bouquets/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1482.%20%E5%88%B6%E4%BD%9C%20m%20%E6%9D%9F%E8%8A%B1%E6%89%80%E9%9C%80%E7%9A%84%E6%9C%80%E5%B0%91%E5%A4%A9%E6%95%B0.md) | 数组、二分查找 | 中等 |
| 1486 | [数组异或操作](https://leetcode.cn/problems/xor-operation-in-an-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1486.%20%E6%95%B0%E7%BB%84%E5%BC%82%E6%88%96%E6%93%8D%E4%BD%9C.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 题解(已完成 841 道)](./Contents/00.Introduction/04.Solutions-List.md)
## [12. LeetCode 题解(已完成 844 道)](./Contents/00.Introduction/04.Solutions-List.md)
53 changes: 42 additions & 11 deletions Solutions/1208. 尽可能使字符串相等.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,50 @@

## 题目大意

给定两个长度相同的字符串,`s``t`。将 `s` 中的第 `i` 个字符变到 `t` 中的第 `i` 个字符需要 $| s[i] - t[i] |$ 的开销(开销可能为 `0`),也就是两个字符的 ASCII 码值的差的绝对值。用于变更字符串的最大预算是 `maxCost`。在转化字符串时,总开销应当小于等于该预算,这也意味着字符串的转化可能是不完全的。
**描述**给定两个长度相同的字符串,$s$$t$。将 $s$ 中的第 $i$ 个字符变到 $t$ 中的第 $i$ 个字符需要 $| s[i] - t[i] |$ 的开销(开销可能为 $0$),也就是两个字符的 ASCII 码值的差的绝对值。用于变更字符串的最大预算是 $maxCost$。在转化字符串时,总开销应当小于等于该预算,这也意味着字符串的转化可能是不完全的。

要求:如果你可以将 `s` 的子字符串转化为它在 `t` 中对应的子字符串,则返回可以转化的最大长度。如果 `s` 中没有子字符串可以转化成 `t` 中对应的子字符串,则返回 `0`
**要求**:如果你可以将 $s$ 的子字符串转化为它在 $t$ 中对应的子字符串,则返回可以转化的最大长度。如果 $s$ 中没有子字符串可以转化成 $t$ 中对应的子字符串,则返回 $0$。

**说明**

- $1 \le s.length, t.length \le 10^5$。
- $0 \le maxCost \le 10^6$。
- $s$ 和 $t$ 都只含小写英文字母。

**示例**

- 示例 1:

```python
输入:s = "abcd", t = "bcdf", maxCost = 3
输出:3
解释:s 中的 "abc" 可以变为 "bcd"。开销为 3,所以最大长度为 3
```

- 示例 2:

```python
输入:s = "abcd", t = "cdef", maxCost = 3
输出:1
解释:s 中的任一字符要想变成 t 中对应的字符,其开销都是 2。因此,最大长度为 1
```

## 解题思路

维护一个滑动窗口 `window_sum` 用于记录窗口内的开销总和,保证窗口内的开销总和小于等于 `maxCost`。使用 `ans` 记录可以转化的最大长度。具体做法如下:
### 思路 1:滑动窗口

使用两个指针 `left``right`。分别指向滑动窗口的左右边界,保证窗口内所有元素转化开销总和小于等于 `maxCost`
维护一个滑动窗口 $window\underline{}sum$ 用于记录窗口内的开销总和,保证窗口内的开销总和小于等于 $maxCost$。使用 $ans$ 记录可以转化的最大长度。具体做法如下:

- 先统计出 `s` 中第 `i` 个字符变为 `t` 的第 `i` 个字符的开销,用数组 `costs` 保存。
- 一开始,`left``right` 都指向 `0`
- 将最右侧字符的转变开销填入窗口中,向右移动 `right`
- 直到窗口内开销总和 `window_sum` 大于 `maxCost`。则不断右移 `left`,缩小窗口长度。直到 `window_sum <= maxCost` 时,更新可以转换的最大长度 `ans`
- 向右移动 `right`,直到 `right >= len(s)` 为止。
- 输出答案 `ans`
使用两个指针 $left$、$right$。分别指向滑动窗口的左右边界,保证窗口内所有元素转化开销总和小于等于 $maxCost$。

## 代码
- 先统计出 $s$ 中第 $i$ 个字符变为 $t$ 的第 $i$ 个字符的开销,用数组 $costs$ 保存。
- 一开始,$left$、$right$ 都指向 $0$。
- 将最右侧字符的转变开销填入窗口中,向右移动 $right$。
- 直到窗口内开销总和 $window\underline{}sum$ 大于 $maxCost$。则不断右移 $left$,缩小窗口长度。直到 $window\underline{}sum \le maxCost$ 时,更新可以转换的最大长度 $ans$。
- 向右移动 $right$,直到 $right \ge len(s)$ 为止。
- 输出答案 $ans$。

### 思路 1:代码

```python
class Solution:
Expand All @@ -50,3 +76,8 @@ class Solution:
return ans
```

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

- **时间复杂度**
- **空间复杂度**

87 changes: 87 additions & 0 deletions Solutions/1324. 竖直打印单词.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# [1324. 竖直打印单词](https://leetcode.cn/problems/print-words-vertically/)

- 标签:数组、字符串、模拟
- 难度:中等

## 题目链接

- [1324. 竖直打印单词 - 力扣](https://leetcode.cn/problems/print-words-vertically/)

## 题目大意

**描述**:给定一个字符串 $s$。

**要求**:按照单词在 $s$ 中出现顺序将它们全部竖直返回。

**说明**

- 单词应该以字符串列表的形式返回,必要时用空格补位,但输出尾部的空格需要删除(不允许尾随空格)。
- 每个单词只能放在一列上,每一列中也只能有一个单词。
- $1 \le s.length \le 200$。
- $s$ 仅含大写英文字母。
- 题目数据保证两个单词之间只有一个空格。

**示例**

- 示例 1:

```python
输入:s = "HOW ARE YOU"
输出:["HAY","ORO","WEU"]
解释:每个单词都应该竖直打印。
"HAY"
"ORO"
"WEU"
```

- 示例 2:

```python
输入:s = "TO BE OR NOT TO BE"
输出:["TBONTB","OEROOE"," T"]
解释:题目允许使用空格补位,但不允许输出末尾出现空格。
"TBONTB"
"OEROOE"
" T"
```

## 解题思路

### 思路 1:模拟

1. 将字符串 $s$ 按空格分割为单词数组 $words$。
2. 计算出单词数组 $words$ 中单词的最大长度 $max\underline{}len$。
3. 第一重循环遍历竖直单词的每个单词位置 $i$,第二重循环遍历当前第 $j$ 个单词。
1. 如果当前单词没有第 $i$ 个字符(当前单词的长度超过了单词位置 $i$),则将空格插入到竖直单词中。
2. 如果当前单词有第 $i$ 个字符,泽讲当前单词的第 $i$ 个字符插入到竖直单词中。
4. 第二重循环遍历完,将竖直单词去除尾随空格,并加入到答案数组中。
5. 第一重循环遍历完,则返回答案数组。

### 思路 1:代码

```Python
class Solution:
def printVertically(self, s: str) -> List[str]:
words = s.split(' ')
max_len = 0
for word in words:
max_len = max(len(word), max_len)

res = []
for i in range(max_len):
ans = ""
for j in range(len(words)):
if i + 1 > len(words[j]):
ans += ' '
else:
ans += words[j][i]
res.append(ans.rstrip())

return res
```

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

- **时间复杂度**:$O(n \times max(|word|))$,其中 $n$ 为字符串 $s$ 中的单词个数,$max(|word|)$ 是最长的单词长度。。
- **空间复杂度**:$O(n \times max(|word|))$。

74 changes: 74 additions & 0 deletions Solutions/1347. 制造字母异位词的最小步骤数.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# [1347. 制造字母异位词的最小步骤数](https://leetcode.cn/problems/minimum-number-of-steps-to-make-two-strings-anagram/)

- 标签:哈希表、字符串、计数
- 难度:中等

## 题目链接

- [1347. 制造字母异位词的最小步骤数 - 力扣](https://leetcode.cn/problems/minimum-number-of-steps-to-make-two-strings-anagram/)

## 题目大意

**描述**:给定两个长度相等的字符串 $s$ 和 $t$。每一个步骤中,你可以选择将 $t$ 中任一个字符替换为另一个字符。

**要求**:返回使 $t$ 成为 $s$ 的字母异位词的最小步骤数。

**说明**

- **字母异位词**:指字母相同,但排列不同(也可能相同)的字符串。
- $1 \le s.length \le 50000$。
- $s.length == t.length$。
- $s$ 和 $t$ 只包含小写英文字母。

**示例**

- 示例 1:

```python
输出:s = "bab", t = "aba"
输出:1
提示:用 'b' 替换 t 中的第一个 'a',t = "bba" 是 s 的一个字母异位词。
```

- 示例 2:

```python
输出:s = "leetcode", t = "practice"
输出:5
提示:用合适的字符替换 t 中的 'p', 'r', 'a', 'i''c',使 t 变成 s 的字母异位词。
```

## 解题思路

### 思路 1:哈希表

题目要求使 $t$ 成为 $s$ 的字母异位词,则只需要 $t$ 和 $s$ 对应的每种字符数量相一致即可,无需考虑字符位置。

因为每一次转换都会减少一个字符,并增加另一个字符。

1. 我们使用两个哈希表 $cnts\underline{}s$、$cnts\underline{}t$ 分别对 $t$ 和 $s$ 中的字符进行计数,并求出两者的交集。
2. 遍历交集中的字符种类,以及对应的字符数量。
3. 对于当前字符 $key$,如果当前字符串 $s$ 中的字符 $key$ 的数量小于字符串 $t$ 中字符 $key$ 的数量,即 $cnts\underline{}s[key] < cnts\underline{}t[key]$。则 $s$ 中需要补齐的字符数量就是需要的最小步数,将其累加到答案中。
4. 遍历完返回答案。

### 思路 1:代码

```Python
class Solution:
def minSteps(self, s: str, t: str) -> int:
cnts_s, cnts_t = Counter(s), Counter(t)
cnts = cnts_s | cnts_t

ans = 0
for key, cnt in cnts.items():
if cnts_s[key] < cnts_t[key]:
ans += cnts_t[key] - cnts_s[key]

return ans
```

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

- **时间复杂度**:$O(m + n)$,其中 $m$、$n$ 分别为字符串 $s$、$t$ 的长度。
- **空间复杂度**:$O(|\sum|)$,其中 $\sum$ 是字符集,本题中 $| \sum | = 26$。

115 changes: 115 additions & 0 deletions Solutions/1476. 子矩形查询.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# [1476. 子矩形查询](https://leetcode.cn/problems/subrectangle-queries/)

- 标签:设计、数组、矩阵
- 难度:中等

## 题目链接

- [1476. 子矩形查询 - 力扣](https://leetcode.cn/problems/subrectangle-queries/)

## 题目大意

**要求**:实现一个类 SubrectangleQueries,它的构造函数的参数是一个 $rows \times cols $的矩形(这里用整数矩阵表示),并支持以下两种操作:

1. `updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)`:用 $newValue$ 更新以 $(row1,col1)$ 为左上角且以 $(row2,col2)$ 为右下角的子矩形。

2. `getValue(int row, int col)`:返回矩形中坐标 (row,col) 的当前值。

**说明**

- 最多有 $500$ 次 `updateSubrectangle``getValue` 操作。
- $1 <= rows, cols <= 100$。
- $rows == rectangle.length$。
- $cols == rectangle[i].length$。
- $0 <= row1 <= row2 < rows$。
- $0 <= col1 <= col2 < cols$。
- $1 <= newValue, rectangle[i][j] <= 10^9$。
- $0 <= row < rows$。
- $0 <= col < cols$。

**示例**

- 示例 1:

```python
输入:
["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue","getValue"]
[[[[1,2,1],[4,3,4],[3,2,1],[1,1,1]]],[0,2],[0,0,3,2,5],[0,2],[3,1],[3,0,3,2,10],[3,1],[0,2]]
输出:
[null,1,null,5,5,null,10,5]
解释:
SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,2,1],[4,3,4],[3,2,1],[1,1,1]]);
// 初始的 (4x3) 矩形如下:
// 1 2 1
// 4 3 4
// 3 2 1
// 1 1 1
subrectangleQueries.getValue(0, 2); // 返回 1
subrectangleQueries.updateSubrectangle(0, 0, 3, 2, 5);
// 此次更新后矩形变为:
// 5 5 5
// 5 5 5
// 5 5 5
// 5 5 5
subrectangleQueries.getValue(0, 2); // 返回 5
subrectangleQueries.getValue(3, 1); // 返回 5
subrectangleQueries.updateSubrectangle(3, 0, 3, 2, 10);
// 此次更新后矩形变为:
// 5 5 5
// 5 5 5
// 5 5 5
// 10 10 10
subrectangleQueries.getValue(3, 1); // 返回 10
subrectangleQueries.getValue(0, 2); // 返回 5
```

- 示例 2:

```python
输入:
["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue"]
[[[[1,1,1],[2,2,2],[3,3,3]]],[0,0],[0,0,2,2,100],[0,0],[2,2],[1,1,2,2,20],[2,2]]
输出:
[null,1,null,100,100,null,20]
解释:
SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,1,1],[2,2,2],[3,3,3]]);
subrectangleQueries.getValue(0, 0); // 返回 1
subrectangleQueries.updateSubrectangle(0, 0, 2, 2, 100);
subrectangleQueries.getValue(0, 0); // 返回 100
subrectangleQueries.getValue(2, 2); // 返回 100
subrectangleQueries.updateSubrectangle(1, 1, 2, 2, 20);
subrectangleQueries.getValue(2, 2); // 返回 20

```

## 解题思路

### 思路 1:暴力

矩形最大为 $row \times col == 100 \times 100$,则每次更新最多需要更新 $10000$ 个值,更新次数最多为 $500$ 次。

用暴力更新的方法最多需要更新 $5000000$ 次,我们可以尝试一下用暴力更新的方法解决本题(提交后发现可以通过)。

### 思路 1:代码

```Python
class SubrectangleQueries:

def __init__(self, rectangle: List[List[int]]):
self.rectangle = rectangle


def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int, newValue: int) -> None:
for row in range(row1, row2 + 1):
for col in range(col1, col2 + 1):
self.rectangle[row][col] = newValue


def getValue(self, row: int, col: int) -> int:
return self.rectangle[row][col]
```

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

- **时间复杂度**:$O(row \times col \times 500)$。
- **空间复杂度**:$O(row \times col)$。
Loading

0 comments on commit fd1e206

Please sign in to comment.