https://leetcode.com/problems/brick-wall/
http://www.cnblogs.com/grandyang/p/6697067.html
https://leetcode.com/problems/brick-wall/discuss/101752/Neat-Java-Solution-O(n)-using-hashmap
We want to cut from the edge of the most common location among all the levels, hence using a map to record the locations and their corresponding occurrence
https://leetcode.com/problems/brick-wall/discuss/137777/Java-Map-solution
https://leetcode.com/problems/brick-wall/discuss/101794/Verbose-Java-Solution-PriorityQueue
There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The bricks have the same height but different width. You want to draw a vertical line from the top to the bottom and cross the least bricks.
The brick wall is represented by a list of rows. Each row is a list of integers representing the width of each brick in this row from left to right.
If your line go through the edge of a brick, then the brick is not considered as crossed. You need to find out how to draw the line to cross the least bricks and return the number of crossed bricks.
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.
Example:
Input: [[1,2,2,1], [3,1,2], [1,3,2], [2,4], [3,1,2], [1,3,1,1]] Output: 2 Explanation:
Note:
- The width sum of bricks in different rows are the same and won't exceed INT_MAX.
- 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.
http://www.cnblogs.com/grandyang/p/6697067.html
这道题给了我们一个砖头墙壁,上面由不同的长度的砖头组成,让我们选个地方从上往下把墙劈开,使得被劈开的砖头个数最少,前提是不能从墙壁的两边劈,这样没有什么意义。我们使用一个哈希表来建立每一个断点的长度和其出现频率之间的映射,这样只要我们从断点频率出现最多的地方劈墙,损坏的板砖一定最少
https://leetcode.com/problems/brick-wall/discuss/101728/I-DON'T-THINK-THERE-IS-A-BETTER-PERSON-THAN-ME-TO-ANSWER-THIS-QUESTIONhttps://leetcode.com/problems/brick-wall/discuss/101752/Neat-Java-Solution-O(n)-using-hashmap
We want to cut from the edge of the most common location among all the levels, hence using a map to record the locations and their corresponding occurrence
https://leetcode.com/problems/brick-wall/discuss/137777/Java-Map-solution
The idea is straightforward, since each brick length is positive, we can record all the edge index in the wall and figure out which edge index is the most common. We cut through that edge index, it will cross
number of rows - most common edge count
rows public int leastBricks(List<List<Integer>> wall) {
if(wall.size() == 0) return 0;
int count = 0;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(List<Integer> list : wall){
int length = 0;
for(int i = 0; i < list.size() - 1; i++){
length += list.get(i);
map.put(length, map.getOrDefault(length, 0) + 1);
count = Math.max(count, map.get(length));
}
}
return wall.size() - count;
}
X. PQhttps://leetcode.com/problems/brick-wall/discuss/101794/Verbose-Java-Solution-PriorityQueue
public int leastBricks(List<List<Integer>> wall) {
int R = wall.size(), min = R;
if (R == 1 && wall.get(0).size() > 1) return 0;
// [0: end, 1: row, 2: col]
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> (a[0] - b[0]));
for (int i = 0; i < R; i++) {
pq.add(new int[] {wall.get(i).get(0), i, 0});
}
while (!pq.isEmpty()) {
int end = pq.peek()[0], count = 0;
while (!pq.isEmpty() && pq.peek()[0] == end) {
count++;
int[] brick = pq.poll();
if (brick[2] < wall.get(brick[1]).size() - 1) {
pq.add(new int[] {end + wall.get(brick[1]).get(brick[2] + 1), brick[1], brick[2] + 1});
}
}
if (!pq.isEmpty()) {
min = Math.min(min, R - count);
}
}
return min;
}PQ