Program to find maximum value of k for which we can maintain safe distance in Python

Suppose we have a binary matrix where 0 signifies an empty cell, and 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 (Chebyshev distance). A 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.

Example Input

If the input matrix is ?

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 the corner cells are at least distance 1 from all persons in the matrix.

Algorithm Steps

To solve this problem, we use dynamic programming with the following approach ?

  • Get matrix dimensions N (rows) and M (columns)

  • Flip the matrix: convert 0s to 1s and 1s to 0s using XOR operation

  • Apply dynamic programming to find largest square of 1s

  • For each cell (i,j), if it's 1 and not on the border, calculate: A[i,j] = 1 + min(A[i-1,j], A[i,j-1], A[i-1,j-1])

  • Return (maximum_square_size + 1) // 2

Implementation

class Solution:
    def solve(self, A):
        N = len(A)
        M = len(A[0])
        
        # Flip the matrix: 0 -> 1, 1 -> 0
        for i in range(N):
            for j in range(M):
                A[i][j] ^= 1
        
        ans = 0
        
        # Dynamic programming to find largest square
        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

# Test the solution
ob = Solution()
matrix = [
    [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],
]

result = ob.solve(matrix)
print("Maximum safe distance factor k:", result)
Maximum safe distance factor k: 1

How It Works

The algorithm works by ?

  1. Matrix Flipping: We flip 0s and 1s because we want to find the largest square of empty cells

  2. Dynamic Programming: For each cell, we calculate the size of the largest square ending at that position

  3. Square Detection: The DP formula finds squares where A[i][j] represents the side length of the largest square with bottom-right corner at (i,j)

  4. Safe Distance: The safe distance is half the side length of the largest square, rounded up

Time and Space Complexity

  • Time Complexity: O(N × M) where N and M are matrix dimensions

  • Space Complexity: O(1) as we modify the input matrix in-place

Conclusion

This solution uses dynamic programming to find the largest square of empty cells, then calculates the maximum safe distance factor. The key insight is that the safe distance equals half the side length of the largest empty square.

Updated on: 2026-03-25T13:52:05+05:30

218 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements