add new solutions
This commit is contained in:
parent
886b5e0a8e
commit
67cad91898
47 changed files with 1549 additions and 1 deletions
39
solutions/23/q2352/solution.go
Normal file
39
solutions/23/q2352/solution.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package q2352
|
||||
|
||||
import "strings"
|
||||
|
||||
func index(grid [][]int, col bool, i int) string {
|
||||
b := strings.Builder{}
|
||||
b.Grow(len(grid) * 3)
|
||||
for j := range grid {
|
||||
var num int
|
||||
switch {
|
||||
case col: // ith col
|
||||
num = grid[j][i]
|
||||
default: // ith row
|
||||
num = grid[i][j]
|
||||
}
|
||||
|
||||
for range 3 {
|
||||
b.WriteByte(byte(num % 256))
|
||||
num >>= 8
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func equalPairs(grid [][]int) int {
|
||||
idx := make(map[string]int, len(grid))
|
||||
nEq := 0
|
||||
for row := range grid {
|
||||
s := index(grid, false, row)
|
||||
idx[s]++
|
||||
}
|
||||
for col := range grid {
|
||||
s := index(grid, true, col)
|
||||
nEq += idx[s]
|
||||
}
|
||||
return nEq
|
||||
}
|
||||
|
||||
var _ = equalPairs
|
||||
Loading…
Add table
Add a link
Reference in a new issue