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
.
解题思路:
1.这一题明显使用动态规划来解题,开始想法使用动态规划的方法来记录所有1连在一起的面积,之后记录该面积右下角的坐标,之后通过遍历一个方向的坐标来得到一条边长,之后使用面积来相除,得到一条短边的长度,即可求出正方形面积。但是这么做存在一个问题,即,面积所得出的结果是两边相乘的结果,如果体现在一个数值上,是无法应用到下一子状态的:比如,2*6=12 先前子状态最大面积为12,然而在下一子状态中是无法确定12是从何而来,可能是3*4,可能是2*6。无法使用一个数值来记录两个变量。
2. 随即求助于其他解法,如果一个点是一个正方形的右下角,那么它的左上,上方和左方一定存在着至少存在一个正方形,最优情况是三个正方形的边长是相同的,这样直接在边长上加上1就会构成一个新的正方形,然而如果三个方向的正方形不等的话如果选择最大的边长,就会在某个方向上缺失一角。所以正确的方式是找出最小的一个正方形之后加上1,构成新的正方形。
3.如果matrix的位置上为0,则之前所积累的正方形边长将会中断,所有在为0的dp[i][j]上的位置为0
参考代码:
public int maxSquare(int[][] matrix) {
// write your code here
int row = matrix.length;
int colum = matrix[0].length;
int[][] dp = new int[row][colum];
int max = 0;
dp[0][0] = matrix[0][0];
for(int i=1; i<row; i++){
dp[i][0] = matrix[i][0];
max = Math.max(dp[i][0], max); } for(int j=1; j<colum; j++){
dp[0][j] = matrix[0][j];
max = Math.max(dp[0][j],max); } for(int i=1; i<row; i++){
for(int j=1; j<colum; j++){
dp[i][j] = matrix[i][j]==1?Math.min(dp[i-1][j-1], Math.min(dp[i-1][j],dp[i][j-1]))+1:0;
max = Math.max(max, dp[i][j]);
}
}
return max*max;
}
LintCode刷题笔记-- Maximal Square的更多相关文章
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...
- LintCode刷题笔记-- Distinct Subsequences
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...
- LintCode刷题笔记-- BackpackIV
标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...
- LintCode刷题笔记-- BackpackII
标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...
- LintCode刷题笔记-- Update Bits
标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...
随机推荐
- Delphi的日志库
1. 安装 Log4D下载: 官网地址 LoggerPro下载 GitHub地址 特点: log4d简单易用.性能稳定 LoggerPro貌似功能很强大,只是没有详细的文档,懒得翻源码 安装步骤 Lo ...
- 03_Hibernate关系映射
关系映射? Hibernate的主要目的就是JAVA程序员可以随心所欲的使用对象编程思维来操作数据库.一些数据库表的关系我们应该可以通过hibernate实现,比如数据库中用到的主外键关系,还有一些与 ...
- Redis学习笔记01-分布式锁
1.分布式锁的定义与理解 在并发任务中,当对数据执行修改和删除时为了防止多个任务同时拿到数据而产生的混乱,这时就要用到分布式锁来限制程序的并发执行. Redis分布式锁本质上要实现的目标就是在Redi ...
- vue 路由跳转记住滚动位置,返回时回到上次滚动位置
参考:https://blog.csdn.net/qq_40204835/article/details/79853685 方法一: 利用Keep-Alive和监听器 1.首先在路由中引入需要的模块 ...
- JAVA缓存的实现
缓存可分为二大类: 一.通过文件缓存,顾名思义文件缓存是指把数据存储在磁盘上,不管你是以XML格式,序列化文件DAT格式还是其它文件格式: 二.内存缓存,也就是实现一个类中静态Map,对这个Map进行 ...
- PKU 百练OJ Arbitrage
http://bailian.openjudge.cn/practice/2240/ #include <iostream> #include <string> #includ ...
- Leetcode152. Maximum Product Subarray乘积的最大子序列
给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6. 示例 2 ...
- Windows API 第三篇
1.获得程序自身的路径: DWORD GetModuleFileName( HMODULE hModule, // handle to module LPTSTR lpFilename, // pat ...
- 3.Spring框架中的标签与配置文件分离
1.Spring框架中标签的配置 1. id属性和name属性的区别 * id -- Bean起个名字,在约束中采用ID的约束,唯一 * 取值要求:必须以字母开始,可以使用字母.数字.连字符.下划线. ...
- python 日记 day4
1.为何数据要分类 数据是用来表示状态的,不同的状态应该用不同类型的数据来表示. 2.数据类型 数字 字符串 列表 元组 字典 集合 列表:列表相比于字符串,不仅可以储存不同的数据类型,而且可以储存大 ...