-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Description
-
https://leetcode-cn.com/problems/maximize-the-confusion-of-an-exam/
-
思路
- 滑动窗口
- LongestOnes1004 #400
-
刷15mins
-
刷5mins
// time O(n*2)
// space O(1)
public int maxConsecutiveAnswers(String answerKey, int k) {
return Math.max(maxConsecutiveChar(answerKey, k, 'T'),
maxConsecutiveChar(answerKey, k, 'F'));
}
int maxConsecutiveChar(String answerKey, int k, char ch) {
int n = answerKey.length();
int ans = 0;
for (int left = 0, right = 0, sum = 0; right < n; right++) {
sum += answerKey.charAt(right) != ch ? 1 : 0;
while (sum > k) {
sum -= answerKey.charAt(left++) != ch ? 1 : 0;
}
ans = Math.max(ans, right - left + 1);
}
return ans;
}