- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find minimum swaps to arrange a binary grid using Python
Suppose we have a n x n binary matrix. We can perform an operation on it like, at one step we select two adjacent rows and swap them. We have to count number of minimum swaps required, so that all nodes above the major diagonal of the matrix is 0. If there is no such solution, then return -1.
So, if the input is like
0 | 1 | 0 |
0 | 1 | 1 |
1 | 0 | 0 |
then the output will be 2 because −
To solve this, we will follow these steps:
n := row count of matrix
m := make an array of size n and fill with n
for i in range 0 to n - 1, do
for j in range n-1 to 0, decrease by 1, do
if matrix[i, j] is same as 1, then
m[i] := n-j-1
come out from the loop
t := 0, ans := 0
for i in range 0 to n - 1, do
t := t + 1
flag := False
for j in range i to n - 1, do
if m[j] >= n-t, then
ans := ans + j-i
flag := True
come out from loop
if flag is false, then
return -1
update m[from index i+1 to j] by m[from index i to j-1]
return ans
Let us see the following implementation to get better understanding −
Example
def solve(matrix): n = len(matrix) m = [n] * n for i in range(n): for j in range(n-1,-1,-1): if matrix[i][j] == 1: m[i] = n-j-1 break t,ans = 0,0 for i in range(n): t += 1 flag = False for j in range(i,n): if m[j] >= n-t: ans += j-i flag = True break if not flag: return -1 m[i+1:j+1] = m[i:j] return ans matrix = [[0,1,0],[0,1,1],[1,0,0]] print(solve(matrix))
Input
[[0,1,0],[0,1,1],[1,0,0]]
Output
2
- Related Articles
- Program to find minimum number of swaps needed to arrange all pair of socks together in C++
- Program to find minimum swaps required to make given anagram in python
- Program to find minimum swaps needed to group all 1s together in Python
- Program to find minimum adjacent swaps for K consecutive ones in Python
- Minimum swaps required to make a binary string alternating in C++
- Program to find minimum possible integer after at most k adjacent swaps on digits in Python
- Program to count number of minimum swaps required to make it palindrome in Python
- Golang Program to find the minimum and maximum number, using binary operations.
- C++ program to find minimum vertex cover size of a graph using binary search
- Program to find minimum changes required for alternating binary string in Python
- Program to find the minimum cost to arrange the numbers in ascending or descending order in Python
- Program to find minimum insertions to balance a parentheses string using Python
- Program to group the 1s with minimum number of swaps in a given string in Python
- Program to find minimum distance to the target element using Python
- Program to find minimum operations to make array equal using Python
