Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2024.html

2024. Maximize the Confusion of an Exam (Medium)

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

Similar Questions:

Solution 1. Sliding Window

Check out “C++ Maximum Sliding Window Cheatsheet Template!”.

Intuition: Use a sliding window to get the longest substring with at most k 'T' (or 'F').

Algorithm: Implement a function count(c) which gets the longest substring with at most k character c. The answer is max(count('T'), count('F'))

We can use a shrinkable sliding window:

  • // OJ: https://leetcode.com/problems/maximize-the-confusion-of-an-exam/
    // Time: O(N)
    // Space: O(1)
    class Solution {
        int count(string &s, int k, char c) {
            int N = s.size(), cnt = 0, i = 0, ans = 0;
            for (int j = 0; j < N; ++j) {
                cnt += s[j] == c;
                while (cnt > k) cnt -= s[i++] == c; // if there are more than `k` `c` characters, shrink the window until the `cnt` drops back to `k`.
                ans = max(ans, j - i + 1);
            }
            return ans;
        }
    public:
        int maxConsecutiveAnswers(string s, int k) {
            return max(count(s, k, 'T'), count(s, k, 'F'));
        }
    };
    
  • class Solution:
        def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
            def get(c, k):
                l = r = -1
                while r < len(answerKey) - 1:
                    r += 1
                    if answerKey[r] == c:
                        k -= 1
                    if k < 0:
                        l += 1
                        if answerKey[l] == c:
                            k += 1
                return r - l
    
            return max(get('T', k), get('F', k))
    
    ############
    
    # 2024. Maximize the Confusion of an Exam
    # https://leetcode.com/problems/maximize-the-confusion-of-an-exam
    
    class Solution:
        def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
            
            def go(key):
                left = res = count = 0
                
                for right, x in enumerate(answerKey):
                    if x == key:
                        count += 1
                    
                    while count > k:
                        if answerKey[left] == key:
                            count -= 1
                        left += 1
                    
                    res = max(res, right - left + 1)
                
                return res
            
            return max(go('T'), go('F'))
    
    
  • class Solution {
        public int maxConsecutiveAnswers(String answerKey, int k) {
            return Math.max(get('T', k, answerKey), get('F', k, answerKey));
        }
    
        public int get(char c, int k, String answerKey) {
            int l = 0, r = 0;
            while (r < answerKey.length()) {
                if (answerKey.charAt(r++) == c) {
                    --k;
                }
                if (k < 0 && answerKey.charAt(l++) == c) {
                    ++k;
                }
            }
            return r - l;
        }
    }
    
  • func maxConsecutiveAnswers(answerKey string, k int) int {
    	get := func(c byte, k int) int {
    		l, r := -1, -1
    		for r < len(answerKey)-1 {
    			r++
    			if answerKey[r] == c {
    				k--
    			}
    			if k < 0 {
    				l++
    				if answerKey[l] == c {
    					k++
    				}
    			}
    		}
    		return r - l
    	}
    	return max(get('T', k), get('F', k))
    }
    
    func max(a, b int) int {
    	if a > b {
    		return a
    	}
    	return b
    }
    
  • function maxConsecutiveAnswers(answerKey: string, k: number): number {
        const n = answerKey.length;
        const getMaxCount = (target: 'T' | 'F'): number => {
            let l = 0;
            let u = k;
            for (const c of answerKey) {
                if (c !== target) {
                    u--;
                }
                if (u < 0 && answerKey[l++] !== target) {
                    u++;
                }
            }
            return n - l;
        };
        return Math.max(getMaxCount('T'), getMaxCount('F'));
    }
    
    
  • impl Solution {
        pub fn max_consecutive_answers(answer_key: String, k: i32) -> i32 {
            let bs = answer_key.as_bytes();
            let n = bs.len();
            let get_max_count = |target| {
                let mut l = 0;
                let mut u = k;
                for b in bs.iter() {
                    if b != &target {
                        u -= 1;
                    }
                    if u < 0 {
                        if bs[l] != target {
                            u += 1;
                        }
                        l += 1;
                    }
                }
                n - l
            };
            get_max_count(b'T').max(get_max_count(b'F')) as i32
        }
    }
    
    

