1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// @Title: 考试的最大困扰度 (Maximize the Confusion of an Exam)
// @Author: 15816537946@163.com
// @Date: 2022-03-29 23:33:54
// @Runtime: 8 ms
// @Memory: 5 MB
func maxConsecutiveChar(answerKey string, k int, ch byte) (ans int) {
    left, sum := 0, 0
    for right := range answerKey {
        if answerKey[right] != ch {
            sum++
        }
        for sum > k {
            if answerKey[left] != ch {
                sum--
            }
            left++
        }
        ans = max(ans, right-left+1)
    }
    return
}

func maxConsecutiveAnswers(answerKey string, k int) int {
    return max(maxConsecutiveChar(answerKey, k, 'T'),
               maxConsecutiveChar(answerKey, k, 'F'))
}

func max(a, b int) int {
    if b > a {
        return b
    }
    return a
}