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 leastbricks.

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:

  1. The width sum of bricks in different rows are the same and won't exceed INT_MAX.
  2. 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.

这道题给了我们一个砖头墙壁,上面由不同的长度的砖头组成,让选个地方从上往下把墙劈开,使得被劈开的砖头个数最少,前提是不能从墙壁的两边劈,这样没有什么意义。这里使用一个 HashMap 来建立每一个断点的长度和其出现频率之间的映射,这样只要从断点频率出现最多的地方劈墙,损坏的板砖一定最少。遍历砖墙的每一层,新建一个变量 sum,然后从第一块转头遍历到倒数第二块,将当前转头长度累加到 sum 上,这样每次得到的 sum 就是断点的长度,将其在 HashMap 中的映射值自增1,并且每次都更新下最大的映射值到变量 mx,这样最终 mx 就是出现次数最多的断点值,在这里劈开,绝对损伤的转头数量最少,参见代码如下:

class Solution {
public:
int leastBricks(vector<vector<int>>& wall) {
int mx = , n = wall.size();
unordered_map<int, int> m;
for (auto &row : wall) {
int sum = , cnt = row.size();
for (int i = ; i < cnt - ; ++i) {
sum += row[i];
++m[sum];
mx = max(mx, m[sum]);
}
}
return n - mx;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/554

参考资料:

https://leetcode.com/problems/brick-wall/

https://leetcode.com/problems/brick-wall/discuss/101738/C%2B%2B-6-lines-(hash-map)

https://leetcode.com/problems/brick-wall/discuss/101728/I-DON'T-THINK-THERE-IS-A-BETTER-PERSON-THAN-ME-TO-ANSWER-THIS-QUESTION

LeetCode All in One 题目讲解汇总(持续更新中...)

最新文章

  1. Lesson 13 The Greenwood Boys
  2. linux 运维必备150个命令
  3. 《深入理解bootstrap》读书笔记:第一章 入门准备
  4. maven总结2
  5. .NET程序员吧需要知道的小知识——关于数据库
  6. Javascript中的冒泡排序,插入排序,选择排序,快速排序,归并排序,堆排序 算法性能分析
  7. SQL内外左右交叉连接
  8. jQuery自学笔记(一):初识jQuery
  9. Fedora15下搭建QT开发环境及编译QT
  10. MySQL命令记录1
  11. Tri_integral Summer Training 5 总结
  12. 超级强悍的PHP代码编辑器PHPstorm及配置
  13. java系列--集合
  14. hibernate使用setResultTransformer()将SQL查询结果放入集合中
  15. x264源代码简单分析:x264_slice_write()
  16. javascript知识详解之8张思维导图
  17. Django基础四&lt;二&gt;(OneToMany和 ManyToMany,ModelForm)
  18. OpenCV读写摄像头并写入视频
  19. gitlab 添加 ssh
  20. 理解maven命令package、install、deploy的联系与区别

热门文章

  1. VS Code 提示‘未找到Git。请安装Git,或在“git.path”设置中配置’
  2. 九、Spring之BeanFactory源码分析(一)
  3. python在字节流中对int24的转换
  4. touch.js - 移动设备上的手势识别与事件库
  5. A query was run and no Result Maps were found for the Mapped Statement
  6. AngleSharp 实战(04)之遍历内部超链接(a)元素的 Href 和 InnerText
  7. 原创的离线版 Redis 教程,给力!
  8. javascript:警告(alert 消息对话框),确认(confirm 消息对话框)
  9. LeetCode 905. Sort Array By Parity 按奇偶校验排列数组
  10. codeforces #592(Div.2)