Skip to content

Commit bded865

Browse files
committed
✨feat: add 1051、1737
1 parent 23cea7e commit bded865

File tree

4 files changed

+215
-0
lines changed

4 files changed

+215
-0
lines changed

Index/排序.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
| [954. 二倍数对数组](https://leetcode-cn.com/problems/array-of-doubled-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/array-of-doubled-pairs/solution/by-ac_oier-d1z7/) | 中等 | 🤩🤩🤩 |
2121
| [969. 煎饼排序](https://leetcode-cn.com/problems/pancake-sorting/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/pancake-sorting/solution/gong-shui-san-xie-mou-pao-pai-xu-yun-yon-c0mn/) | 中等 | 🤩🤩🤩🤩🤩 |
2222
| [987. 二叉树的垂序遍历](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-h-wfm3/) | 困难 | 🤩🤩🤩🤩🤩 |
23+
| [1051. 高度检查器](https://leetcode.cn/problems/height-checker/) | [LeetCode 题解链接](https://leetcode.cn/problems/height-checker/solution/by-ac_oier-8xoj/) | 简单 | 🤩🤩🤩🤩🤩 |
2324
| [1833. 雪糕的最大数量](https://leetcode-cn.com/problems/maximum-ice-cream-bars/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-ice-cream-bars/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-yrhjx/) | 中等 | 🤩🤩🤩🤩🤩 |
2425
| [1834. 单线程 CPU](https://leetcode-cn.com/problems/single-threaded-cpu/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/single-threaded-cpu/solution/gong-shui-san-xie-shu-ju-jie-gou-yun-yon-1qk0/) | 中等 | 🤩🤩🤩🤩 |
2526
| [1838. 最高频元素的频数](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/solution/gong-shui-san-xie-cong-mei-ju-dao-pai-xu-kxnk/) | 中等 | 🤩🤩🤩 |

Index/模拟.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
| [1005. K 次取反后最大化的数组和](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/solution/gong-shui-san-xie-jian-dan-fen-qing-kuan-6qwu/) | 简单 | 🤩🤩🤩🤩 |
116116
| [1021. 删除最外层的括号](https://leetcode.cn/problems/remove-outermost-parentheses/) | [LeetCode 题解链接](https://leetcode.cn/problems/remove-outermost-parentheses/solution/by-ac_oier-jmxi/) | 简单 | 🤩🤩🤩🤩 |
117117
| [1047. 删除字符串中的所有相邻重复项](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/solution/cong-30-dao-100wu-chong-shi-xian-jie-jue-vkah/) | 简单 | 🤩🤩🤩🤩 |
118+
| [1051. 高度检查器](https://leetcode.cn/problems/height-checker/) | [LeetCode 题解链接](https://leetcode.cn/problems/height-checker/solution/by-ac_oier-8xoj/) | 简单 | 🤩🤩🤩 |
118119
| [1078. Bigram 分词](https://leetcode-cn.com/problems/occurrences-after-bigram/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/occurrences-after-bigram/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-qyki/) | 简单 | 🤩🤩🤩🤩 |
119120
| [1104. 二叉树寻路](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-rw2d/) | 中等 | 🤩🤩🤩 |
120121
| [1154. 一年中的第几天](https://leetcode-cn.com/problems/day-of-the-year/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/day-of-the-year/solution/gong-shui-san-xie-jian-dan-qian-zhui-he-lwo2g/) | 简单 | 🤩🤩🤩🤩 |
@@ -141,6 +142,7 @@
141142
| [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/) | 简单 | 🤩🤩🤩 |
142143
| [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/) | 简单 | 🤩🤩🤩🤩 |
143144
| [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/) | 简单 | 🤩🤩🤩🤩 |
145+
| [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/) | 中等 | 🤩🤩🤩🤩 |
144146
| [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/) | 中等 | 🤩🤩🤩🤩 |
145147
| [1748. 唯一元素的和](https://leetcode-cn.com/problems/sum-of-unique-elements/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-unique-elements/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-atnd/) | 简单 | 🤩🤩🤩🤩 |
146148
| [1763. 最长的美好子字符串](https://leetcode-cn.com/problems/longest-nice-substring/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-nice-substring/solution/cong-shu-ju-fan-wei-xuan-ze-he-gua-suan-n3y2a/) | 简单 | 🤩🤩🤩 |
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1051. 高度检查器](https://leetcode.cn/problems/height-checker/solution/by-ac_oier-8xoj/)** ,难度为 **简单**
4+
5+
Tag : 「排序」、「模拟」
6+
7+
8+
9+
学校打算为全体学生拍一张年度纪念照。根据要求,学生需要按照 非递减 的高度顺序排成一行。
10+
11+
排序后的高度情况用整数数组 `expected` 表示,其中 `expected[i]` 是预计排在这一行中第 `i` 位的学生的高度(下标从 $0$ 开始)。
12+
13+
给你一个整数数组 `heights`,表示 当前学生站位 的高度情况。`heights[i]` 是这一行中第 `i` 位学生的高度(下标从 $0$ 开始)。
14+
15+
返回满足 `heights[i] != expected[i]` 的 下标数量 。
16+
17+
示例:
18+
```
19+
输入:heights = [1,1,4,2,1,3]
20+
21+
输出:3
22+
23+
解释:
24+
高度:[1,1,4,2,1,3]
25+
预期:[1,1,1,2,3,4]
26+
下标 2 、4 、5 处的学生高度不匹配。
27+
```
28+
示例 2:
29+
```
30+
输入:heights = [5,1,2,3,4]
31+
32+
输出:5
33+
34+
解释:
35+
高度:[5,1,2,3,4]
36+
预期:[1,2,3,4,5]
37+
所有下标的对应学生高度都不匹配。
38+
```
39+
示例 3:
40+
```
41+
输入:heights = [1,2,3,4,5]
42+
43+
输出:0
44+
45+
解释:
46+
高度:[1,2,3,4,5]
47+
预期:[1,2,3,4,5]
48+
所有下标的对应学生高度都匹配。
49+
```
50+
51+
提示:
52+
* $1 <= heights.length <= 100$
53+
* $1 <= heights[i] <= 100$
54+
55+
---
56+
57+
### 排序
58+
59+
先排序得到目标序列,再将目标序列和原序列进行逐一对比,得到答案。
60+
61+
排序部分,可以使用复杂度为 $O(n\log{n})$ 的双轴快排,也可以利用数据范围为 $100$,利用计数排序思想来构建目标序列,复杂度为 $O(C + n)$。
62+
63+
代码(快排 $P1$,计数排序 $P2$):
64+
```Java
65+
class Solution {
66+
public int heightChecker(int[] heights) {
67+
int[] t = heights.clone();
68+
Arrays.sort(t);
69+
int n = heights.length, ans = 0;
70+
for (int i = 0; i < n; i++) {
71+
if (t[i] != heights[i]) ans++;
72+
}
73+
return ans;
74+
}
75+
}
76+
```
77+
78+
-
79+
80+
```Java
81+
class Solution {
82+
public int heightChecker(int[] heights) {
83+
int[] cnts = new int[110];
84+
for (int i : heights) cnts[i]++;
85+
int n = heights.length, ans = 0;
86+
int[] t = new int[n];
87+
for (int i = 0, j = 0; i < 110; i++) {
88+
while (cnts[i]-- > 0) t[j++] = i;
89+
}
90+
for (int i = 0; i < n; i++) {
91+
if (t[i] != heights[i]) ans++;
92+
}
93+
return ans;
94+
}
95+
}
96+
```
97+
* 时间复杂度:排序复杂度为 $O(n\log{n})$(快排)或者为 $O(C + n)$(计数排序),统计答案复杂度为 $O(n)$。整体复杂度为 $O(n\log{n})$(快排)或 $O(C + n)$(计数排序)
98+
* 空间复杂度:$O(\log{n})$
99+
100+
---
101+
102+
### 最后
103+
104+
这是我们「刷穿 LeetCode」系列文章的第 `No.1051` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
105+
106+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
107+
108+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
109+
110+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
111+
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1737. 满足三条件之一需改变的最少字符数](https://leetcode.cn/problems/change-minimum-characters-to-satisfy-one-of-three-conditions/solution/by-ac_oier-vs5u/)** ,难度为 **中等**
4+
5+
Tag : 「枚举」、「计数」、「模拟」
6+
7+
8+
9+
给你两个字符串 `a``b` ,二者均由小写字母组成。
10+
11+
一步操作中,你可以将 `a``b` 中的 任一字符 改变为 任一小写字母 。
12+
13+
操作的最终目标是满足下列三个条件 之一 :
14+
* `a` 中的 每个字母 在字母表中 **严格小于** `b` 中的 每个字母 。
15+
* `b` 中的 每个字母 在字母表中 **严格小于** `a` 中的 每个字母 。
16+
* `a``b` 都 由 同一个 字母组成。
17+
18+
返回达成目标所需的 最少 操作数。
19+
20+
示例 1:
21+
``` 
22+
输入:a = "aba", b = "caa"
23+
24+
输出:2
25+
26+
解释:满足每个条件的最佳方案分别是:
27+
1) 将 b 变为 "ccc",2 次操作,满足 a 中的每个字母都小于 b 中的每个字母;
28+
2) 将 a 变为 "bbb" 并将 b 变为 "aaa",3 次操作,满足 b 中的每个字母都小于 a 中的每个字母;
29+
3) 将 a 变为 "aaa" 并将 b 变为 "aaa",2 次操作,满足 a 和 b 由同一个字母组成。
30+
最佳的方案只需要 2 次操作(满足条件 1 或者条件 3)。
31+
```
32+
示例 2:
33+
``` 
34+
输入:a = "dabadd", b = "cda"
35+
36+
输出:3
37+
38+
解释:满足条件 1 的最佳方案是将 b 变为 "eee" 。
39+
```
40+
41+
提示:
42+
* $1 <= a.length, b.length <= 10^5$
43+
* `a``b` 只由小写字母组成
44+
45+
---
46+
47+
### 计数 + 枚举
48+
49+
使用 `c1``c2` 对字符串 `a``b` 分别进行词频统计,记字符串 `a``b` 的长度为 $n$ 和 $m$。
50+
51+
然后枚举字符 $i$,分别对三种情况的修改次数进行统计:
52+
53+
1. 对应条件 $1$:目的是要将字符串 `a` 中所有的字符变得「严格小于」字符 $i$,将字符串 `b` 中的所有字符变成「不小于/大于等于」字符 $i$。
54+
这可以分别统计 `a` 中大小满足「大于等于」字符 $i$ 的字符数量,以及 `b` 中大小满足「小于」字符 $i$ 数量,两者之和即是满足该条件的最小修改次数。
55+
注意,当 $i = 0$(含义为枚举到小写字母 $a$)时,需要跳过,因为不存在值大小「严格小于」字母 $a$ 的字符,即无法做到将某个字符串替换成所有字符都「严格小于」字母 $a$;
56+
2. 对应条件 $2$:与条件 $1$ 同理;
57+
3. 对应条件 $3$:如果要将两字符的所有字符都变成 $i$,其中字符串 `a` 要修改的字符数为 $ca = n - c1[i]$,字符串 `b` 要修改的字符数为 $cb = m - c2[i]$,总修改次数为 $ca + cb$。
58+
59+
枚举完所有的字符 $i$ 后,统计到的所有修改次数的最小值即是答案。
60+
61+
代码:
62+
```Java
63+
class Solution {
64+
public int minCharacters(String a, String b) {
65+
int n = a.length(), m = b.length(), ans = 0x3f3f3f3f;
66+
int[] c1 = new int[26], c2 = new int[26];
67+
for (char c : a.toCharArray()) c1[c - 'a']++;
68+
for (char c : b.toCharArray()) c2[c - 'a']++;
69+
for (int i = 0; i < 26 && ans != 0; i++) {
70+
// 3
71+
int ca = n - c1[i], cb = m - c2[i];
72+
ans = Math.min(ans, ca + cb);
73+
if (i == 0) continue;
74+
int r1 = 0, r2 = 0;
75+
// 1
76+
for (int j = i; j < 26; j++) r1 += c1[j];
77+
for (int j = 0; j < i; j++) r1 += c2[j];
78+
// 2
79+
for (int j = i; j < 26; j++) r2 += c2[j];
80+
for (int j = 0; j < i; j++) r2 += c1[j];
81+
ans = Math.min(ans, Math.min(r1, r2));
82+
}
83+
return ans;
84+
}
85+
}
86+
```
87+
* 时间复杂度:统计词频的复杂度为 $O(n + m)$,统计答案的复杂度为 $O(C^2)$,其中 $C = 26$ 为字符集大小
88+
* 空间复杂度:$O(C)$
89+
90+
---
91+
92+
### 最后
93+
94+
这是我们「刷穿 LeetCode」系列文章的第 `No.1737` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
95+
96+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
97+
98+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
99+
100+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
101+

0 commit comments

Comments
 (0)