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.

思路:

可以从图片中看出,如果能够同时go through说明每一层左边的和是相等的,图中go through的位置和为4,第2、3、5、6层的均满足,

而brick总共6层,于是最少穿过的brick为2层。

于是想到出现同一个和的个数越多,即满足穿过的brick越少。

用一个map来统计即可。

 int leastBricks(vector<vector<int> >& wall)
{
map<int,int>sumTable;//统计相同和的个数
map<int ,int>::iterator it;
for(int i =;i<wall.size();i++)
{
int sum =;
for(int j =;j<wall[i].size()-;j++)//从0开始 倒数第一个结束
{
sum+=wall[i][j];
sumTable[sum]++;
}
}
int res =;
for(it = sumTable.begin();it!=sumTable.end();it++)
{
res = max(res,it->second);
}
return wall.size()-res;
}

[leetcode-554-Brick Wall]的更多相关文章

  1. [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 ...

  2. 【LeetCode】554. Brick Wall 解题报告(Python)

    [LeetCode]554. Brick Wall 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  3. 554. Brick Wall最少的穿墙个数

    [抄题]: There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. ...

  4. 554. Brick Wall

    class Solution { public: int leastBricks(vector<vector<int>>& wall) { unordered_map& ...

  5. [LeetCode] Brick Wall 砖头墙壁

    There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The b ...

  6. [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 ...

  7. Java实现 LeetCode 554 砖墙(缝隙可以放在数组?)

    554. 砖墙 你的面前有一堵方形的.由多行砖块组成的砖墙. 这些砖块高度相同但是宽度不同.你现在要画一条自顶向下的.穿过最少砖块的垂线. 砖墙由行的列表表示. 每一行都是一个代表从左至右每块砖的宽度 ...

  8. Leetcode 554.砖墙

    砖墙 你的面前有一堵方形的.由多行砖块组成的砖墙. 这些砖块高度相同但是宽度不同.你现在要画一条自顶向下的.穿过最少砖块的垂线. 砖墙由行的列表表示. 每一行都是一个代表从左至右每块砖的宽度的整数列表 ...

  9. UVa 900 - Brick Wall Patterns

    题目大意:用1*2的砖头建n*2的墙,问有多少种不同的砖头排列方式?与斐波那契序列相似. #include <cstdio> #define MAXN 60 #define N 50 un ...

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

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. weiphp的学习

    ##2015-07-27 **token 一定要加 不然在lists页面无法显示** ##2015-07-20 1. 自定义回复里面添加 微现场 跳转url OAuth2.0网页授权演示      & ...

  2. 数组的重排序方法reverse()和sort()

    js数组中存在两个可以直接用来重排序的方法:reverse()和sort(). reverse()方法比较简单,直接反转数组项的顺序: var arr = [1, 3, 2, 4, 5]; arr.r ...

  3. 关于MySQL数据库的一些操作

    启动:net start MySQL 关闭:net stop MySQL (也可以用quit:) 登录到MySQL:mysql -u root -p -u : 所要登录的用户名; -p : 告诉服务器 ...

  4. c# .net core 下的网络请求

    本文章是在VS2017的环境下,.net core 1.1版本以上. 在这期间,由于.net core 并不基于IIS,我们的过去的网络请求代码在.net core框架下,有可能会出现不兼容,报错的现 ...

  5. 刨根究底字符编码之四——EASCII及ISO 8859字符编码方案

    EASCII及ISO 8859字符编码方案   1. 计算机出现之后,从美国发展到欧洲,由于欧洲很多国家中所用到的字符中,除了基本的美国也用的那128个ASCII字符之外,还有很多衍生的拉丁字母等字符 ...

  6. Maven学习(四)

    eclipse创建Maven web项目 1.创建新项目 选择File -> New ->Project 选择New Project窗口中选择 Maven -> Maven Proj ...

  7. laravel5.2之logout注销账号无效

    问题描述:laravel5.2的框架,使用框架auth用户认证后,进行账号注销退出的时候,无法实现. 只有清除浏览器缓存,才能实现账号退出. 解决办法: 改变路由 Route::get('auth/l ...

  8. CocoaPods 安装使用教程

    CocoaPods 是比较好的第三方类库管理工具.可通过 terminal 命令进行第三方类库的安装,非常方便. 安装: Mac terminal 输入:gem install cocoapods 若 ...

  9. ORACLE中死锁的知识点总结

      死锁的概念 什么是死锁呢? 其实我们生活中也有很多类似死锁的例子. 我先举一个生活中的例子:过年回家,父亲买了一把水弹枪,儿子和侄子争抢着要先玩,谁也不让谁,拆开包装后,一个抢了枪, 一个逮住了子 ...

  10. ThinkPHP5.0版本和ThinkPHP3.2版本的区别

    5.0版本和之前版本的差异较大,本篇对熟悉3.2版本的用户给出了一些5.0的主要区别. URL和路由 5.0的URL访问不再支持普通URL模式,路由也不支持正则路由定义,而是全部改为规则路由配合变量规 ...