[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 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]的更多相关文章
- [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 ...
- 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& ...
- [LeetCode] Brick Wall 砖头墙壁
There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The b ...
- [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. 砖墙 你的面前有一堵方形的.由多行砖块组成的砖墙. 这些砖块高度相同但是宽度不同.你现在要画一条自顶向下的.穿过最少砖块的垂线. 砖墙由行的列表表示. 每一行都是一个代表从左至右每块砖的宽度 ...
- Leetcode 554.砖墙
砖墙 你的面前有一堵方形的.由多行砖块组成的砖墙. 这些砖块高度相同但是宽度不同.你现在要画一条自顶向下的.穿过最少砖块的垂线. 砖墙由行的列表表示. 每一行都是一个代表从左至右每块砖的宽度的整数列表 ...
- UVa 900 - Brick Wall Patterns
题目大意:用1*2的砖头建n*2的墙,问有多少种不同的砖头排列方式?与斐波那契序列相似. #include <cstdio> #define MAXN 60 #define N 50 un ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- PL/SQL Developer使用技巧以及快捷键设置
1.类SQL PLUS窗口: File->New->Command Window,这个类似于oracle的客户端工具sql plus,但是比在cmd中的sqlplus好用多了. 2.设置关 ...
- Java学习笔记——设计模式之四.代理模式
To be, or not to be: that is the question. --<哈姆雷特> 代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问. 上代码: p ...
- RowSet的使用
ResultSet是使用Jdbc编程的人入门和常用的操作数据库的类,自 JDK1.4开始,易于使用RowSet接口被引入.RowSet 接口扩展了标准java.sql.ResultSet接口.RowS ...
- JAVA printWriter中write()和println()区别
PrintWriter 的Write()方法和println()方法有何细微的区别? 最近学习JAVA网络编程,在服务器端和客户端产生一个Socket 后, 两边各自用getIputStream()和 ...
- Linux 下安装RabbitMQ 3.6.1
1.安装erlang 依赖 yum install -y gcc gcc-c++ unixODBC-devel openssl-devel ncurses-devel 2.安装erlang ### 设 ...
- 用python实现对图像的卷积(滤波)
之前在看卷积神经网络,很好奇卷积到底是什么,最后看到了这篇文章http://blog.csdn.net/zouxy09/article/details/49080029,讲得很清楚,这篇文章中提到了对 ...
- 进军VR虚拟现实-先来全景智慧城市-有梦想的互联网创业者
随着VR的大火,越来越多的企业开始进军VR行业,不过并不是所有企业进军VR行业都是成功的,那么问题来了,VR虚拟现实行业投资怎么做才能取得成功呢?这是当下很多企业面临的一个问题,VR虚拟现实行业投资也 ...
- nmon指标
表字段分析 关键指标类型 关键指标名称 关键指标含义 SYS_SUMM CPU% cpu占有率变化情况: IO/sec IO的变化情况: AAA AIX AIX版本号: cpus CPU数量: har ...
- 开发一个Servlet示例
Servlet响应请求步骤: Servlet是一个基于Java技术的Web组件,运行在服务器端,用户利用Servlet可以很轻松地扩展Web服务器的功能,使其满足特定的应用需要.Tomcat是一个常用 ...
- CollectioView滚动到指定section的方法
项目中的需求:collectionView顶部有一个scrollView组成的标签,点击标签,让collectionView滚动到指定的行,滚动collectionView自动切换到顶部指定的标签 实 ...