Skip to content

Commit fd4c14f

Browse files
committed
leetcode
1 parent c4ea402 commit fd4c14f

File tree

4 files changed

+469
-0
lines changed

4 files changed

+469
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
type Node struct {
4+
value int
5+
next *Node
6+
}
7+
8+
func NewNode(value int) *Node {
9+
return &Node{
10+
value: value,
11+
next: nil,
12+
}
13+
}
14+
15+
type LinkedList struct {
16+
head *Node
17+
tail *Node
18+
}
19+
20+
func NewLinkedList() *LinkedList {
21+
return &LinkedList{
22+
head: nil,
23+
tail: nil,
24+
}
25+
}
26+
27+
func (list *LinkedList) Add(value int) {
28+
newNode := NewNode(value)
29+
if list.head == nil {
30+
list.head = newNode
31+
list.tail = newNode
32+
} else {
33+
list.tail.next = newNode
34+
list.tail = newNode
35+
}
36+
}
37+
38+
func (list *LinkedList) RemoveFirst() int {
39+
if list.head == nil {
40+
return -1
41+
}
42+
value := list.head.value
43+
list.head = list.head.next
44+
if list.head == nil {
45+
list.tail = nil
46+
}
47+
return value
48+
}
49+
50+
func (list *LinkedList) IsEmpty() bool {
51+
return list.head == nil
52+
}
53+
54+
func maxConsecutiveAnswers(answerKey string, k int) int {
55+
return max(flipper(answerKey, k, 'F'), flipper(answerKey, k, 'T'))
56+
}
57+
58+
func flipper(answerKey string, k int, countLetter rune) int {
59+
maximum := 0
60+
count := 0
61+
linkedList := NewLinkedList()
62+
for i, letter := range answerKey {
63+
if letter == countLetter {
64+
count++
65+
} else if k > 0 {
66+
linkedList.Add(i)
67+
k--
68+
count++
69+
} else {
70+
linkedList.Add(i)
71+
maximum = max(count, maximum)
72+
firstEncountered := linkedList.RemoveFirst()
73+
if firstEncountered != -1 {
74+
count = i - firstEncountered
75+
}
76+
}
77+
}
78+
maximum = max(count, maximum)
79+
return maximum
80+
}
81+
82+
func max(a, b int) int {
83+
if a > b {
84+
return a
85+
}
86+
return b
87+
}

0 commit comments

Comments
 (0)