[LeetCode] Brick Wall 砖头墙壁
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:
- 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.
这道题给了我们一个砖头墙壁,上面由不同的长度的砖头组成,让选个地方从上往下把墙劈开,使得被劈开的砖头个数最少,前提是不能从墙壁的两边劈,这样没有什么意义。这里使用一个 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)
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Brick Wall 砖头墙壁的更多相关文章
- [LeetCode] 554. Brick Wall 砖头墙壁
There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The b ...
- 【LeetCode】554. Brick Wall 解题报告(Python)
[LeetCode]554. Brick Wall 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- [Swift]LeetCode554. 砖墙 | Brick Wall
There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The b ...
- 554. Brick Wall最少的穿墙个数
[抄题]: There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. ...
- UVa 900 - Brick Wall Patterns
题目大意:用1*2的砖头建n*2的墙,问有多少种不同的砖头排列方式?与斐波那契序列相似. #include <cstdio> #define MAXN 60 #define N 50 un ...
- 554. Brick Wall
class Solution { public: int leastBricks(vector<vector<int>>& wall) { unordered_map& ...
- leetcode 学习心得 (3)
源代码地址:https://github.com/hopebo/hopelee 语言:C++ 517. Super Washing Machines You have n super washing ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- 升级 mysql5.6 配置文件my.cnf sql_mode 解析与设置问题
sql_mode是个很容易被忽视的变量,默认值是空值,在这种设置下是可以允许一些非法操作的,比如允许一些非法数据的插入.在生产环境必须将这个值设置为严格模式,所以开发.测试环境的数据库也必须要设置,这 ...
- 开始补习JavaScript的第一天
JavaScript介绍: ①.JavaScript是一种解释性的,基于对象的脚本语言. ②.JavaScript是一种轻量级的编程语言,可以嵌入到html页面中,由浏览器来解释执行. ③.JavaS ...
- SuSE的命令安装软件 zypper
转自:http://blog.csdn.net/s_k_yliu/article/details/6674079 SuSE的命令安装软件 zypper,yast2 redhat yum debain ...
- socket_sever实现多客户端并发
#!/usr/bin/env python # -*- coding:utf-8 -*- import socketserver class mysever(socketserver.BaseRequ ...
- DES MEI号码加密
对于加密来说,使用DES加密解密很好,但是为了使不同设备的密文不一样,可以使用 固定字符串 + 设备IMEI号码 加密的方式,这样可以分辨不同手机,限制手机的使用(若是注册,一个手机只有一个IMEI号 ...
- SourceTree 实现 git flow 流程
为什么使用 git 和 git flow,这篇文章 深入理解学习Git工作流 的内容相信能够给你一个完整的答案. 我们以使用SVN的工作流来使用git有什么不妥? git 方便的branch在哪里,团 ...
- java异常常见面试问题
java异常常见面试问题 一.java异常的理解 异常主要是处理编译期不能捕获的错误.出现问题时能继续顺利执行下去,而不导致程序终止,确保程序的健壮性. 处理过程:产生异常状态时,如果当前的conte ...
- 新概念英语(1-111)The most expensive model
Lesson 111 The most expensive model 最昂贵的型号 Listen to the tape then answer this question. Can Mr. Fri ...
- Mysql 5.1的坑
1.数据库表是区分大小写的 之前程序在5.7数据库没问题,测试环境上数据库是5.1的,就提示表找不到. 2.同样的sql,在5.1上会提示事务获取锁失败,超时返回.而5.7上正常.原因暂未找到.
- 分享java常用技术教程:dubbo、zookeeper、ActiveMq、多线程、Redis、PowerDesigner等
游戏是自己整理的邮箱来源于网上,下面是我的有道云的分享地址: https://note.youdao.com/share/?id=c5f258fa9efd1b28b2d8f0d37e59b194&am ...