[虚拟机OA]Maximal Square 最大正方形

Given a 2D binary matrix filled with 0‘s and 1‘s,
find the largest square containing only 1‘s and return its area.

Input:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

Output: 4

(参考LeetCode 221 Maximal Square)

思路:

dp[i][j] 代表在以i, j 这个点为右下角的正方形变成
若这一格的值是1, 则这个正方形的边长就是它的上面,左手边
和斜上的值的最小边长 + 1
因为如果有一边短了缺了
都构成不了正方形

代码:

 1  public int maximalSquare(char[][] matrix) {
 2         // corner case
 3         if (matrix == null || matrix.length == 0) return 0;
 4         int m = matrix.length;
 5         int n = matrix[0].length;
 6         int[][] dp = new int[m + 1][n + 1];
 7
 8         int res = 0;
 9         for (int i = 1; i <= m; i++) {
10             for (int j = 1; j <= n; j++) {
11                 if (matrix[i - 1][j - 1] == ‘1‘) {
12                     dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
13                     res = Math.max(res, dp[i][j]);
14                 }
15             }
16         }
17         return res * res;
18     }

原文地址:https://www.cnblogs.com/liuliu5151/p/11508097.html

时间: 2024-08-10 23:15:51

[虚拟机OA]Maximal Square 最大正方形的相关文章

[LintCode] 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. Have you met this question in a real interview? Example 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

[LeetCode] 221. Maximal Square 最大正方形

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 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 addin

lintcode 中等题:Maximal Square 最大子正方形

题目: 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. 解题: 给定一个二维01矩阵,从中找出最大的全

leetcode每日解题思路 221 Maximal Square

问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到, 这道题目的标签还是DP, 那么问题的关键就是要找到一个符合判断是否为正方形的递推式. 老套路, 先看基本型, 对于一个2*2的正方形,对于右下角的元素(1,1)而言, 他的上(0,1), 左(1,0), 左上(0,0)三个元素应该都是'1', 如此才能够组成一个合规的正方形: 那么如果是一个3*

【动态规划】leetcode - Maximal Square

称号: 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. 分析: 利用动态规划求解.建立一个类node.nod

bzoj1661[Usaco2006 Nov]Big Square 巨大正方形*

bzoj1661[Usaco2006 Nov]Big Square 巨大正方形 题意: n*n的图中有一些J点,一些B点和一些空白点,问在空白点添加一个J点所能得到的有4个J点组成最大正方形面积.n≤100. 题解: 枚举两个点,然后根据这两个点组成的边尝试在4个上下两个方向组成四边形. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <queue>

[LintCode] 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. Related Problems Maximal Square II Maximum Su

计算形状Shape(圆Circle,矩形Square ,正方形Rectangle)的面积、周长

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _计算形状 { class Program { static void Main(string[] args) { //作业:计算形状Shape(圆Circle,矩形Square ,正方形Rectangle)的面积.周长 Shape shape =

leetcode_221题——Maximal Square (动态规划)

Maximal Square Total Accepted: 6373 Total Submissions: 31927My Submissions Question Solution 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