1536. Minimum Swaps to Arrange a Binary Grid Medium

@problem@discussion
#Array#Greedy#Matrix



1/**
2 * [1536] Minimum Swaps to Arrange a Binary Grid
3 *
4 * Given an n x n binary grid, in one step you can choose two adjacent rows of the grid and swap them.
5 * A grid is said to be valid if all the cells above the main diagonal are zeros.
6 * Return the minimum number of steps needed to make the grid valid, or -1 if the grid cannot be valid.
7 * The main diagonal of a grid is the diagonal that starts at cell (1, 1) and ends at cell (n, n).
8 *  
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2020/07/28/fw.jpg" style="width: 750px; height: 141px;" />
11 * Input: grid = [[0,0,1],[1,1,0],[1,0,0]]
12 * Output: 3
13 * 
14 * Example 2:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2020/07/16/e2.jpg" style="width: 270px; height: 270px;" />
16 * Input: grid = [[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]]
17 * Output: -1
18 * Explanation: All rows are similar, swaps have no effect on the grid.
19 * 
20 * Example 3:
21 * <img alt="" src="https://assets.leetcode.com/uploads/2020/07/16/e3.jpg" style="width: 200px; height: 200px;" />
22 * Input: grid = [[1,0,0],[1,1,0],[1,1,1]]
23 * Output: 0
24 * 
25 *  
26 * Constraints:
27 * 
28 * 	n == grid.length == grid[i].length
29 * 	1 <= n <= 200
30 * 	grid[i][j] is either 0 or 1
31 * 
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/minimum-swaps-to-arrange-a-binary-grid/
36// discuss: https://leetcode.com/problems/minimum-swaps-to-arrange-a-binary-grid/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41    pub fn min_swaps(grid: Vec<Vec<i32>>) -> i32 {
42        0
43    }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_1536() {
54    }
55}
56


Back
© 2025 bowen.ge All Rights Reserved.