[抄题]:

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:

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

最后一条缝不算,< i < list.size() - 1

[思维问题]:

不知道怎么用数学表达:(最少)穿墙个数 = 总墙数 - (最多)缝的个数

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O(n^2) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[算法思想:递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int leastBricks(List<List<Integer>> wall) {
//cc
if (wall == null || wall.size() == 0) return 0; //ini: count = 0; map
int count = 0;
Map<Integer, Integer> map = new HashMap<>(); //for loop: max count
for (List<Integer> list : wall) {
int length = 0;
for (int i = 0; i < list.size() - 1; i++) {
length += list.get(i);
map.put(length, map.getOrDefault(length, 0) + 1);
count = Math.max(count, map.get(length));
}
} return wall.size() - count;
}
}

554. Brick Wall最少的穿墙个数的更多相关文章

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

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

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

  3. 554. Brick Wall

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

  4. [LeetCode] Brick Wall 砖头墙壁

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

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

  6. [LeetCode] 871. Minimum Number of Refueling Stops 最少的加油站个数

    A car travels from a starting position to a destination which is target miles east of the starting p ...

  7. hdu 5256 最少修改多少个数 能使原数列严格递增 (LIS)

    Problem Description我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数. 请输出最少需要修改多少 ...

  8. UVa 900 - Brick Wall Patterns

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

  9. poj2987最大权闭包(输出最少建塔个数)

    题意:      公司要裁员,每个员工被裁掉之后都会有一定的收益(正或者负),有一些员工之间有限制关系,就是裁掉谁之前必须要先裁掉另一个人,问公司的最大收益和最大收益前提下的最小裁员人数? 思路:   ...

随机推荐

  1. Error unmarshalling file:/opt/test/jboss/server/defalt/conf/bootstrap.xml

    启动命令:#/usr/local/jboss/bin/run.sh -b 0.0.0.0 -c defalt 启动的defalt写错了,应该写default.

  2. JavaSE 手写 Web 服务器(一)

    原文地址:JavaSE 手写 Web 服务器(一) 博客地址:http://www.extlight.com 一.背景 某日,在 Java 技术群中看到网友讨论 tomcat 容器相关内容,然后想到自 ...

  3. Android adb push 和 adb pull

    将电脑 D 盘 libreference-ril.so 文件拷贝到安卓设备的 /system/lib 目录下 $ adb remount $ adb root $ adb push D:\libref ...

  4. CMD中文显示为乱码

    中文显示为乱码 临时解决方案: 在 CMD 中运行 chcp 936. 永久解决方案: 打开不正常的 CMD 或命令提示符窗口后,单击窗口左上角的图标,选择弹出的菜单中的“默认值”,打开如下图的对话框 ...

  5. 用js实现屏蔽F12、鼠标右击、鼠标左击

    function window.onhelp(){return false} //屏蔽F1帮助 document.onkeydown = function(){ if(window.event &am ...

  6. STL 练习

    makefile ------------------------------------- %.o : %.cpp g++ -g -c $< -o $@ all: t t2 rmXX % : ...

  7. 20165226 实验三 敏捷开发与XP实践

    实验三 敏捷开发与XP实践 实验目的 一.安装alibaba 插件,解决代码中的规范问题.在IDEA中使用工具(Code->Reformate Code)格式化代码,研究一下Code菜单,比较功 ...

  8. 1103 Integer Factorization

    题意:给出一个正整数N,以及k,p,要求把N分解成k个因式,即N=n1^p + n2^p + ... + nk^p.要求n1,n2,...按降序排列,若有多个解,则选择n1+n2+...+nk最大的, ...

  9. 详解AJAX核心中的XMLHttpRequest对象

    转自:http://developer.51cto.com/art/200904/119577.htm XMLHttpRequest 对象是AJAX功能的核心,要开发AJAX程序必须从了解XMLHtt ...

  10. 示例文件下载demo

    页面: 后台: package com.js.ai.modules.shwindow.util; import java.io.BufferedInputStream; import java.io. ...