Files
C/leetcode/src/2024.c
Alexander Pantyukhin bb11a35227 feat: add Maximize the Confusion LeetCode problem (#1150)
* add Maximize the Confusion of an Exam leetcode

* Update leetcode/src/2024.c

Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>

* Update 2024.c

fix build

Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>
2022-12-01 22:55:36 -06:00

34 lines
962 B
C

#define max(X, Y) ((X) > (Y) ? (X) : (Y))
int maximizeTarget(char * answerKey, char targetChar, int k){
int leftIndex = -1;
int result = 0;
int currTargetChars = 0;
int lenAnswerKey = strlen(answerKey);
for (int rightIndex = 0; rightIndex < lenAnswerKey; rightIndex++){
char ch = answerKey[rightIndex];
if (ch == targetChar){
currTargetChars++;
}
while (rightIndex - leftIndex > currTargetChars + k) {
leftIndex++;
if (answerKey[leftIndex] == targetChar){
currTargetChars--;
}
}
result = max(result, rightIndex - leftIndex);
}
return result;
}
// Use sliding window approach + two pointers.
// Runtime: O(n)
// Space: O(1)
int maxConsecutiveAnswers(char * answerKey, int k){
return max(maximizeTarget(answerKey, 'T', k), maximizeTarget(answerKey, 'F', k));
}