source: https://leetcode.com/problems/maximize-the-confusion-of-an-exam/description/
Table of Contents
Maximize the Confusion of an Exam
Description
A teacher is writing a test with n true/false questions, with ‘T’ denoting true and ‘F’ denoting false. He wants to confuse the students by maximizing the number of consecutive questions with the same answer (multiple trues or multiple falses in a row).
You are given a string answerKey, where answerKey[i] is the original answer to the ith question. In addition, you are given an integer k, the maximum number of times you may perform the following operation:
Change the answer key for any question to ‘T’ or ‘F’ (i.e., set answerKey[i] to ‘T’ or ‘F’).
Return the maximum number of consecutive ‘T’s or ‘F’s in the answer key after performing the operation at most k times.
Example 1:
Input: answerKey = “TTFF”, k = 2
Output: 4
Explanation: We can replace both the ‘F’s with ‘T’s to make answerKey = “TTTT”.
There are four consecutive ‘T’s.
Example 2:
Input: answerKey = “TFFT”, k = 1
Output: 3
Explanation: We can replace the first ‘T’ with an ‘F’ to make answerKey = “FFFT”.
Alternatively, we can replace the second ‘T’ with an ‘F’ to make answerKey = “TFFF”.
In both cases, there are three consecutive ‘F’s.
Example 3:
Input: answerKey = “TTFTTFTT”, k = 1
Output: 5
Explanation: We can replace the first ‘F’ to make answerKey = “TTTTTFTT”
Alternatively, we can replace the second ‘F’ to make answerKey = “TTFTTTTT”.
In both cases, there are five consecutive ‘T’s.
Constraints:
- n == answerKey.length
- 1 <= n <= 5 * 104
- answerKey[i] is either ‘T’ or ‘F’
- 1 <= k <= n
Solution
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
class Solution { public int maxConsecutiveAnswers(String answerKey, int k) { int n = answerKey.length(); int[] t = new int[n+1]; int[] f = new int[n+1]; char[] a = answerKey.toCharArray(); int countT = 0; int countF = 0; //prefixSum for T and F for(int i=0;i<a.length;i++){ if(a[i]=='T'){ countT++; } else { countF++; } t[i+1] = countT; f[i+1] = countF; } int lo = 1; int hi = n; while(lo<hi){ int mid = lo + (hi-lo)/2; if(isFeasible(a, t, f, k, mid)){ lo = mid+1; } else { hi = mid; } } return isFeasible(a, t, f, k, lo)?lo:lo-1; } private boolean isFeasible(char[] a, int[] t, int[] f, int k, int target){ for(int i=0;i<a.length-target+1;i++){ int left = i; int right = i + target; int countT = t[right] - t[left]; int countF = f[right] - f[left]; int max = Math.max(countT, countF); int diff = Math.abs(target-max); if(diff<=k) return true; } return false; } // TLE // public int maxConsecutiveAnswers(String answerKey, int k) { // int[] ans = new int[answerKey.length()]; // char[] key = answerKey.toCharArray(); // int i=0; // for(char c: key){ // if(c=='T'){ // ans[i] = 1; // } // i++; // } // return dfs(ans, k, 0); // } // private int dfs(int[] answerKey, int k, int index){ // if(index >= answerKey.length){ // return max(answerKey); // } // int max = 0; // if(k>0){ // //either don't change // int temp = answerKey[index]; // answerKey[index] = 1 - answerKey[index]; // max = Math.max(max, dfs(answerKey, k-1, index+1)); // answerKey[index] = temp; // } // //or don't change it // max = Math.max(max, dfs(answerKey, k, index+1)); // return max; // } // private int max(int[] answerKey){ // int count = 1; // int prev = answerKey[0]; // int max = 0; // for(int i=1;i<answerKey.length;i++){ // if(answerKey[i]==prev){ // count++; // } else { // max = Math.max(max, count); // count = 1; // prev = answerKey[i]; // } // } // max = Math.max(max, count); // return max; // } } |