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:

image

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.

答案
隨便想的答案,沒想到beat 99%的答案。。

class Solution {
    public int leastBricks(List<List<Integer>> wall) {
        /* To minimize the number of bricks crossed, we want to maximize the number of bricks that end
            at certain width
            For example, 4 is where most number of bricks end at
        */
        Map<Integer, Integer> map = new HashMap<>();
        int max = 0;
        for(List<Integer> row : wall) {
            int curr_width = 0;
            for(int i = 0; i < row.size() - 1; i++) {
                curr_width += row.get(i);
                Integer lookup = map.get(curr_width);
                if(lookup == null) {
                    map.put(curr_width, 1);
                    max = Math.max(max, 1);
                }
                else {
                    map.put(curr_width, lookup + 1);
                    max = Math.max(max, lookup + 1);
                }
            }
        }
        return wall.size() - max;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,096評論 0 23
  • 仿佛這世上所有人臉孔一般模樣 閉閉眼要花費(fèi)一個(gè)世紀(jì)的漫長 喧囂吵鬧逐漸遠(yuǎn)去的天空依舊晴朗 河水在陽光下泛著暖洋洋的...
    意莫安閱讀 227評論 0 0
  • 1、消息摘要算法包含 MD,SHA,MAC三大系列。經(jīng)常用來驗(yàn)證數(shù)據(jù)的完整性。是數(shù)字簽名的核心算法 2、算法體系光...
    K1024閱讀 359評論 0 0
  • “工作是越來越忙了,每天干不完的活” “客戶是越來越難...
    她林小講閱讀 240評論 0 1
  • 高中時(shí)期的好朋友在群里發(fā)消息,聲稱一個(gè)月后她要辦結(jié)婚酒了,在南寧。還補(bǔ)充了一句:反正我已經(jīng)提前一個(gè)月告訴你們了,你...
    趙花花老師閱讀 834評論 0 50

友情鏈接更多精彩內(nèi)容