-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalg3.py
More file actions
24 lines (20 loc) · 928 Bytes
/
alg3.py
File metadata and controls
24 lines (20 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Task 3 - DP for Problem 1 in O(mn)
def alg3(matrix, h):
rows, cols = len(matrix), len(matrix[0])
# Initialize dp array with 0
dp = [[0] * (cols + 1) for _ in range(rows + 1)]
# To store maximum square size
maxSquareSize = 0
# To store bounding indices of the max square
i1,j1,i2,j2 = None, None, None, None
for i in range(1, rows + 1):
for j in range(1, cols + 1):
if matrix[i - 1][j - 1] >= h:
# bellman equation: value of current element = minimum value of top, left, diagonal element + 1
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1
if dp[i][j] >= maxSquareSize:
# keeping track of max size in same loop
maxSquareSize = dp[i][j]
i1,j1 = i-maxSquareSize + 1, j - maxSquareSize + 1
i2,j2 = i,j
return i1, j1, i2, j2