-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path122.go
More file actions
41 lines (31 loc) · 702 Bytes
/
122.go
File metadata and controls
41 lines (31 loc) · 702 Bytes
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
import (
"math"
)
func minimumRecolors(blocks string, k int) int {
numOfBlacks := make([]int, len(blocks) + 1)
for i := 0; i < len(blocks); i++ {
isWhite := 0
if blocks[i] == 'W' {
isWhite = 1
}
numOfBlacks[i + 1] = numOfBlacks[i] + isWhite
}
res := math.MaxInt32
for i := 0; i < len(blocks); i++ {
rightBoundary := i + k
if rightBoundary > len(blocks) {
break
}
res = min(res, numOfBlacks[rightBoundary] - numOfBlacks[i])
}
if res == math.MaxInt32 {
return 0
}
return res
}
func min(a int, b int) int {
if a < b {
return a
}
return b
}