- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a binary matrix. Here 0 signifies an empty cell, and a 1 signifies a cell with a person. The distance between two cells is the maximum value between the difference in x coordinates and the difference in y coordinates. Now matrix is considered safe with a factor k if there is an empty square such that the distance from the cell to each person in the matrix, and each side of the matrix is all greater or equal to k. We have to find the maximum value of factor k for which we can be safe.

So, if the input is like

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 0 |

0 | 1 | 1 | 1 | 0 |

0 | 1 | 1 | 1 | 0 |

0 | 0 | 0 | 0 | 0 |

then the output will be 1, as in the middle cell the distance from the cell to each person in the grid is at least 1.

To solve this, we will follow these steps −

N := size of A

M := size of A[0]

for i in range 0 to N, do

for j in range 0 to M, do

A[i, j] := A[i, j] XOR 1

ans := 0

for i in range 0 to N, do

for j in range 0 to M, do

if i and j are non-zero and A[i, j] is 1, then

A[i, j] := 1 + minimum of A[i - 1, j], A[i, j - 1] and A[i - 1, j - 1]

ans = maximum of A[i, j] and ans

return (ans + 1) / 2

Let us see the following implementation to get better understanding −

class Solution: def solve(self, A): N = len(A) M = len(A[0]) for i in range(N): for j in range(M): A[i][j] ^= 1 ans = 0 for i in range(N): for j in range(M): if i and j and A[i][j]: A[i][j] = 1 + min(A[i - 1][j], A[i][j - 1], A[i - 1][j - 1]) ans = max(A[i][j], ans) return (ans + 1) // 2 ob = Solution() matrix = [ [0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0], ] print(ob.solve(matrix))

[ [0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0], ]

1

- Related Questions & Answers
- Program to find maximum profit we can make after k Buy and Sell in python
- Program to find maximum number of coins we can collect in Python
- Program to find minimum possible maximum value after k operations in python
- Program to find maximum number of coins we can get using Python
- Program to find how many ways we can climb stairs (maximum steps at most k times) in Python
- Program to find maximum value we can get in knapsack problem by taking multiple copies in Python
- Program to find maximum number of boxes we can fit inside another boxes in python
- Program to check whether we can stand at least k distance away from the closest contacts in Python
- Program to find maximum time to finish K tasks in Python
- Program to find maximum how many water bottles we can drink in Python
- Program to find maximum possible value of smallest group in Python
- Program to find maximum k-repeating substring from sequence in Python
- Program to find maximum number of courses we can take based on interval time in Python?
- Program to find maximum amount of coin we can collect from a given matrix in Python
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree in Python

Advertisements