[LeetCode] 554. 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] 554. Brick Wall 砖头墙壁的更多相关文章
- [LeetCode] 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 ...
- 554. Brick Wall最少的穿墙个数
[抄题]: There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. ...
- 554. Brick Wall
class Solution { public: int leastBricks(vector<vector<int>>& wall) { unordered_map& ...
- [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 ...
- Java实现 LeetCode 554 砖墙(缝隙可以放在数组?)
554. 砖墙 你的面前有一堵方形的.由多行砖块组成的砖墙. 这些砖块高度相同但是宽度不同.你现在要画一条自顶向下的.穿过最少砖块的垂线. 砖墙由行的列表表示. 每一行都是一个代表从左至右每块砖的宽度 ...
- UVa 900 - Brick Wall Patterns
题目大意:用1*2的砖头建n*2的墙,问有多少种不同的砖头排列方式?与斐波那契序列相似. #include <cstdio> #define MAXN 60 #define N 50 un ...
- Leetcode 554.砖墙
砖墙 你的面前有一堵方形的.由多行砖块组成的砖墙. 这些砖块高度相同但是宽度不同.你现在要画一条自顶向下的.穿过最少砖块的垂线. 砖墙由行的列表表示. 每一行都是一个代表从左至右每块砖的宽度的整数列表 ...
- leetcode 学习心得 (3)
源代码地址:https://github.com/hopebo/hopelee 语言:C++ 517. Super Washing Machines You have n super washing ...
随机推荐
- snappy 安装
1 下载snappywget https://kojipkgs.fedoraproject.org//vol/fedora_koji_archive00/packages/snappy/1.1.0/1 ...
- python批量裁剪图片
"""用Pythonp批量裁剪图片""" from PIL import Imageimport matplotlib.pyplot as ...
- WebApi安全性 参数签名校验(结合Axios使用)
接口参数签名校验,是WebApi接口服务最重要的安全防护手段之一. 结合项目中实际使用情况,介绍下前后端参数签名校验实现方案. 签名校验规则 http请求,有两种传参形式: 1.通过url传参,最常见 ...
- visa
- ASP.NET Core Web 项目文件
在本节中,我们将探索并了解 asp.net core 项目文件. 我们使用 C#作为编程语言,因此项目文件具有.csproj 扩展名. 如果您使用过以前版本的 ASP.NET,那么您可能对此文件非常熟 ...
- JQ动态生成节点绑定事件无效问题
最近做项目的时候遇见了一个问题,通过jq将动态节点绑定到dom节点上,并且为动态节点绑定方法,此方法再次为动态节点添加动态节点,但在刷新之后,动态节点上的方法失效了,过程为:创建动态节点->动态 ...
- C变量和常量
变量定义 变量定义就是告诉编译器如何创建变量的储存,以及在何处创建变量的储存,变量定义指定了一个数据类型,并包含一个或者多个变量的列表: type variable_list //如: int i; ...
- c# Hashtable Synchronized vs SyncRoot
Synchronized vs SyncRoot 我们知道,在.net的一些集合类型中,譬如Hashtable和ArrayList,都有Synchronized静态方法和SyncRoot实例方法,他们 ...
- pip python
简介 pip 是一个安装和管理 Python 包的工具,python安装包的工具有easy_install, setuptools, pip,distribute.使用这些工具都能下载并安装djang ...
- poi操作Word创建超链接
项目引入poi: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</ ...