动态规划例子: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.

 1 public class Solution {
 2     public int maximalSquare(char[][] matrix) {
 3         int rows = matrix.length;
 4         if(rows == 0) return 0;
 5         int cols = matrix[0].length;
 6         if(cols == 0) return 0;
 7         Node[][] sta = new Node[rows][cols];
 8         for(int i=0; i<rows; i++){
 9             for(int j=0; j<cols; j++){
10                 sta[i][j] = new Node();
11             }
12         }
13
14         int res = 0;
15
16         if(matrix[0][0] == ‘1‘){
17             sta[0][0].left = 1;
18             sta[0][0].up = 1;
19             sta[0][0].maxSize = 1;
20             res = 1;
21         }
22         //求第一行
23         for(int i=1; i<cols; i++){
24             if(matrix[0][i] == ‘1‘){
25                 sta[0][i].left = sta[0][i-1].left+1;
26                 sta[0][i].maxSize = 1;
27                 res = 1;
28             }
29         }
30         //求第一列
31         for(int j=1;j<rows;j++){
32             if(matrix[j][0] == ‘1‘){
33                 sta[j][0].up = sta[j-1][0].up + 1;
34                 sta[j][0].maxSize = 1;
35                 res = 1;
36             }
37         }
38         //动态求其他
39         for(int i=1; i<rows; i++){
40             for(int j=1; j<cols; j++){
41                 if(matrix[i][j] == ‘1‘){
42                     sta[i][j].left = sta[i-1][j].left + 1;
43                     sta[i][j].up = sta[i][j-1].up + 1;
44                     sta[i][j].maxSize = 1;
45                     if(matrix[i-1][j-1] == ‘1‘){
46                         sta[i][j].maxSize = Math.min(sta[i][j].left, sta[i][j].up);
47                         sta[i][j].maxSize = Math.min(sta[i][j].maxSize, sta[i-1][j-1].maxSize+1);
48                     }
49
50                 }
51                 res = Math.max(sta[i][j].maxSize, res);
52             }
53         }
54
55         return res*res;
56
57     }
58
59     class Node{
60         int left;
61         int up;
62         int maxSize;
63     }
64
65 }
时间: 2024-10-11 13:41:33

动态规划例子:Maximal Square的相关文章

LeetCode之“动态规划”: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. 在GeeksforGeeks有一个解决该问题的方法: 1) Cons

【动态规划】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

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

[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

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矩阵,从中找出最大的全

[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

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*

[LintCode] Maximal Square II

Given a 2D binary matrix filled with 0's and 1's, find the largest square which diagonal is all 1 and others is 0. Only consider the main diagonal situation. Example For example, given the following matrix: 1 0 1 0 0 1 0 0 1 0 1 1 0 0 1 1 0 0 1 0 Ret

【LeetCode】221. 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. Credits:Special thanks to @Fre

[虚拟机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 01 0 1 1 11 1 1 1 11 0 0 1 0 Output: 4 (参考LeetCode 221 Maximal Square) 思路: dp[i][j] 代表在以i, j 这个点为右下角的正方形变成若这一格的值是1, 则这个正方