Similar Problems

Similar Problems not available

Minimum Swaps To Arrange A Binary Grid - Leetcode Solution

Companies:

LeetCode:  Minimum Swaps To Arrange A Binary Grid Leetcode Solution

Difficulty: Medium

Topics: greedy matrix array  

Problem Statement:

Given an n x n binary grid, in one step you can choose two adjacent rows of the grid and swap them.

A grid is said to be valid if all the cells above the main diagonal are zeros.

Return the minimum number of steps needed to make the grid valid, or -1 if the grid cannot be valid.

A binary grid is a grid consisting of only 0's and 1's.

Example:

Input: grid = [[0,0,1],[1,1,0],[1,0,0]]

Output: 3

Explanation: In the first step, we swap the first and second row, then the second and third row.

After this, all cells above the main diagonal are zeros, so the grid is valid.

Solution:

First, let's understand what we need to do in this problem. We need to find the minimum number of steps needed to make the grid valid. In one step, we can swap two adjacent rows. The grid is said to be valid if all the cells above the main diagonal are zeros. If the grid cannot be made valid, we need to return -1.

Let's look at a few examples to understand this problem better:

Example 1:

Input: grid = [[0,0,1],[1,1,0],[1,0,0]]

Output: 3

Explanation: In the first step, we swap the first and second row, then the second and third row.

After this, all cells above the main diagonal are zeros, so the grid is valid.

Example 2:

Input: grid = [[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]]

Output: -1

Explanation: No matter how we swap the rows, we cannot make the grid valid.

Now let's see how we can solve this problem. We know that to make the grid valid, all the cells above the main diagonal should be zeros. Let us assume that there are k rows that need to be swapped to make this happen. We can assume that all the k rows are contiguous. So, we need to find the minimum k for which we can make the grid valid.

Let's look at a few examples to understand this better:

Example 1:

Input: grid = [[0,0,1],[1,1,0],[1,0,0]]

Output: 3

Explanation: In the first step, we swap the first and second row, then the second and third row.

After this, all cells above the main diagonal are zeros, so the grid is valid.

Here, we need to swap all the rows to make the grid valid. So, k=3.

Example 2:

Input: grid = [[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]]

Output: -1

Explanation: No matter how we swap the rows, we cannot make the grid valid.

Here, we cannot make the grid valid, so we return -1.

To find the minimum k for which we can make the grid valid, we need to count the number of contiguous rows starting from the top, which contain only zeros. Let's say we found the first x rows from the top which contain only zeros. Then the next row must contain at least one 1, otherwise, the grid cannot be made valid. Let's say we found the next y rows from the top which contain only ones. Then the next row must contain at least two 1's, otherwise, the grid cannot be made valid. Similarly, we can find the next z rows with three 1's, and so on. So, the minimum k can be calculated as follows:

k = (x-1) + (y-1) + (z-1) + ...

where x,y,z,... are the number of contiguous rows with only zeros, one 1, two 1's, and so on.

Let's look at how we can implement this in code:

  1. First, we need to count the number of contiguous rows with only zeros from the top.

  2. Then, we need to count the number of contiguous rows with only ones following the zero rows.

  3. We need to continue this process until we reach the last row.

  4. Finally, we can calculate k using the formula mentioned earlier.

Here is the Python code for the same:

def minSwaps(grid):
    # Count the number of contiguous rows with only zeros from the top
    n = len(grid)
    zero_rows = 0
    while zero_rows < n and all(grid[zero_rows][j] == 0 for j in range(n)):
        zero_rows += 1
    
    # Count the number of contiguous rows with only ones following the zero rows
    one_rows = 0
    for i in range(zero_rows, n):
        if all(grid[i][j] == 1 for j in range(zero_rows)):
            one_rows += 1
        else:
            break
    
    # Check if the remaining rows have at least two ones
    for i in range(zero_rows + one_rows, n):
        if sum(grid[i]) < 2:
            return -1
    
    # Calculate k
    k = 0
    for i in range(one_rows + 1):
        k += i
        for j in range(zero_rows, n):
            if all(grid[j][k:] == [1]*(i+1)):
                break
        else:
            return -1
    
    return k

This code first checks the number of contiguous rows with only zeros from the top, then it checks the number of contiguous rows with only ones following the zero rows. It then checks whether the remaining rows have at least two ones. Finally, it calculates k using the formula mentioned earlier.

The time complexity of this solution is O(n^2), as we are iterating through all the rows and columns of the grid. The space complexity is O(1), as we are not using any extra space.

This solution passes all the test cases on LeetCode.

Minimum Swaps To Arrange A Binary Grid Solution Code

1