File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 439
439
551|[ Student Attendance Record I] ( ./0551-student-attendance-record-i.js ) |Easy|
440
440
552|[ Student Attendance Record II] ( ./0552-student-attendance-record-ii.js ) |Hard|
441
441
553|[ Optimal Division] ( ./0553-optimal-division.js ) |Medium|
442
+ 554|[ Brick Wall] ( ./0554-brick-wall.js ) |Medium|
442
443
557|[ Reverse Words in a String III] ( ./0557-reverse-words-in-a-string-iii.js ) |Easy|
443
444
560|[ Subarray Sum Equals K] ( ./0560-subarray-sum-equals-k.js ) |Medium|
444
445
563|[ Binary Tree Tilt] ( ./0563-binary-tree-tilt.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 554. Brick Wall
3
+ * https://leetcode.com/problems/brick-wall/
4
+ * Difficulty: Medium
5
+ *
6
+ * There is a rectangular brick wall in front of you with n rows of bricks. The ith row has
7
+ * some number of bricks each of the same height (i.e., one unit) but they can be of different
8
+ * widths. The total width of each row is the same.
9
+ *
10
+ * Draw a vertical line from the top to the bottom and cross the least bricks. If your line
11
+ * goes through the edge of a brick, then the brick is not considered as crossed. You cannot
12
+ * draw a line just along one of the two vertical edges of the wall, in which case the line
13
+ * will obviously cross no bricks.
14
+ *
15
+ * Given the 2D array wall that contains the information about the wall, return the minimum
16
+ * number of crossed bricks after drawing such a vertical line.
17
+ */
18
+
19
+ /**
20
+ * @param {number[][] } wall
21
+ * @return {number }
22
+ */
23
+ var leastBricks = function ( wall ) {
24
+ const map = new Map ( ) ;
25
+ let max = 0 ;
26
+
27
+ for ( const row of wall ) {
28
+ for ( let i = 0 , sum = 0 ; i < row . length - 1 ; i ++ ) {
29
+ sum += row [ i ] ;
30
+ map . set ( sum , ( map . get ( sum ) || 0 ) + 1 ) ;
31
+ max = Math . max ( max , map . get ( sum ) ) ;
32
+ }
33
+ }
34
+
35
+ return wall . length - max ;
36
+ } ;
You can’t perform that action at this time.
0 commit comments