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 1b2895a

Browse files
✨feat: Add 2024
1 parent 9d39dd7 commit 1b2895a

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

‎Index/双指针.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@
4343
| [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/) | 简单 | 🤩🤩🤩🤩 |
4444
| [1764. 通过连接另一个数组的子数组得到一个数组](https://leetcode-cn.com/problems/form-array-by-concatenating-subarrays-of-another-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/form-array-by-concatenating-subarrays-of-another-array/solution/clean-solutionni-jue-dui-neng-kan-dong-d-l4ts/) | 中等 | 🤩🤩🤩🤩 |
4545
| [2000. 反转单词前缀](https://leetcode-cn.com/problems/reverse-prefix-of-word/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-prefix-of-word/solution/gong-shui-san-xie-jian-dan-shuang-zhi-zh-dp9u/) | 简单 | 🤩🤩🤩🤩 |
46+
| [2024. 考试的最大困扰度](https://leetcode-cn.com/problems/maximize-the-confusion-of-an-exam/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximize-the-confusion-of-an-exam/solution/by-ac_oier-2rii/) | 中等 | 🤩🤩🤩🤩 |
4647
| [2047. 句子中的有效单词数](https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-5pcz/) | 简单 | 🤩🤩🤩🤩 |
4748

‎Index/滑动窗口.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
| [1610. 可见点的最大数目](https://leetcode-cn.com/problems/maximum-number-of-visible-points/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-visible-points/solution/gong-shui-san-xie-qiu-ji-jiao-ji-he-ti-b-0bid/) | 困难 | 🤩🤩🤩🤩 |
2121
| [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/) | 中等 | 🤩🤩🤩 |
2222
| [1984. 学生分数的最小差值](https://leetcode-cn.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/solution/gong-shui-san-xie-pai-xu-hua-dong-chuang-ru6e/) | 简单 | 🤩🤩🤩🤩🤩 |
23+
| [2024. 考试的最大困扰度](https://leetcode-cn.com/problems/maximize-the-confusion-of-an-exam/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximize-the-confusion-of-an-exam/solution/by-ac_oier-2rii/) | 中等 | 🤩🤩🤩🤩 |
2324

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[2024. 考试的最大困扰度](https://leetcode-cn.com/problems/maximize-the-confusion-of-an-exam/solution/by-ac_oier-2rii/)** ,难度为 **中等**
4+
5+
Tag : 「滑动窗口」、「双指针」
6+
7+
8+
9+
一位老师正在出一场由 $n$ 道判断题构成的考试,每道题的答案为 `true` (用 `'T'` 表示)或者 `false` (用 `'F'` 表示)。老师想增加学生对自己做出答案的不确定性,方法是最大化有连续相同结果的题数。(也就是连续出现 `true` 或者连续出现 `false`)。
10+
11+
给你一个字符串 $answerKey$ ,其中 $answerKey[i]$ 是第 $i$ 个问题的正确结果。除此以外,还给你一个整数 $k,ドル表示你能进行以下操作的最多次数:
12+
13+
每次操作中,将问题的正确答案改为 `'T'` 或者 `'F'` (也就是将 $answerKey[i]$ 改为 `'T'` 或者 `'F'` )。
14+
请你返回在不超过 $k$ 次操作的情况下,最大 连续 `'T'` 或者 `'F'` 的数目。
15+
16+
示例 1:
17+
```
18+
输入:answerKey = "TTFF", k = 2
19+
20+
输出:4
21+
22+
解释:我们可以将两个 'F' 都变为 'T' ,得到 answerKey = "TTTT" 。
23+
总共有四个连续的 'T' 。
24+
```
25+
示例 2:
26+
```
27+
输入:answerKey = "TFFT", k = 1
28+
29+
输出:3
30+
31+
解释:我们可以将最前面的 'T' 换成 'F' ,得到 answerKey = "FFFT" 。
32+
或者,我们可以将第二个 'T' 换成 'F' ,得到 answerKey = "TFFF" 。
33+
两种情况下,都有三个连续的 'F' 。
34+
```
35+
示例 3:
36+
```
37+
输入:answerKey = "TTFTTFTT", k = 1
38+
39+
输出:5
40+
41+
解释:我们可以将第一个 'F' 换成 'T' ,得到 answerKey = "TTTTTFTT" 。
42+
或者我们可以将第二个 'F' 换成 'T' ,得到 answerKey = "TTFTTTTT" 。
43+
两种情况下,都有五个连续的 'T' 。
44+
```
45+
46+
提示:
47+
* $n == answerKey.length$
48+
* 1ドル <= n <= 5 * 104$
49+
* $answerKey[i]$ 要么是 `'T'` ,要么是 `'F'`
50+
* 1ドル <= k <= n$
51+
52+
---
53+
54+
### 滑动窗口
55+
56+
题目求修改次数不超过 $k$ 的前提下,连续段 `'T'``'F'` 的最大长度。
57+
58+
等价于求一个包含 `'F'` 或者 `'T'` 的个数不超过 $k$ 的最大长度窗口。
59+
60+
假定存在一个 `int getCnt(char c)` 函数,返回包含字符 `c` 数量不超过 $k$ 的最大窗口长度,那么最终 `max(getCnt('T'), getCnt('F'))` 即是答案。
61+
62+
其中 `getCnt` 函数的实现可以使用「滑动窗口」:使用 $j$ 和 $i$ 分别代表窗口的左右端点,$cnt$ 为区间 $[j, i]$ 中的字符 `c` 的数量,每次右端点 $i$ 移动时,若满足 $s[i] = c,ドル让 $cnt$ 自增,当 $cnt > k$ 时,使左端点 $j$ 往右移动,同时更新 $cnt,ドル直到 $[j, i]$ 区间恢复合法性(包含字符 `c` 的数量 $cnt$ 不超过 $k$ 个)。
63+
64+
代码:
65+
```Java
66+
class Solution {
67+
String s;
68+
int n, k;
69+
public int maxConsecutiveAnswers(String answerKey, int _k) {
70+
s = answerKey;
71+
n = s.length(); k = _k;
72+
return Math.max(getCnt('T'), getCnt('F'));
73+
}
74+
int getCnt(char c) {
75+
int ans = 0;
76+
for (int i = 0, j = 0, cnt = 0; i < n; i++) {
77+
if (s.charAt(i) == c) cnt++;
78+
while (cnt > k) {
79+
if (s.charAt(j) == c) cnt--;
80+
j++;
81+
}
82+
ans = Math.max(ans, i - j + 1);
83+
}
84+
return ans;
85+
}
86+
}
87+
```
88+
* 时间复杂度:$O(n)$
89+
* 空间复杂度:$O(1)$
90+
91+
---
92+
93+
### 最后
94+
95+
这是我们「刷穿 LeetCode」系列文章的第 `No.2024` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
96+
97+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
98+
99+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
100+
101+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
102+

0 commit comments

Comments
(0)

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