221. Maximal Square
Medium

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

Example:

Input: 

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0 Output: 4
 
 

To appy DP, we define the state as the maximal size (square = size * size) of the square that can be formed till point (i, j), denoted as dp[i][j].

For the topmost row (i = 0) and the leftmost column (j = 0), we have dp[i][j] = matrix[i][j] - '0', meaning that it can at most form a square of size 1 when the matrix has a '1'in that cell.

When i > 0 and j > 0, if matrix[i][j] = '0', then dp[i][j] = 0 since no square will be able to contain the '0' at that cell. If matrix[i][j] = '1', we will have dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1, which means that the square will be limited by its left, upper and upper-left neighbors.

class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if (matrix.empty()) {
return ;
}
int m = matrix.size(), n = matrix[].size(), sz = ;
vector<vector<int>> dp(m, vector<int>(n, ));
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (!i || !j || matrix[i][j] == '') {
dp[i][j] = matrix[i][j] - '';
} else {
dp[i][j] = min(dp[i - ][j - ], min(dp[i - ][j], dp[i][j - ])) + ;
}
sz = max(dp[i][j], sz);
}
}
return sz * sz;
}
};

In the above code, it uses O(mn) space. Actually each time when we update dp[i][j], we only need dp[i-1][j-1]dp[i-1][j] (the previous row) and dp[i][j-1] (the current row). So we may just keep two rows.

class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if (matrix.empty()) {
return ;
}
int m = matrix.size(), n = matrix[].size(), sz = ;
vector<int> pre(n, ), cur(n, );
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (!i || !j || matrix[i][j] == '') {
cur[j] = matrix[i][j] - '';
} else {
cur[j] = min(pre[j - ], min(pre[j], cur[j - ])) + ;
}
sz = max(cur[j], sz);
}
fill(pre.begin(), pre.end(), );
swap(pre, cur);
}
return sz * sz;
}
};

Furthermore, we may only use just one vector (thanks to @stellari for sharing the idea).

class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if (matrix.empty()) {
return ;
}
int m = matrix.size(), n = matrix[].size(), sz = , pre;
vector<int> cur(n, );
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
int temp = cur[j];
if (!i || !j || matrix[i][j] == '') {
cur[j] = matrix[i][j] - '';
} else {
cur[j] = min(pre, min(cur[j], cur[j - ])) + ;
}
sz = max(cur[j], sz);
pre = temp;
}
}
return sz * sz;
}
};

LeetCode | DP专题详解的更多相关文章

  1. 状压DP入门详解+题目推荐

    在动态规划的题型中,一般叫什么DP就是怎么DP,状压DP也不例外 所谓状态压缩,一般是通过用01串表示状态,充分利用二进制数的特性,简化计算难度.举个例子,在棋盘上摆放棋子的题目中,我们可以用1表示当 ...

  2. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  3. LeetCode专题——详解搜索算法中的搜索策略和剪枝

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题第20篇文章,今天讨论的是数字组合问题. 描述 给定一个int类型的候选集,和一个int类型的target,要求返 ...

  4. Leetcode之广度优先搜索(BFS)专题-详解429. N叉树的层序遍历(N-ary Tree Level Order Traversal)

    Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree Level Order Traversal) 给定一个 N 叉树,返回其节点值的层序遍历. (即从左到右 ...

  5. LeetCode dp专题

    1. 动态规划的适用场景 动态规划常常适用于有重叠子问题和最优子结构性质的问题,动态规划方法所耗时间往往远少于朴素解法. 2. 动态规划的基本思想 动态规划背后的基本思想非常简单.大致上,若要解一个给 ...

  6. 分布式专题——详解Google levelDB底层原理

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是分布式专题的第10篇文章,我们继续来聊聊LSMT这个数据结构. LSMT是一个在分布式系统当中应用非常广泛,并且原理直观简单的数据结构 ...

  7. 【动态规划】树形DP完全详解!

    蒟蒻大佬时隔三个月更新了!!拍手拍手 而且是更新了几篇关于DP的文章(RioTian狂喜) 现在赶紧学习和复习一下树形DP.... 树形DP基础:Here,CF上部分树形DP练习题:Here \[QA ...

  8. poj1417 true liars(并查集 + DP)详解

    这个题做了两天了.首先用并查集分类是明白的, 不过判断是否情况唯一刚开始用的是搜索.总是超时. 后来看别人的结题报告, 才恍然大悟判断唯一得用DP. 题目大意: 一共有p1+p2个人,分成两组,一组p ...

  9. LeetCode Permutations问题详解

    题目一 permutations 题目描述 Given a collection of numbers, return all possible permutations. For example,[ ...

随机推荐

  1. js中Array的sort方法

    Array.sort方法里需要传入一个参数,是一个function, 如果想要升序排序,就传入这样的一个function: function sortFunction(a,b){ return a-b ...

  2. BZOJ 1778: [Usaco2010 Hol]Dotp 驱逐猪猡 概率与期望+高斯消元

    这个还挺友好的,自己相对轻松能想出来~令 $f[i]$ 表示起点到点 $i$ 的期望次数,则 $ans[i]=f[i]\times \frac{p}{q}$ #include <cmath> ...

  3. 51 Nod 1636 教育改革(dp)

    1636 教育改革  题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 最近A学校正在实施教育改革. 一个学年由n天 ...

  4. 51 Nod 1352 集合计数

    大致题意:求ax+by=n+1的正数解的个数. 先看下面: 相信看过了通解的参数表示后已经知道怎么解了,贴代码: #include <bits/stdc++.h> #define ll l ...

  5. 利用栈实现字符串中三种括号的匹配问题c++语言实现

    编写一个算法,检查一个程序中的花括号,方括号和圆括号是否配对,若能够全部配对则返回1,否则返回0. Head.h: #ifndef HEAD_H_INCLUDED #define HEAD_H_INC ...

  6. css引入第三方字体

    上面图片时将字体文件放入到fonts文件夹内, 里面有一个fonts.css文件,将字体文件声明好, 然后像下面图片一样,在另外一个css内@import引入,(当然,也可以直接将声明和引用放在一个c ...

  7. BZOJ 2959 长跑 (LCT、并查集)

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2959 题解 真是被这题搞得心态大崩--调了7个小时--然而并查集都能写成\(O(n^2) ...

  8. TypeScript----函数与类

    TypeScript中的类 传统的JavaScript程序使用函数和基于原型的继承来创建可重用的组件,但对于熟悉使用面向对象方式的程序员来讲就有些棘手,因为他们用的是基于类的继承并且对象是由类构建出来 ...

  9. python3 selenium使用

    其实这个就相当于模拟人的点击事件来连续的访问浏览器.如果你玩过王者荣耀的话在2016年一月份的版本里面就有一个bug. 安卓手机下载一个按键精灵就可以在冒险模式里面设置按键,让手机自动玩闯关,一局19 ...

  10. win7安装Elasticsearch和Elasticsearch-Head插件

    1.环境搭建 1)Java环境搭建可以参考相关的资料,这里不做详细介绍 2)nodejs环境搭建 到官方网站下载相应的zip包:https://nodejs.org/dist/v8.9.1/node- ...