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 }

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11785076.html

时间: 2024-10-21 03:55:57

LeetCode 1139. Largest 1-Bordered Square的相关文章

LeetCode[Sort]: Largest Number

LeetCode[Sort]: Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to

[LeetCode OJ] Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

LeetCode:Largest Number

1.题目名称 Largest Number(求整型数组中各元素可拼合成的最大数字) 2.题目地址 https://leetcode.com/problems/largest-number/ 3.题目内容 英文:Given a list of non negative integers, arrange them such that they form the largest number. 中文:给出一组非负整数,求这些非负整数可以拼接出的最大数字 说明:例如,给出数组 [3, 30, 34,

8.15 [LeetCode] 179 Largest Number

[LeetCode 179] Largest Number | COMMENTS Question link Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be ver

leetcode Kth Largest Element in an Array

题目连接 https://leetcode.com/problems/kth-largest-element-in-an-array/ Kth Largest Element in an Array Description Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct eleme

[LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值

You need to find the largest value in each row of a binary tree. Example: Input: 1 / 3 2 / \ \ 5 3 9 Output: [1, 3, 9] 这道题让我们找二叉树每行的最大的结点值,那么实际上最直接的方法就是用层序遍历,然后在每一层中找到最大值,加入结果res中即可,参见代码如下: 解法一: class Solution { public: vector<int> largestValues(Tre

leetCode 179. Largest Number 字符串排序 | Medium

179. Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a st

[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 sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array'

LeetCode之“动态规划”: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. 在GeeksforGeeks有一个解决该问题的方法: 1) Cons