Skip to content

Commit 2481c33

Browse files
committed
MaximalSquare
1 parent b404961 commit 2481c33

5 files changed

+103
-1
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ Keep updating.
1818

1919
## Hard
2020
- Kaprekars Constant
21-
- Chessboard Traveling
21+
- Chessboard Traveling
22+
- MaximalSquare
23+
I think this one is very hard when it comes to matrix and filter, and how to handle filter movement within a matrix by pure code without using some matrix handling library.
24+
Since coderbyte uses python2, and my own laptop uses python3, I have 2 version of the code.
25+
For python2 version, you can just copy and paste the code on to the [coderbyte online editor](https://www.coderbyte.com/information/Maximal%20Square) and test the code.
26+
For python3, you can run it on your own computer. You need to copy and paste the test case on strArr, for example:
27+
'''
28+
strArr = ["0111", "1111", "1111", "1111"]
29+
'''
30+
Have fun!
File renamed without changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Challenge (hard)
3+
4+
Have the function MaximalSquare(strArr) take the strArr parameter being passed which will be a 2D matrix of 0 and 1's, and determine the area of the largest square submatrix that contains all 1's. A square submatrix is one of equal width and height, and your program should return the area of the largest submatrix that contains only 1's. For example: if strArr is ["10100", "10111", "11111", "10010"] then this looks like the following matrix:
5+
6+
1 0 1 0 0
7+
1 0 1 1 1
8+
1 1 1 1 1
9+
1 0 0 1 0
10+
11+
For the input above, you can see the bolded 1's create the largest square submatrix of size 2x2, so your program should return the area which is 4. You can assume the input will not be empty.
12+
13+
Hard challenges are worth 15 points and you are not timed for them.
14+
15+
Sample Test Cases
16+
17+
Input:["0111", "1111", "1111", "1111"]
18+
19+
Output:9
20+
21+
Input:["0111", "1101", "0111"]
22+
23+
Output:1
24+
'''
25+
def MaximalSquare(strArr):
26+
l = list([[int(x) for x in s] for s in strArr])
27+
row = len(strArr)
28+
col = len(strArr[0])
29+
F = min(row, col)
30+
31+
for f in range(F, 0, -1): # descending searching
32+
for i in range(0, row-f+1): # from row to row (filter goes from upper to bottom)
33+
# if f == row, do this for loop only once
34+
# i is the row index of the matrix
35+
for j in range(0, col-f+1): # from column to column (filter goes from left to right)
36+
# if f == col, do this loop only once
37+
# j is the column index of the matrix
38+
# strArr[i][j] is the upper left corner of the submatrix
39+
multiply = 1
40+
41+
# l[m][n] is the elements of the submatrix
42+
for m in range(i, f+i):
43+
for n in range(j, f+j):
44+
multiply *= l[m][n]
45+
## after all the multiply operator:
46+
if multiply == 1:
47+
return f*f
48+
return 0
49+
50+
print MaximalSquare(raw_input())
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on Mon Jun 10 16:54:05 2019
5+
6+
@author: taylorliang
7+
"""
8+
def MaximalSquare(strArr):
9+
l = list([[int(x) for x in s] for s in strArr])
10+
#print(l)
11+
12+
row = len(strArr)
13+
col = len(strArr[0])
14+
F = min(row, col)
15+
#print("row, col = ", row, ', ', col)
16+
#print("original F =", F)
17+
18+
for f in range(F, 0, -1): # descending searching
19+
#print("======================================================\nfilter size = ", f)
20+
for i in range(0, row-f+1): # from row to row (filter goes from upper to bottom)
21+
# if f == row, do this for loop only once
22+
# i is the row index of the matrix
23+
for j in range(0, col-f+1): # from column to column (filter goes from left to right)
24+
# if f == col, do this loop only once
25+
# j is the column index of the matrix
26+
# strArr[i][j] is the upper left corner of the submatrix
27+
#print(f'Top left coordinate of the submatrix is: i, j = {i}, {j}')
28+
multiply = 1
29+
30+
# l[m][n] is the elements of the submatrix
31+
for m in range(i, f+i):
32+
for n in range(j, f+j):
33+
#print(f'm, n = {m}, {n}')
34+
multiply *= l[m][n]
35+
#print(f'multiply = {multiply}')
36+
## after all the multiply operator:
37+
if multiply == 1:
38+
return f*f
39+
return 0
40+
41+
# copy and paste the test case here:
42+
strArr = ["0111", "1111", "1111", "1111"]
43+
print(MaximalSquare(strArr))

0 commit comments

Comments
 (0)