zoukankan      html  css  js  c++  java
  • 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: 
    

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩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;
        }
    }
    View Code
  • 相关阅读:
    微软一站式示例代码库(中文版)20110924版本, 新添加ASP.NET, Windows Base, Silverlight, WinForm等20个Sample
    Expression Blend实例中文教程系列文章汇总
    【分享】Silverlight 探秘系列课程汇总(下载)
    【分享】分享几个不错的富文本编辑器插件
    推荐 10 个优秀的 HTML5 编码工具
    SQLite数据库操作类
    每天写出好代码的5个建议
    ASP.NET中26个常用性能优化方法
    ConnectString中enlist设置的含义
    使用VS2008打开VS2010的解决方案
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9046864.html
Copyright © 2011-2022 走看看