deno.land / x / masx200_leetcode_test@10.6.5 / transform-to-chessboard / index.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
package index
func movesToChessboard(board [][]int) int { var ( n = len(board) rowSum = 0 colSum = 0 rowDiff = 0 colDiff = 0 ) for i := 0; i < n; i++ { for j := 0; j < n; j++ { if itob((board[0][0]) ^ (board[i][0]) ^ (board[0][j]) ^ (board[i][j])) { return -1 } } } for i := 0; i < n; i++ { rowSum += board[0][i] colSum += board[i][0] rowDiff += Btoi(board[i][0] == i%2) colDiff += Btoi(board[0][i] == i%2) } if n/2 > rowSum || rowSum > (n+1)/2 { return -1 } if n/2 > colSum || colSum > (n+1)/2 { return -1 } if itob(n % 2) { if itob(rowDiff % 2) { rowDiff = n - rowDiff } if itob(colDiff % 2) { colDiff = n - colDiff } } else { rowDiff = min(n-rowDiff, rowDiff) colDiff = min(n-colDiff, colDiff) } return (rowDiff + colDiff) / 2
}func Btoi(b bool) int { if b { return 1 } return 0}func itob(b int) bool { return b != 0}
func min(a, b int) int { if a < b { return a } return b}
masx200_leetcode_test

Version Info

Tagged at
a year ago