|
4 | 4 | import java.util.List;
|
5 | 5 | import java.util.Map;
|
6 | 6 |
|
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 |
| - */ |
36 | 7 | public class _554 {
|
37 | 8 | public static class Solution1 {
|
38 | 9 | /**
|
39 | 10 | * credit: https://leetcode.com/articles/brick-wall/
|
40 |
| - * |
| 11 | + * <p> |
41 | 12 | * we make use of a HashMap
|
42 | 13 | * map which is used to store entries in the form:
|
43 | 14 | * (sum,count). Here,
|
44 | 15 | * sum refers to the cumulative sum of the bricks' widths encountered in the current row, and
|
45 | 16 | * count refers to the number of times the corresponding sum is obtained. Thus,
|
46 | 17 | * sum in a way, represents the positions of the bricks's boundaries relative to the leftmost boundary.
|
47 |
| - * |
| 18 | + * <p> |
48 | 19 | * This is done based on the following observation:
|
49 | 20 | * We will never obtain the same value of sum twice while traversing over a particular row.
|
50 | 21 | * 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