|
| 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 | + */ |
| 33 | +pub 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 | + |
| 40 | +impl Solution { |
| 41 | + pub fn min_swaps(grid: Vec<Vec<i32>>) -> i32 { |
| 42 | + // Credit: https://leetcode.com/problems/minimum-swaps-to-arrange-a-binary-grid/solutions/3167180/just-a-runnable-solution/ |
| 43 | + let mut zeros = vec![0; grid.len()]; |
| 44 | + for (i, row) in grid.iter().enumerate() { |
| 45 | + zeros[i] = row.iter().rev().take_while(|&&x| x == 0).count() as i32; |
| 46 | + } |
| 47 | + |
| 48 | + let mut swaps = 0; |
| 49 | + |
| 50 | + for i in 0..zeros.len() { |
| 51 | + if zeros[i] < (zeros.len() - i - 1) as i32 { |
| 52 | + let mut j = i + 1; |
| 53 | + while j < zeros.len() && zeros[j] < (zeros.len() - i - 1) as i32 { |
| 54 | + j += 1; |
| 55 | + } |
| 56 | + if j == zeros.len() { |
| 57 | + return -1; |
| 58 | + } |
| 59 | + while j > i { |
| 60 | + zeros.swap(j, j - 1); |
| 61 | + swaps += 1; |
| 62 | + j -= 1; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + swaps |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// submission codes end |
| 72 | + |
| 73 | +#[cfg(test)] |
| 74 | +mod tests { |
| 75 | + use super::*; |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn test_1536_example_1() { |
| 79 | + let grid = vec![vec![0, 0, 1], vec![1, 1, 0], vec![1, 0, 0]]; |
| 80 | + |
| 81 | + let result = 3; |
| 82 | + |
| 83 | + assert_eq!(Solution::min_swaps(grid), result); |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_1536_example_2() { |
| 88 | + let grid = vec![ |
| 89 | + vec![0, 1, 1, 0], |
| 90 | + vec![0, 1, 1, 0], |
| 91 | + vec![0, 1, 1, 0], |
| 92 | + vec![0, 1, 1, 0], |
| 93 | + ]; |
| 94 | + |
| 95 | + let result = -1; |
| 96 | + |
| 97 | + assert_eq!(Solution::min_swaps(grid), result); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn test_1536_example_3() { |
| 102 | + let grid = vec![vec![1, 0, 0], vec![1, 1, 0], vec![1, 1, 1]]; |
| 103 | + |
| 104 | + let result = 0; |
| 105 | + |
| 106 | + assert_eq!(Solution::min_swaps(grid), result); |
| 107 | + } |
| 108 | +} |
0 commit comments