Skip to content

Commit d838d7f

Browse files
refactor 554
1 parent c29e1d6 commit d838d7f

File tree

1 file changed

+2
-31
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+2
-31
lines changed

src/main/java/com/fishercoder/solutions/_554.java

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,18 @@
44
import java.util.List;
55
import java.util.Map;
66

7-
/**
8-
* 554. Brick Wall
9-
*
10-
* There is a brick wall in front of you. The wall is rectangular and has several rows of bricks.
11-
* The bricks have the same height but different width.
12-
* You want to draw a vertical line from the top to the bottom and cross the least bricks.
13-
* The brick wall is represented by a list of rows.
14-
* Each row is a list of integers representing the width of each brick in this row from left to right.
15-
* If your line go through the edge of a brick,
16-
* then the brick is not considered as crossed.
17-
* You need to find out how to draw the line to cross the least bricks and return the number of crossed bricks.
18-
* You cannot draw a line just along one of the two vertical edges of the wall, in which case the line will obviously cross no bricks.
19-
20-
Example:
21-
Input:
22-
[[1,2,2,1],
23-
[3,1,2],
24-
[1,3,2],
25-
[2,4],
26-
[3,1,2],
27-
[1,3,1,1]]
28-
29-
Output: 2
30-
Explanation:
31-
32-
Note:
33-
The width sum of bricks in different rows are the same and won't exceed INT_MAX.
34-
The number of bricks in each row is in range [1,10,000]. The height of wall is in range [1,10,000]. Total number of bricks of the wall won't exceed 20,000.
35-
*/
367
public class _554 {
378
public static class Solution1 {
389
/**
3910
* credit: https://leetcode.com/articles/brick-wall/
40-
*
11+
* <p>
4112
* we make use of a HashMap
4213
* map which is used to store entries in the form:
4314
* (sum,count). Here,
4415
* sum refers to the cumulative sum of the bricks' widths encountered in the current row, and
4516
* count refers to the number of times the corresponding sum is obtained. Thus,
4617
* sum in a way, represents the positions of the bricks's boundaries relative to the leftmost boundary.
47-
*
18+
* <p>
4819
* This is done based on the following observation:
4920
* We will never obtain the same value of sum twice while traversing over a particular row.
5021
* Thus, if the sum value is repeated while traversing over the rows, it means some row's brick boundary coincides with some previous row's brick boundary.

0 commit comments

Comments
 (0)