Last active
January 3, 2020 18:38
-
-
Save imvladikon/5a4f25edccc7d170040fb9c75dcd59e3 to your computer and use it in GitHub Desktop.
Maximum size square sub-matrix with all values equal 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# B = np.random.randint(-100,100,size=100000) | |
# TODO: kadane's alg. ? | |
B = np.array([[1, 1, 1, 1, 1], | |
[1, 1, 1, 0, 0], | |
[1, 1, 1, 0, 0], | |
[1, 1, 1, 0, 0], | |
[1, 1, 1, 1, 1]]) | |
def largestMatrix(arr): | |
array = np.array(arr) | |
num_rows = array.shape[0] | |
num_cols = array.shape[1] | |
matrix = np.zeros(shape=(num_rows, num_cols), dtype=np.int) | |
matrix[0, :] = array[0, :] | |
matrix[:, 0] = array[:, 0] | |
for row in range(1, num_rows): | |
for col in range(1, num_cols): | |
if array[row, col] == 0: | |
matrix[row, col] = 0 | |
continue | |
diag = matrix[row - 1, col - 1] | |
top = matrix[row - 1, col] | |
left = matrix[row, col - 1] | |
matrix[row, col] = min(diag, min(top, left)) + 1 | |
return matrix.max() | |
print(largestMatrix(B)) | |
#3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment