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. 20170515-20170523学习计划---学习java(1)

    背景: 组内搞了一个读书会,我是负责人,我们学习的主题是java,为了更好服务读书会,所以安排一下学习计划,到时候在读书会上做分享. 这是第二次读书会,第一次讲了java的一些基本知识,这次只要学习对 ...

  2. 搜索技术---solr

    solr 企业站内搜索技术选型 在一些大型门户网站.电子商务网站等都需要站内搜索功能,使用传统的数据库查询方式实现搜索无法满足一些高级的搜索需求,比如:搜索速度要快.搜索结果按相关度排序.搜索内容格式 ...

  3. 来自一位工作一年多的猿的分享 ------- ioc

    这是我第一次写东西记录吧:还记得小学老师教的写文章要有"开篇 起伏 高潮...高潮 结束",反正我不会用上: 最近在研究IOC这玩意:百度了很多关于这方面的帖子:关于IOC和DI的 ...

  4. 1254 Flip and Shift

    这题是目的是把黑球和白球分开连续放,事实上只要把其中一种颜色分好在一边就可以,可以绕一个球转即是第n个球可以放在n-2或者n+2上,因为这是个环,所以只需要把黑球或者白球连续放好就可以,当一共有奇数个 ...

  5. Sqoop简介及安装

    Hadoop业务的大致开发流程以及Sqoop在业务中的地位: Sqoop概念 Sqoop可以理解为[SQL–to–Hadoop],正如名字所示,Sqoop是一个用来将关系型数据库和Hadoop中的数据 ...

  6. springboot 集成shiro

    首先看下shiro configuration 的配置,重要部分用红色标出了 package cn.xiaojf.today.shiro.configuration; import at.pollux ...

  7. [rctf](web)rcdn 解题分析,知识点总结

    比赛平台关闭了,没有截图,见谅.   解题思路流程: 分析网站结构,看源码,元素审计.发现以下信息. 要得到flag要获得一个pro cdn pro 子域名长度为3到6个字符 存在一个提交ticke页 ...

  8. js实现分页

    <html> <head> <meta charset='utf-8'> <script type="text/javascript" s ...

  9. 一篇%3CDIV%20style%3D%22FONT-SIZE%

    %3CDIV%20style%3D%22FONT-SIZE%3A%2016px%22%3E1%EF%BC%8C%E6%88%91%E4%BB%A5%E4%B8%BA%E7%BB%88%E6%9C%89 ...

  10. Common.Logging源码解析一

    Common.Logging是Apache下的一个开源日志接口组件,主要用于切换不同的日志库,因为当前流行的日志库有很多向log4j.log4net(log4j的.net版本)等等,所以为了能灵活的切 ...