Or use non-shrinkable sliding window:

  • // OJ: https://leetcode.com/problems/maximize-the-confusion-of-an-exam/
    // Time: O(N)
    // Space: O(1)
    class Solution {
        int count(string &s, int k, char c) {
            int N = s.size(), cnt = 0, i = 0, j = 0;
            for (; j < N; ++j) {
                cnt += s[j] == c;
                if (cnt > k) cnt -= s[i++] == c; // If `cnt > k` we shift the window.
            }
            return j - i;
        }
    public:
        int maxConsecutiveAnswers(string s, int k) {
            return max(count(s, k, 'T'), count(s, k, 'F'));
        }
    };
    
  • class Solution:
        def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
            def get(c, k):
                l = r = -1
                while r < len(answerKey) - 1:
                    r += 1
                    if answerKey[r] == c:
                        k -= 1
                    if k < 0:
                        l += 1
                        if answerKey[l] == c:
                            k += 1
                return r - l
    
            return max(get('T', k), get('F', k))
    
    ############
    
    # 2024. Maximize the Confusion of an Exam
    # https://leetcode.com/problems/maximize-the-confusion-of-an-exam
    
    class Solution:
        def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
            
            def go(key):
                left = res = count = 0
                
                for right, x in enumerate(answerKey):
                    if x == key:
                        count += 1
                    
                    while count > k:
                        if answerKey[left] == key:
                            count -= 1
                        left += 1
                    
                    res = max(res, right - left + 1)
                
                return res
            
            return max(go('T'), go('F'))
    
    
  • class Solution {
        public int maxConsecutiveAnswers(String answerKey, int k) {
            return Math.max(get('T', k, answerKey), get('F', k, answerKey));
        }
    
        public int get(char c, int k, String answerKey) {
            int l = 0, r = 0;
            while (r < answerKey.length()) {
                if (answerKey.charAt(r++) == c) {
                    --k;
                }
                if (k < 0 && answerKey.charAt(l++) == c) {
                    ++k;
                }
            }
            return r - l;
        }
    }
    
  • func maxConsecutiveAnswers(answerKey string, k int) int {
    	get := func(c byte, k int) int {
    		l, r := -1, -1
    		for r < len(answerKey)-1 {
    			r++
    			if answerKey[r] == c {
    				k--
    			}
    			if k < 0 {
    				l++
    				if answerKey[l] == c {
    					k++
    				}
    			}
    		}
    		return r - l
    	}
    	return max(get('T', k), get('F', k))
    }
    
    func max(a, b int) int {
    	if a > b {
    		return a
    	}
    	return b
    }
    
  • function maxConsecutiveAnswers(answerKey: string, k: number): number {
        const n = answerKey.length;
        const getMaxCount = (target: 'T' | 'F'): number => {
            let l = 0;
            let u = k;
            for (const c of answerKey) {
                if (c !== target) {
                    u--;
                }
                if (u < 0 && answerKey[l++] !== target) {
                    u++;
                }
            }
            return n - l;
        };
        return Math.max(getMaxCount('T'), getMaxCount('F'));
    }
    
    
  • impl Solution {
        pub fn max_consecutive_answers(answer_key: String, k: i32) -> i32 {
            let bs = answer_key.as_bytes();
            let n = bs.len();
            let get_max_count = |target| {
                let mut l = 0;
                let mut u = k;
                for b in bs.iter() {
                    if b != &target {
                        u -= 1;
                    }
                    if u < 0 {
                        if bs[l] != target {
                            u += 1;
                        }
                        l += 1;
                    }
                }
                n - l
            };
            get_max_count(b'T').max(get_max_count(b'F')) as i32
        }
    }
    
    

Discuss

https://leetcode.com/problems/maximize-the-confusion-of-an-exam/discuss/1499033/C%2B%2B-Sliding-window

All Problems

All Solutions