[Swift]LeetCode782. 變爲棋盤 | Transform to Chessboard

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-cebepgeo-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

An N x N board contains only 0s and 1s. In each move, you can swap any 2 rows with each other, or any 2 columns with each other.git

What is the minimum number of moves to transform the board into a "chessboard" - a board where no 0s and no 1s are 4-directionally adjacent? If the task is impossible, return -1.github

Examples:
Input: board = [[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]]
Output: 2
Explanation:
One potential sequence of moves is shown below, from left to right:

0110     1010     1010
0110 --> 1010 --> 0101
1001     0101     1010
1001     0101     0101

The first move swaps the first and second column.
The second move swaps the second and third row.

Input: board = [[0, 1], [1, 0]]
Output: 0
Explanation:
Also note that the board with 0 in the top left corner,
01
10

is also a valid chessboard.

Input: board = [[1, 0], [1, 0]]
Output: -1
Explanation:
No matter what sequence of moves you make, you cannot end with a valid chessboard.

Note:微信

  • board will have the same number of rows and columns, a number in the range [2, 30].
  • board[i][j] will be only 0s or 1s.

一個 N x N的 board 僅由 0 和 1 組成 。每次移動,你能任意交換兩列或是兩行的位置。spa

輸出將這個矩陣變爲 「棋盤」 所需的最小移動次數。「棋盤」 是指任意一格的上下左右四個方向的值均與自己不一樣的矩陣。若是不存在可行的變換,輸出 -1。code

示例:
輸入: board = [[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]]
輸出: 2
解釋:
一種可行的變換方式以下,從左到右:

0110     1010     1010
0110 --> 1010 --> 0101
1001     0101     1010
1001     0101     0101

第一次移動交換了第一列和第二列。
第二次移動交換了第二行和第三行。

輸入: board = [[0, 1], [1, 0]]
輸出: 0
解釋:
注意左上角的格值爲0時也是合法的棋盤,如:

01
10

也是合法的棋盤.

輸入: board = [[1, 0], [1, 0]]
輸出: -1
解釋:
任意的變換都不能使這個輸入變爲合法的棋盤。 

提示:orm

  • board 是方陣,且行列數的範圍是[2, 30]
  • board[i][j] 將只包含 0或 1

Runtime: 52 ms
Memory Usage: 18.7 MB
 1 class Solution {
 2     func movesToChessboard(_ board: [[Int]]) -> Int {
 3         var n:Int = board.count
 4         var rowSum:Int = 0
 5         var colSum:Int = 0
 6         var rowDiff:Int = 0
 7         var colDiff:Int = 0
 8         for i in 0..<n
 9         {
10             for j in 0..<n
11             {
12                 if board[0][0] ^ board[i][0] ^ board[0][j] ^ board[i][j] != 0
13                 {
14                     return -1
15                 }
16             }
17         }
18         for i in 0..<n
19         {
20             rowSum += board[0][i]
21             colSum += board[i][0]
22             rowDiff += (board[i][0] == i % 2 ? 1 : 0)
23             colDiff += (board[0][i] == i % 2 ? 1 : 0)
24         }
25         if n / 2 > rowSum || rowSum > (n + 1) / 2 {return -1}
26         if n / 2 > colSum || colSum > (n + 1) / 2 {return -1}
27         if n % 2 != 0
28         {
29             if rowDiff % 2 != 0 {rowDiff = n - rowDiff}
30             if colDiff % 2 != 0 {colDiff = n - colDiff}
31         }
32         else
33         {
34             rowDiff = min(n - rowDiff, rowDiff)
35             colDiff = min(n - colDiff, colDiff)
36         }
37         return (rowDiff + colDiff) / 2
38     }
39 }
相關文章
相關標籤/搜索