Skip to content

Commit 2dc488d

Browse files
committed
Add solution #554
1 parent 219d349 commit 2dc488d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@
439439
551|[Student Attendance Record I](./0551-student-attendance-record-i.js)|Easy|
440440
552|[Student Attendance Record II](./0552-student-attendance-record-ii.js)|Hard|
441441
553|[Optimal Division](./0553-optimal-division.js)|Medium|
442+
554|[Brick Wall](./0554-brick-wall.js)|Medium|
442443
557|[Reverse Words in a String III](./0557-reverse-words-in-a-string-iii.js)|Easy|
443444
560|[Subarray Sum Equals K](./0560-subarray-sum-equals-k.js)|Medium|
444445
563|[Binary Tree Tilt](./0563-binary-tree-tilt.js)|Easy|

solutions/0554-brick-wall.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
};

0 commit comments

Comments
 (0)