-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmaximal_square.rb
45 lines (39 loc) · 953 Bytes
/
maximal_square.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# https://leetcode.com/problems/maximal-square/
#
# Given a 2D binary matrix filled with 0's and 1's, find the largest square
# containing all 1's and return its area.
#
# For example:
#
# Given the following matrix:
#
# 1 0 1 0 0
# 1 0 1 1 1
# 1 1 1 1 1
# 1 0 0 1 0
#
# Return 4.
#
# Credits:
#
# Special thanks to @Freezen for adding this problem and creating all
# test cases.
# @param {Character[][]} matrix
# @return {Integer}
def maximal_square(matrix)
return 0 if matrix.empty?
m, n = matrix.size, matrix[0].size
dp = Array.new(m) { Array.new(n, 0) }
maxwidth = 0
0.upto(m - 1) do |i|
0.upto(n - 1) do |j|
if i == 0 || j == 0 || matrix[i][j] == '0'
dp[i][j] = matrix[i][j].to_i
else
dp[i][j] = [dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]].min + 1
end
maxwidth = dp[i][j] if maxwidth < dp[i][j]
end
end
maxwidth * maxwidth
end