-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmaximize_the_confusion_of_an_exam.go
87 lines (77 loc) · 1.4 KB
/
maximize_the_confusion_of_an_exam.go
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
package main
type Node struct {
value int
next *Node
}
func NewNode(value int) *Node {
return &Node{
value: value,
next: nil,
}
}
type LinkedList struct {
head *Node
tail *Node
}
func NewLinkedList() *LinkedList {
return &LinkedList{
head: nil,
tail: nil,
}
}
func (list *LinkedList) Add(value int) {
newNode := NewNode(value)
if list.head == nil {
list.head = newNode
list.tail = newNode
} else {
list.tail.next = newNode
list.tail = newNode
}
}
func (list *LinkedList) RemoveFirst() int {
if list.head == nil {
return -1
}
value := list.head.value
list.head = list.head.next
if list.head == nil {
list.tail = nil
}
return value
}
func (list *LinkedList) IsEmpty() bool {
return list.head == nil
}
func maxConsecutiveAnswers(answerKey string, k int) int {
return max(flipper(answerKey, k, 'F'), flipper(answerKey, k, 'T'))
}
func flipper(answerKey string, k int, countLetter rune) int {
maximum := 0
count := 0
linkedList := NewLinkedList()
for i, letter := range answerKey {
if letter == countLetter {
count++
} else if k > 0 {
linkedList.Add(i)
k--
count++
} else {
linkedList.Add(i)
maximum = max(count, maximum)
firstEncountered := linkedList.RemoveFirst()
if firstEncountered != -1 {
count = i - firstEncountered
}
}
}
maximum = max(count, maximum)
return maximum
}
func max(a, b int) int {
if a > b {
return a
}
return b
}