原题链接在这里:https://leetcode.com/problems/largest-1-bordered-square/

题目:

Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s 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] is 0 or 1

题解:

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的更多相关文章

  1. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  2. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  3. 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 ...

  4. [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  5. [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 ...

  6. JavaScript中sort方法的一个坑(leetcode 179. Largest Number)

    在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...

  7. LeetCode Kth Largest Element in an Array

    原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...

  8. Leetcode:Largest Number详细题解

    题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...

  9. [LeetCode][Python]Largest Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...

随机推荐

  1. SQLite接口函数 - C核心api实践与总结

    SQLite核心源代码由C语言写就,同时提供了很多的扩展包可应用于其他编程语言和类库,如Python.Ruby.Java.Perl..Net/C#.Qt和ODBC.在很多情况下,针对一种语言有很多扩展 ...

  2. 关于nslookup以及dig命令的研究报告

    我们在日常上网时都是用域名访问网路,如www.baidu.com,而在实际寻址过程中,是使用IP地址,如180.101.49.11,域名到IP地址的解析是通过DNS服务器来实现的,系统中我们可以用一些 ...

  3. 视频推流模式HLS,HTTP,RTSP,RTMP协议的区别

    HTTP: 先通过服务器将FLV下载到本地缓存,然后再通过NetConnection的本地连接来播放这个FLV,这种方法是播放本地的视频,并不是播放服务器的视频.因此在本地缓存里可以找到这个FLV.其 ...

  4. win10系统驱动备份及还原

    win10系统驱动备份及还原 方法如下: 1.右键单击开始按钮,选择“命令提示符(管理员)” 2.输入如下命令后按回车,等待备份完成: dism /online /export-driver /des ...

  5. 复杂dic的文件化存储和读取问题

    今天遇到一个难题.整出一个复杂的dic,里面不仅维度多,还含有numpy.array.超级复杂.过程中希望能够存储一下,万一服务器停了呢?万一断电了呢? 结果存好存,取出来可就不是那样了.网上搜索了很 ...

  6. 05、MySQL—字符集

    1.字符编码概念 字符(Character)是各种文字和符号的总称,包括各国家文字.标点符号.图形符号.数字等. 在计算机中所看到的任何内容都是字符构成的. 字符编码(character code)是 ...

  7. Linux 命令:ls

    1. ls命令: list的缩写,查看目录中的文件 ls [选项] [目录名] dos 命令: dir 2. 常用参数: -a, –all 列出目录下的所有文件,包括以 . 开头的隐含文件 -A 同- ...

  8. js 取得数组中的最大值和最小值(含多维数组)

    转自:http://www.dewen.org/q/433 方法一: var a=[1,2,3,5]; alert(Math.max.apply(null, a));//最大值 alert(Math. ...

  9. Linux命令——parted

    参考:8 Linux ‘Parted’ Commands to Create, Resize and Rescue Disk Partitions 简介 parted是磁盘分区操作工具,支持多种磁盘分 ...

  10. 计算机网络基础之IP地址详解

    计算机网络基础之IP地址详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.IP地址概述 1>.什么是IP地址 我们为什么要使用逻辑地址(IP地址)来标识网络设备,而不采 ...