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)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市阳懂,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖哥蔚,帶你破解...
    沈念sama閱讀 211,884評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件倒谷,死亡現(xiàn)場離奇詭異,居然都是意外死亡糙箍,警方通過查閱死者的電腦和手機恨锚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,347評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來倍靡,“玉大人猴伶,你說我怎么就攤上這事∷鳎” “怎么了他挎?”我有些...
    開封第一講書人閱讀 157,435評論 0 348
  • 文/不壞的土叔 我叫張陵玲躯,是天一觀的道長昧甘。 經(jīng)常有香客問我,道長垢箕,這世上最難降的妖魔是什么站辉? 我笑而不...
    開封第一講書人閱讀 56,509評論 1 284
  • 正文 為了忘掉前任呢撞,我火速辦了婚禮,結(jié)果婚禮上饰剥,老公的妹妹穿的比我還像新娘殊霞。我一直安慰自己,他們只是感情好汰蓉,可當(dāng)我...
    茶點故事閱讀 65,611評論 6 386
  • 文/花漫 我一把揭開白布绷蹲。 她就那樣靜靜地躺著,像睡著了一般顾孽。 火紅的嫁衣襯著肌膚如雪祝钢。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,837評論 1 290
  • 那天若厚,我揣著相機與錄音拦英,去河邊找鬼。 笑死测秸,一個胖子當(dāng)著我的面吹牛疤估,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播乞封,決...
    沈念sama閱讀 38,987評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼做裙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了肃晚?” 一聲冷哼從身側(cè)響起锚贱,我...
    開封第一講書人閱讀 37,730評論 0 267
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎关串,沒想到半個月后拧廊,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體监徘,經(jīng)...
    沈念sama閱讀 44,194評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,525評論 2 327
  • 正文 我和宋清朗相戀三年吧碾,在試婚紗的時候發(fā)現(xiàn)自己被綠了凰盔。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,664評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡倦春,死狀恐怖户敬,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情睁本,我是刑警寧澤尿庐,帶...
    沈念sama閱讀 34,334評論 4 330
  • 正文 年R本政府宣布,位于F島的核電站呢堰,受9級特大地震影響抄瑟,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜枉疼,卻給世界環(huán)境...
    茶點故事閱讀 39,944評論 3 313
  • 文/蒙蒙 一皮假、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧骂维,春花似錦惹资、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,764評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽哮笆。三九已至来颤,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間稠肘,已是汗流浹背福铅。 一陣腳步聲響...
    開封第一講書人閱讀 31,997評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留项阴,地道東北人滑黔。 一個月前我還...
    沈念sama閱讀 46,389評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像环揽,于是被迫代替她去往敵國和親略荡。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,554評論 2 349

推薦閱讀更多精彩內(nèi)容