滑动窗口,LeetCode 2024. 考试的最大困扰度

一、题目

1、题目描述

一位老师正在出一场由 n 道判断题构成的考试,每道题的答案为 true (用 'T' 表示)或者 false (用 'F' 表示)。老师想增加学生对自己做出答案的不确定性,方法是 最大化 有 连续相同 结果的题数。(也就是连续出现 true 或者连续出现 false)。

给你一个字符串 answerKey ,其中 answerKey[i] 是第 i 个问题的正确结果。除此以外,还给你一个整数 k ,表示你能进行以下操作的最多次数:

  • 每次操作中,将问题的正确答案改为 'T' 或者 'F' (也就是将 answerKey[i] 改为 'T' 或者 'F' )。

请你返回在不超过 k 次操作的情况下,最大 连续 'T' 或者 'F' 的数目。

2、接口描述

python3
 ​
class Solution:
    def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
cpp
 ​
class Solution {
public:
    int maxConsecutiveAnswers(string answerKey, int k) {
        
    }
};
C#
 ​
public class Solution {
    public int MaxConsecutiveAnswers(string answerKey, int k) {

    }
}

3、原题链接

2024. 考试的最大困扰度


二、解题报告

1、思路分析

考虑一个窗口是否可以变为连续?

只要窗口内的T或F的个数小于等于k即可

那么我们维护一个变长滑窗,保证滑窗内 T 或 F的个数小于等于k

滑窗的最大长度就是答案

2、复杂度

时间复杂度: O(N)空间复杂度:O(N)

3、代码详解

python3
 ​
class Solution:
    def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
        cnt = [0] * 2
        answerKey = list(map(ord, answerKey))
        res = l = 0
        for r, x in enumerate(answerKey):
            cnt[x >> 1 & 1] += 1
            while cnt[0] > k and cnt[1] > k:
                cnt[answerKey[l] >> 1 & 1] -= 1
                l += 1
            res = max(res, r - l + 1)
        return res
cpp
 ​
class Solution {
public:
    int maxConsecutiveAnswers(string answerKey, int k) {
        int n = answerKey.size();
        std::vector<int> a(n);
        for (int i = 0; i < n; ++ i) {
            a[i] = answerKey[i] == 'T' ? 1 : 0;
        }
        int res = 0, l = 0;
        int cnt[2]{0};
        for (int i = 0; i < n; ++ i) {
            cnt[a[i]] ++;
            while (cnt[0] > k && cnt[1] > k)
                cnt[a[l ++]] --;
            res = std::max(res, i - l + 1);
        }
        return res;
    }
};
C#
 ​
public class Solution {
    public int MaxConsecutiveAnswers(string answerKey, int k) {
        int n = answerKey.Length;
        int[] a = new int[n];
        for (int i = 0; i < n; ++ i) {
            a[i] = answerKey[i] == 'T' ? 1 : 0;
        }
        int res = 0, l = 0;
        int[] cnt = new int[2];
        for (int i = 0; i < n; ++ i) {
            cnt[a[i]] ++;
            while (cnt[0] > k && cnt[1] > k)
                cnt[a[l ++]] --;
            res = Math.Max(res, i - l + 1);
        }
        return res;
    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
滑动窗口是一种常用的算法技巧,可以用于解决一类问题,其中包括一些LeetCode上的题目。通过维护一个窗口,我们可以在线性时间内解决一些需要处理连续子数组或子字符串的问题。以下是一些常见的滑动窗口问题: 1. 最小覆盖子串(Minimum Window Substring):给定一个字符串S和一个字符串T,在S中找出包含T所有字符的最小子串。 2. 字符串的排列(Permutation in String):给定两个字符串s1和s2,判断s2是否包含s1的排列。 3. 找到字符串中所有字母异位词(Find All Anagrams in a String):给定一个字符串s和一个非空字符串p,找到s中所有是p的字母异位词的子串。 4. 替换后的最长重复字符(Longest Repeating Character Replacement):给定一个只包含大写英文字母的字符串s,你可以将一个字母替换成任意其他字母,使得包含重复字母的最长子串的长最大化。 5. 至多包含两个不同字符的最长子串(Longest Substring with At Most Two Distinct Characters):给定一个字符串s,找出至多包含两个不同字符的最长子串的长。 以上只是几个例子,滑动窗口可以应用于更多类型的问题。在解决这些问题时,我们通常使用两个指针来表示窗口的左右边界,并根据具体问题的要求移动窗口。在每次移动窗口时,我们可以更新窗口的状态,例如统计字符出现次数、判断窗口是否满足条件等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EQUINOX1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值