mirror of
https://github.com/TheAlgorithms/C.git
synced 2026-07-21 08:25:10 +00:00
* 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>
34 lines
962 B
C
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));
|
|
}
|