File tree 2 files changed +42
-1
lines changed
2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,148 LeetCode solutions in JavaScript
1
+ # 1,149 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
895
895
1131|[ Maximum of Absolute Value Expression] ( ./solutions/1131-maximum-of-absolute-value-expression.js ) |Medium|
896
896
1137|[ N-th Tribonacci Number] ( ./solutions/1137-n-th-tribonacci-number.js ) |Easy|
897
897
1138|[ Alphabet Board Path] ( ./solutions/1138-alphabet-board-path.js ) |Medium|
898
+ 1139|[ Largest 1-Bordered Square] ( ./solutions/1139-largest-1-bordered-square.js ) |Medium|
898
899
1143|[ Longest Common Subsequence] ( ./solutions/1143-longest-common-subsequence.js ) |Medium|
899
900
1161|[ Maximum Level Sum of a Binary Tree] ( ./solutions/1161-maximum-level-sum-of-a-binary-tree.js ) |Medium|
900
901
1189|[ Maximum Number of Balloons] ( ./solutions/1189-maximum-number-of-balloons.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1139. Largest 1-Bordered Square
3
+ * https://leetcode.com/problems/largest-1-bordered-square/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid
7
+ * that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.
8
+ */
9
+
10
+ /**
11
+ * @param {number[][] } grid
12
+ * @return {number }
13
+ */
14
+ var largest1BorderedSquare = function ( grid ) {
15
+ const rows = grid . length ;
16
+ const cols = grid [ 0 ] . length ;
17
+ const left = Array ( rows ) . fill ( ) . map ( ( ) => Array ( cols ) . fill ( 0 ) ) ;
18
+ const top = Array ( rows ) . fill ( ) . map ( ( ) => Array ( cols ) . fill ( 0 ) ) ;
19
+ let maxSide = 0 ;
20
+
21
+ for ( let i = 0 ; i < rows ; i ++ ) {
22
+ for ( let j = 0 ; j < cols ; j ++ ) {
23
+ if ( grid [ i ] [ j ] ) {
24
+ left [ i ] [ j ] = j > 0 ? left [ i ] [ j - 1 ] + 1 : 1 ;
25
+ top [ i ] [ j ] = i > 0 ? top [ i - 1 ] [ j ] + 1 : 1 ;
26
+
27
+ let side = Math . min ( left [ i ] [ j ] , top [ i ] [ j ] ) ;
28
+ while ( side > maxSide ) {
29
+ if ( left [ i - side + 1 ] [ j ] >= side && top [ i ] [ j - side + 1 ] >= side ) {
30
+ maxSide = side ;
31
+ break ;
32
+ }
33
+ side -- ;
34
+ }
35
+ }
36
+ }
37
+ }
38
+
39
+ return maxSide * maxSide ;
40
+ } ;
You can’t perform that action at this time.
0 commit comments