LeetCode 1139. Largest 1-Bordered Square
原题链接在这里:https://leetcode.com/problems/largest-1-bordered-square/
题目:
Given a 2D grid
of 0
s and 1
s, return the number of elements in the largest square subgrid that has all 1
s on its border, or 0
if such a subgrid doesn't exist in the grid
.
Example 1:
Input: grid = [[1,1,1],[1,0,1],[1,1,1]]
Output: 9
Example 2:
Input: grid = [[1,1,0,0]]
Output: 1
Constraints:
1 <= grid.length <= 100
1 <= grid[0].length <= 100
grid[i][j]
is0
or1
题解:
For each cell in the grid, calculate its farest reach on top and left direction.
Then starting from l = Math.min(grid.length, grid[0].length) to l = 1, iterate grid to check if square with boarder l exist. If it does return l*l.
Time Complexity: O(m*n*(min(m,n))). m = grid.length. n = grid[0].length.
Space: O(m*n).
AC Java:
class Solution {
public int largest1BorderedSquare(int[][] grid) {
if(grid == null || grid.length == 0 || grid[0].length == 0){
return 0;
} int m = grid.length;
int n = grid[0].length;
int [][] top = new int[m][n];
int [][] left = new int[m][n];
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(grid[i][j] > 0){
top[i][j] = i == 0 ? 1 : top[i-1][j]+1;
left[i][j] = j == 0 ? 1 : left[i][j-1]+1;
}
}
} for(int l = Math.min(m, n); l>0; l--){
for(int i = 0; i+l-1<m; i++){
for(int j = 0; j+l-1<n; j++){
if(top[i+l-1][j] >= l
&& top[i+l-1][j+l-1] >= l
&& left[i][j+l-1] >= l
&& left[i+l-1][j+l-1] >= l){
return l*l;
}
}
}
} return 0;
}
}
Or after get top and left.
Iterate the grid, for each cell, get small = min(top[i][j], left[i][j]).
All l = small to 1 could be protential square boarder. But we only need to check small larger than global longest boarder since we only care about the largest.
Check top of grid[i][j-small+1] and left of grid[i-small+1][j]. If they are both larger than small, then it is a grid.
Time Complexity: O(n^3).
Space: O(n^2).
AC Java:
class Solution {
public int largest1BorderedSquare(int[][] grid) {
if(grid == null || grid.length == 0 || grid[0].length == 0){
return 0;
} int m = grid.length;
int n = grid[0].length;
int [][] top = new int[m][n];
int [][] left = new int[m][n];
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(grid[i][j] > 0){
top[i][j] = i == 0 ? 1 : top[i-1][j]+1;
left[i][j] = j == 0 ? 1 : left[i][j-1]+1;
}
}
} int res = 0; for(int i = m-1; i>=0; i--){
for(int j = n-1; j>=0; j--){
int small = Math.min(top[i][j], left[i][j]);
while(small > res){
if(top[i][j-small+1] >= small && left[i-small+1][j] >= small){
res = small;
break;
} small--;
}
}
} return res*res;
}
}
LeetCode 1139. Largest 1-Bordered Square的更多相关文章
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
- [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [LeetCode] Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
- LeetCode Kth Largest Element in an Array
原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...
- Leetcode:Largest Number详细题解
题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...
- [LeetCode][Python]Largest Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...
随机推荐
- Tarjan求有向图强连通分量 BY:优少
Tarjan算法:一种由Robert Tarjan提出的求解有向图强连通分量的线性时间的算法. 定义给出之后,让我们进入算法的学习... [情境引入] [HAOI2006受欢迎的牛] 题目描述: 每头 ...
- C# 获取特殊日期
//1.当前时间DateTime dt = DateTime.Now; //2.本周周一DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.D ...
- tkinter基础-输入框、文本框
本节内容 了解输入框.文本框的使用方法 利用1制作简易界面 首先明确上面由几个元素组成:该界面由界面标题,输入框.两个按钮.文本框组成. 该界面我们需要实现的功能: 在输入框中输入文字,点击inser ...
- loadrunner通过字符串左右边界提取字符串
/****** *函数名称:strcut *函数说明:通过左边界.右边界,从字符串中截取子字符串 *注意事项:会申请新的内存,需要手动释放 ******/ void strcut(char *strS ...
- vue侦听器 基础4
侦听器 使用方式:设置需要侦听的data里的属性名就可以了 new Vue({ el:"#app", data:{ count:0 }, watchers:{ count(){ / ...
- 英语Affrike非洲Affrike单词
中文名称阿非利加洲(全称) 外文名称Africa 别 名Affrike 行政区类别洲 下辖地区北非.东非.西非.中非.南非 地理位置东濒印度洋,西临大西洋,北至地中海,南至好望角 面 积3022万平方 ...
- react native错误排查-TypeError: window.deltaUrlToBlobUrl is not a function
错误现象:window.deltaUrlToBlobUrl is not a function 最近在调试react-native时,打开浏览器调试时发现报错window.deltaUrlToBlob ...
- Ubuntu 系统装机指南
1.vim设置 2.git配置 3.系统性能监视器:Ubuntu安装系统监视器 4.编译环境安装:sudo apt-get install build-essential
- windows下用纯C实现一个简陋的imshow:基于GDI
intro 先前实现了GDI显示图像时设定窗口大小为图像大小,不过并没有刻意封装函数调用接口,并不适合给其他函数调用.现在简单封装一下,特点: 纯C 基于GDI,因此只支持windows平台 类似于o ...
- 你真的会使用 VMware Workstation 吗
你真的会使用VMware Workstation吗?网上有很多教程,虽然都还可以,但总感觉差强人意.所以笔者在这里分享自己的使用心得,让大家参考一下,个人认为是最好的了. 简介 VMware Work ...