|
| 1 | +/* |
| 2 | +
|
| 3 | +-* 2024. Maximize the Confusion of an Exam *- |
| 4 | +
|
| 5 | +
|
| 6 | +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). |
| 7 | +
|
| 8 | +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: |
| 9 | +
|
| 10 | +Change the answer key for any question to 'T' or 'F' (i.e., set answerKey[i] to 'T' or 'F'). |
| 11 | +Return the maximum number of consecutive 'T's or 'F's in the answer key after performing the operation at most k times. |
| 12 | +
|
| 13 | + |
| 14 | +
|
| 15 | +Example 1: |
| 16 | +
|
| 17 | +Input: answerKey = "TTFF", k = 2 |
| 18 | +Output: 4 |
| 19 | +Explanation: We can replace both the 'F's with 'T's to make answerKey = "TTTT". |
| 20 | +There are four consecutive 'T's. |
| 21 | +Example 2: |
| 22 | +
|
| 23 | +Input: answerKey = "TFFT", k = 1 |
| 24 | +Output: 3 |
| 25 | +Explanation: We can replace the first 'T' with an 'F' to make answerKey = "FFFT". |
| 26 | +Alternatively, we can replace the second 'T' with an 'F' to make answerKey = "TFFF". |
| 27 | +In both cases, there are three consecutive 'F's. |
| 28 | +Example 3: |
| 29 | +
|
| 30 | +Input: answerKey = "TTFTTFTT", k = 1 |
| 31 | +Output: 5 |
| 32 | +Explanation: We can replace the first 'F' to make answerKey = "TTTTTFTT" |
| 33 | +Alternatively, we can replace the second 'F' to make answerKey = "TTFTTTTT". |
| 34 | +In both cases, there are five consecutive 'T's. |
| 35 | + |
| 36 | +
|
| 37 | +Constraints: |
| 38 | +
|
| 39 | +n == answerKey.length |
| 40 | +1 <= n <= 5 * 104 |
| 41 | +answerKey[i] is either 'T' or 'F' |
| 42 | +1 <= k <= n |
| 43 | +
|
| 44 | +*/ |
| 45 | + |
| 46 | +import 'dart:collection'; |
| 47 | +import 'dart:math'; |
| 48 | + |
| 49 | +class A { |
| 50 | + int maxConsecutiveAnswers(String answerKey, int k) { |
| 51 | + return max(flipper(answerKey, k, 'F'), flipper(answerKey, k, 'T')); |
| 52 | + } |
| 53 | + |
| 54 | + int flipper(String answerKey, int k, String countLetter) { |
| 55 | + int maximum = 0; |
| 56 | + int count = 0; |
| 57 | + final Queue<int> queue = Queue<int>(); |
| 58 | + for (int i = 0; i < answerKey.length; i++) { |
| 59 | + final String letter = answerKey[i]; |
| 60 | + if (letter == countLetter) |
| 61 | + count++; |
| 62 | + else if (k > 0) { |
| 63 | + queue.add(i); |
| 64 | + k--; |
| 65 | + count++; |
| 66 | + } else { |
| 67 | + queue.add(i); |
| 68 | + maximum = max(count, maximum); |
| 69 | + final int firstEncountered = queue.removeFirst(); |
| 70 | + count = i - firstEncountered; |
| 71 | + } |
| 72 | + } |
| 73 | + maximum = max(count, maximum); |
| 74 | + return maximum; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +//============================== Linked List |
| 79 | +class Node { |
| 80 | + late int value; |
| 81 | + Node? next; |
| 82 | + |
| 83 | + Node(int value) { |
| 84 | + this.value = value; |
| 85 | + this.next = null; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +class LinkedList { |
| 90 | + Node? head; |
| 91 | + Node? tail; |
| 92 | + |
| 93 | + LinkedList() { |
| 94 | + head = null; |
| 95 | + tail = null; |
| 96 | + } |
| 97 | + |
| 98 | + void add(int value) { |
| 99 | + Node newNode = Node(value); |
| 100 | + if (head == null) { |
| 101 | + head = newNode; |
| 102 | + tail = newNode; |
| 103 | + } else { |
| 104 | + tail?.next = newNode; |
| 105 | + tail = newNode; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + int? removeFirst() { |
| 110 | + if (head == null) return null; |
| 111 | + int? value = head?.value; |
| 112 | + head = head?.next; |
| 113 | + if (head == null) tail = null; |
| 114 | + return value; |
| 115 | + } |
| 116 | + |
| 117 | + bool isEmpty() { |
| 118 | + return head == null; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +class B { |
| 123 | + int maxConsecutiveAnswers(String answerKey, int k) { |
| 124 | + return max(flipper(answerKey, k, 'F'), flipper(answerKey, k, 'T')); |
| 125 | + } |
| 126 | + |
| 127 | + int flipper(String answerKey, int k, String countLetter) { |
| 128 | + int maximum = 0; |
| 129 | + int count = 0; |
| 130 | + final LinkedList linkedList = LinkedList(); |
| 131 | + for (int i = 0; i < answerKey.length; i++) { |
| 132 | + final String letter = answerKey[i]; |
| 133 | + if (letter == countLetter) |
| 134 | + count++; |
| 135 | + else if (k > 0) { |
| 136 | + linkedList.add(i); |
| 137 | + k--; |
| 138 | + count++; |
| 139 | + } else { |
| 140 | + linkedList.add(i); |
| 141 | + maximum = max(count, maximum); |
| 142 | + final int? firstEncountered = linkedList.removeFirst(); |
| 143 | + if (firstEncountered != null) { |
| 144 | + count = i - firstEncountered; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + maximum = max(count, maximum); |
| 149 | + return maximum; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +//======================== |
| 154 | + |
| 155 | +class C { |
| 156 | + int maxConsecutiveAnswers(String answerKey, int k) { |
| 157 | + int ans1 = solve(answerKey, k, 'T'); |
| 158 | + int ans2 = solve(answerKey, k, 'F'); |
| 159 | + return max(ans1, ans2); |
| 160 | + } |
| 161 | + |
| 162 | + int solve(String answerKey, int k, String c) { |
| 163 | + int i = 0; |
| 164 | + int j = 0; |
| 165 | + int count = 0; |
| 166 | + int n = answerKey.length; |
| 167 | + int answer = 0; |
| 168 | + while (j < n) { |
| 169 | + if (answerKey[j] == c) count++; |
| 170 | + while (i < n && count > k) { |
| 171 | + if (answerKey[i] == c) count--; |
| 172 | + i++; |
| 173 | + } |
| 174 | + answer = max(j - i + 1, answer); |
| 175 | + j++; |
| 176 | + } |
| 177 | + return answer; |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | + |
| 182 | +//== |
| 183 | + |
0 commit comments