Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2023-07-08 01:55:46
https://leetcode.com/problems/maximize-the-confusion-of-an-exam/description/
2024. Maximize the Confusion of an Exam
一个教师正在编写包含 n 个是非题的试卷,T 表示真 F 表示假。
他想透过最大化具有相同答案的连续问题数量(多个连续T或F)来迷惑学生。给你一
个字串 answerKey 表示第 i 个问题的原始答案,此外再给你一个整数 k 表示可以执行
以下操作的次数:
* 将任何问题的答案更改为 T 或 F
执行最多 k 次操作后,返回答案中连续 T 或 F 的最大数量。
Example 1:
Input: answerKey = "TTFF", k = 2
Output: 4
Explanation: We can replace both the 'F's with 'T's to make answerKey =
"TTTT".
There are four consecutive 'T's.
Example 2:
Input: answerKey = "TFFT", k = 1
Output: 3
Explanation: We can replace the first 'T' with an 'F' to make answerKey =
"FFFT".
Alternatively, we can replace the second 'T' with an 'F' to make answerKey =
"TFFF".
In both cases, there are three consecutive 'F's.
Example 3:
Input: answerKey = "TTFTTFTT", k = 1
Output: 5
Explanation: We can replace the first 'F' to make answerKey = "TTTTTFTT"
Alternatively, we can replace the second 'F' to make answerKey = "TTFTTTTT".
In both cases, there are five consecutive 'T's.
思路:
1.找最长连续子序列我先想到滑动窗口,先找全为T最长可以多长, 再来找全为F的,简
单暴力。
2.过程就是还有 k 可以用或是下一个字符是目标字符的时候就一直把字符 push 进窗口
,否则一直 pop 最左元素直到窗口内元素合法。
Java Code:
作者: a1234555 (肉宝宝)   2023-07-08 01:57:00
大师
作者: Che31128 (justjoke)   2023-07-08 01:57:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com