LeetCode:DP专题详解

221 medium

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 0;
        }
        int m = matrix.size(), n = matrix[0].size(), sz = 0;
        vector<vector<int>> dp(m, vector<int>(n, 0));
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (!i || !j || matrix[i][j] == ‘0‘) {
                    dp[i][j] = matrix[i][j] - ‘0‘;
                } else {
                    dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
                }
                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 0;
        }
        int m = matrix.size(), n = matrix[0].size(), sz = 0;
        vector<int> pre(n, 0), cur(n, 0);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (!i || !j || matrix[i][j] == ‘0‘) {
                    cur[j] = matrix[i][j] - ‘0‘;
                } else {
                    cur[j] = min(pre[j - 1], min(pre[j], cur[j - 1])) + 1;
                }
                sz = max(cur[j], sz);
            }
            fill(pre.begin(), pre.end(), 0);
            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 0;
        }
        int m = matrix.size(), n = matrix[0].size(), sz = 0, pre;
        vector<int> cur(n, 0);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int temp = cur[j];
                if (!i || !j || matrix[i][j] == ‘0‘) {
                    cur[j] = matrix[i][j] - ‘0‘;
                } else {
                    cur[j] = min(pre, min(cur[j], cur[j - 1])) + 1;
                }
                sz = max(cur[j], sz);
                pre = temp;
            }
        }
        return sz * sz;
    }
};

原文地址:https://www.cnblogs.com/shona/p/11311161.html

时间: 2024-08-30 17:05:03

LeetCode:DP专题详解的相关文章

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

这个题做了两天了.首先用并查集分类是明白的, 不过判断是否情况唯一刚开始用的是搜索.总是超时. 后来看别人的结题报告, 才恍然大悟判断唯一得用DP. 题目大意: 一共有p1+p2个人,分成两组,一组p1个,一组p2个.给出N个条件,格式如下: x y yes表示x和y分到同一组 x y no表示x和y分到不同组 问分组情况是否唯一,若唯一则按从小到大顺序输出,否则输出no.保证不存在矛盾条件,但是有可能出现x=y的情况. 题目分析: 题中会给我们一些信息, 告诉我们那些是同一类, 哪些是不同类.

数位DP模板详解

1 // pos = 当前处理的位置(一般从高位到低位) 2 // pre = 上一个位的数字(更高的那一位) 3 // status = 要达到的状态,如果为1则可以认为找到了答案,到时候用来返回, 4 // 给计数器+1. 5 // limit = 是否受限,也即当前处理这位能否随便取值.如567,当前处理6这位, 6 // 如果前面取的是4,则当前这位可以取0-9.如果前面取的5,那么当前 7 // 这位就不能随便取,不然会超出这个数的范围,所以如果前面取5的 8 // 话此时的limit

LeetCode dp专题

longest valid parentheses: dp[i]表示到i为止合法的()长度 s[i] == ')' : dp[i] = dp[i-2] + 2                          ( s[i]=='(' ) dp[i] = dp[i-1] + 2 + dp[i-dp[i-1]-2]  ( s[i-1] == ')' && s[i-1-dp[i-1]] == '(' ) 注意判断数组下标值是否存在 72. Edit Distance 将word1转换成word2

sql优化专题详解

数据库的优化问题 一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用 系统提交实际应用后,随着数据库中数据的增加,系统的响应速度就成为目前系统需要解决的最主要的问题之一.系统优化中一个很重要的方面就是SQL语句的优 化.对于海量数据,劣质SQL语句和优质SQL语句之间的速度差别可以达到上百倍,可见对于一个系统不是简单地能实现其功能就可,而是要写出高质量的 SQL语句,提高系统的可用性. 在多数情

LeetCode算法题详解之两个数组的交集

题目背景: 这个与我们高中时期学习的交集是一样的,顺便复习一下相关的数学知识有助于更好的理解. 交集的定义: 对于两个集合A和B,定义A和B的交集为C,其中C={x|x属于A且X属于B},记作A∩B. 如图所示: 解题思路一: public int[] intersect(int[] nums1, int[] nums2) { Arrays.sort(nums1); Arrays.sort(nums2); List<Integer> tmp = new ArrayList<Integer

【leetcode】Edit Distance 详解

下图为TI C6xx DSP Nyquist总线拓扑图,总线连接了master与slave,提供了高速的数据传输.有很多种速率不同的总线,如图中的红色方框,最高速总线为CPU/2 TeraNet SCR(即VBUSM SCR),带宽为256bit,其他低速总线为CPU/3,CPU/6,带宽参考图中所示.总线之间用Bridge(桥)连接,作用包括转换总线的速率,使之与所流向总线的速率相同等. 在具体应用中,各种速率的总线完全可以满足复杂的数据传输,而数据传输的瓶颈往往在于连接总线之间的Bridge

zabbix专题:第二章 zabbix3.0安装详解

zabbix3.0安装详解 本节目录大纲 安装配置mariadb 安装服务器端 zabbix web配置 web页面初始化 更改为中文 中文乱码问题 zabbix专题:第二章 zabbix3.2安装详解 zabbix专题:第二章 zabbix3.2安装详解 官方文档地址: https://www.zabbix.com/documentation/3.2/manual/installation/install_from_packages 我安装zabbix用的rpm包,可以从官网的源里面去下载,需

HUD5282 Senior&#39;s String 详解(使用DP解决组合数学)

题意:假设两个字符串的最长公共子序列长度为L,求第一个字符串中有多少个长度为L的子序列是第二个字符串的子序列.显然找出一个字符串的所有长度为L的子序列是组合数学问题,如果枚举所有子串的时间复杂度是n! 级的.这里就需要用动态规划来解决.首先用dp[i][j]和num[i][j]分别记录x的前I个字母和y的前j 个字母的最长公共子序列的长度和个数.先求出dp, 然后求num:.求num[i][j]分为两种情况,子序列不选x[i]和选x[i]: 1. 不选x[i]: 如果dp[i][j] == dp

(转)dp动态规划分类详解

dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. ****************************************************************************************** 动态规划(英语:Dynamic programm