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
  • 相关阅读:
    js 学习之路8:for循环
    js 学习之路7:switch/case语句的使用
    Python语法速查: 16. 时间日期处理
    初级模拟电路:4-1 BJT交流分析概述
    初级模拟电路:3-11 BJT实现电流源
    Python语法速查: 7. 函数基础
    初级模拟电路:3-10 BJT实现开关电路
    初级模拟电路:3-9 BJT三极管实现逻辑门
    Python语法速查: 6. 循环与迭代
    初级模拟电路:3-8 BJT数据规格书(直流部分)
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9046864.html
Copyright © 2011-2022 走看看