洞察探索如何利用兼容微信生态的小程序容器,实现跨平台开发,助力金融和车联网行业的数字化转型。
762
2022-10-09
782. Transform to Chessboard
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.
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.
Examples:Input: board = [[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]]Output: 2Explanation:One potential sequence of moves is shown below, from left to right:0110 1010 10100110 --> 1010 --> 01011001 0101 10101001 0101 0101The first move swaps the first and second column.The second move swaps the second and third row.Input: board = [[0, 1], [1, 0]]Output: 0Explanation:Also note that the board with 0 in the top left corner,0110is also a valid chessboard.Input: board = [[1, 0], [1, 0]]Output: -1Explanation:No matter what sequence of moves you make, you cannot end
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.
class Solution public int movesToChessboard(int[][] board) { int N = board.length; // count[code] = v, where code is an integer // that represents the row in binary, and v // is the number of occurrences of that row Map
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~