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

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/


题目地址:https://leetcode.com/problems/brick-wall/description/

题目描述:

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:

  1. The width sum of bricks in different rows are the same and won’t exceed INT_MAX.
  2. 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保存每个边缘和左边界的距离以及该位置出现的次数。那么我们只要找出某个空隙的位置出现的次数最多就好。

由于是求穿过的砖块的个数,所以需要用砖块的行数减去孔隙数。

代码如下:

class Solution(object):
def leastBricks(self, wall):
"""
:type wall: List[List[int]]
:rtype: int
"""
left_counter = collections.Counter()
count = 0
for row in wall:
left = 0
for i in range(len(row) - 1):
left += row[i]
left_counter.update([left])
count = max(count, left_counter[left])
return len(wall) - count

日期

2018 年 5 月 31 日 ———— 太阳暴晒,明天就要过儿童节了。激动

【LeetCode】554. Brick Wall 解题报告(Python)的更多相关文章

  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】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  6. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  7. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  8. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  9. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

随机推荐

  1. EXCEL-COUNTIF()统计符合区间上的值个数

    =COUNTIF(D9:D21465,"<-0.2")+COUNTIF(D9:D21465,">0.2")  #计算<-0.2或者>0. ...

  2. 21-Add Two Numbers-Leetcode

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. 『与善仁』Appium基础 — 16、APPium基础操作API

    目录 1.前置代码 2.安装和卸载APP 3.判断APP是否已安装 4.关闭APP软件和关闭驱动对象 5.发送文件到手机和获取手机中的文件 6.获取当前屏幕内元素结构(重点) 7.脚本内启动其他APP ...

  4. 使用SpringBoot实现登录注册的几个问题

    一.用户名密码都正确的情况下被登录拦截器拦截 控制台报错:org.apache.ibatis.executor.ExecutorException: A query was run and no Re ...

  5. (数据科学学习手札132)Python+Fabric实现远程服务器连接

    本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 日常工作中经常需要通过SSH连接到多台远程 ...

  6. Learning Spark中文版--第五章--加载保存数据(2)

    SequenceFiles(序列文件)   SequenceFile是Hadoop的一种由键值对小文件组成的流行的格式.SequenceFIle有同步标记,Spark可以寻找标记点,然后与记录边界重新 ...

  7. oracle异常处理——ORA-01000:超出打开游标最大数

    oracle异常处理--ORA-01000:超出打开游标最大数https://www.cnblogs.com/zhaosj/p/4309352.htmlhttps://blog.csdn.net/u0 ...

  8. Java实现单链表的增删查改及逆置打印

    //所提供的接口 LinkList.java package Struct; public interface LinkList {//判断链表为空public boolean linkListIsE ...

  9. 视频框架 Vitamio使用

    转自http://blog.csdn.net/u010181592/article/category/5893483 1.在https://github.com/yixia/VitamioBundle ...

  10. Ibatis中SqlMapClientTemplate和SqlMapClient的区别

    SqlMapClientTemplate是org.springframework.orm.ibatis下的 而SqlMapClient是ibatis的 SqlMapClientTemplate是Sql ...