LeetCode 1139. Largest 1-Bordered Square

Posted Dylan_Java_NYC

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 1139. Largest 1-Bordered Square相关的知识,希望对你有一定的参考价值。

原题链接在这里: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:

 1 class Solution {
 2     public int largest1BorderedSquare(int[][] grid) {
 3         if(grid == null || grid.length == 0 || grid[0].length == 0){
 4             return 0;
 5         }
 6         
 7         int m = grid.length;
 8         int n = grid[0].length;
 9         int [][] top = new int[m][n];
10         int [][] left = new int[m][n];
11         for(int i = 0; i<m; i++){
12             for(int j = 0; j<n; j++){
13                 if(grid[i][j] > 0){
14                     top[i][j] = i == 0 ? 1 : top[i-1][j]+1;
15                     left[i][j] = j == 0 ? 1 : left[i][j-1]+1;
16                 }
17             }
18         }
19         
20         for(int l = Math.min(m, n); l>0; l--){
21             for(int i = 0; i+l-1<m; i++){
22                 for(int j = 0; j+l-1<n; j++){
23                     if(top[i+l-1][j] >= l 
24                         && top[i+l-1][j+l-1] >= l
25                         && left[i][j+l-1] >= l
26                         && left[i+l-1][j+l-1] >= l){
27                         return l*l;
28                     }
29                 }
30             }
31         }
32         
33         return 0;
34     }
35 }

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:

 1 class Solution {
 2     public int largest1BorderedSquare(int[][] grid) {
 3         if(grid == null || grid.length == 0 || grid[0].length == 0){
 4             return 0;
 5         }
 6         
 7         int m = grid.length;
 8         int n = grid[0].length;
 9         int [][] top = new int[m][n];
10         int [][] left = new int[m][n];
11         for(int i = 0; i<m; i++){
12             for(int j = 0; j<n; j++){
13                 if(grid[i][j] > 0){
14                     top[i][j] = i == 0 ? 1 : top[i-1][j]+1;
15                     left[i][j] = j == 0 ? 1 : left[i][j-1]+1;
16                 }
17             }
18         }
19         
20         int res = 0;
21         
22         for(int i = m-1; i>=0; i--){
23             for(int j = n-1; j>=0; j--){
24                 int small = Math.min(top[i][j], left[i][j]);
25                 while(small > res){
26                     if(top[i][j-small+1] >= small && left[i-small+1][j] >= small){
27                         res = small;
28                         break;
29                     }
30                     
31                     small--;
32                 }
33             }
34         }
35         
36         return res*res;
37     }
38 }

 

以上是关于LeetCode 1139. Largest 1-Bordered Square的主要内容,如果未能解决你的问题,请参考以下文章

[Lintcode]184. Largest Number/[Leetcode]179. Largest Number

LeetCode 1139. 最大的以 1 为边界的正方形

Leetcode--179--Largest Number

LeetCode 976. Largest Perimeter Triangle

[leetcode-812-Largest Triangle Area]

Leetcode: Largest Divisible Subset