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. Canvas实用库Fabric.js使用手册

    简介什么是Fabric.js? Fabric.js是一个可以简化Canvas程序编写的库. Fabric.js为Canvas提供所缺少的对象模型, svg parser, 交互和一整套其他不可或缺的工 ...

  2. vue3.0以上关于打包后出现空白页和路由不起作用

    1.解决页面空白,找不到资源 在项目根目录中的vue.config.js中publicPath: '/'修改为publicPath: './',如果没有这个文件,新建一个,基础代码为: module. ...

  3. jquery selected选择器 语法

    jquery selected选择器 语法 作用::selected 选择器选取被选择的 <option> 元素.直线电机生产厂家 语法:$(":selected") ...

  4. JavaScript 小技巧整理

    1.过滤唯一值 Set类型是在ES6中新增的,它类似于数组,但是成员的值都是唯一的,没有重复的值.结合扩展运算符(...)我们可以创建一个新的数组,达到过滤原数组重复值的功能. const array ...

  5. zabbix自动注册,实现自动添加机器,减少人工干预

    1.zabbix_agent的安装配置: vim install_zabbix_agent.sh #!/bin/bash #author:chenjianwen RealIP=`curl -s htt ...

  6. 回调函数(callback) python / c++ 演示

    什么是回调函数? 我们绕点远路来回答这个问题. 编程分为两类:系统编程(system programming)和应用编程(application programming).所谓系统编程,简单来说,就是 ...

  7. 20165213 Exp7 网络欺诈防范

    Exp7 网络欺诈防范 一. 实践内容 简单应用SET工具建立冒名网站 1.首先使用sudo vi /etc/apache2/ports.conf 进行查看listen的端口号,若不是80改为80. ...

  8. 2 大O表示法

    1.大O表示法 表示程序的执行时间或占用空间随数据规模的增长趋势. 算法操作 时间复杂度 线性查找 O(n) 二分查找 O(logn) 无序数组插入 O(1) 无序数组删除 O(n) 有序数组插入 O ...

  9. SRS之SrsRtmpServer::connect_app详解

    1. connect('live') 2. SrsRtmpServer::connect_app 位于 srs_rtmp_stack.cpp.在 SRS 的 RTMP 连接处理线程 conn 中,当与 ...

  10. LC 406. Queue Reconstruction by Height

    Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...