Skip to content
Rain Hu's Workspace
Go back

[LeetCode] 2024. Maximize the Confusion of an Exam

Rain Hu
class Solution {
private:
    int maxConsecutiveAnswersWith(string keys, int k, char c) {
        int left = 0, right = 0, res = 0, n = keys.size();
        int cnt = 0;
        while (right < n) {
            char key = keys[right++];
            while (key == c && cnt == k) {
                if (keys[left++] == c) {
                    cnt--;
                }
            } 
            if (key == c) cnt++;
            res = max(res, right-left);
        }
        return res;
    }
public:
    int maxConsecutiveAnswers(string answerKey, int k) {
        return max(maxConsecutiveAnswersWith(answerKey, k, 'T'),
                   maxConsecutiveAnswersWith(answerKey, k, 'F'));
    }
};

Share this post on:

Previous
[LeetCode] 1004. Max Consecutive Ones III
Next
[LeetCode] 20. Valid Parentheses