-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (43 loc) · 839 Bytes
/
main.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
package main
import (
"fmt"
"math"
"strconv"
"strings"
)
func operationRequired(strs []string) int {
rows, cols := len(strs), len(strs[0])
result := 0
colCounts := make([]int, cols)
for _, s := range strs {
for j := 0; j < cols-1; j++ {
if s[j] != s[j+1] {
colCounts[j]++
}
}
if s[cols-1] == '1' {
colCounts[cols-1]++
}
}
for _, count := range colCounts {
result += int(math.Min(float64(count), float64(rows-count)))
}
return result
}
func main() {
var t int
var results []string
fmt.Scanln(&t)
for i := 0; i < t; i++ {
var n, m int
var strs []string
fmt.Scanln(&n, &m)
for j := 0; j < n; j++ {
var str string
fmt.Scanln(&str)
strs = append(strs, str)
}
results = append(results, strconv.Itoa(operationRequired(strs)))
}
fmt.Println(strings.Join(results, "\n"))
}