[LeetCode] 11. Container With Most Water


Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.


</br>

Solution

As we know, the volume of the water is determined by the height and the width of the container, and height is restricted by the shorter line of the container.

Suppose we start by two pointer at each side of the array, if we want to expand the area, we should not move the longer line as the height is restricted by the shorter one.

Instead, we can move the shorter line to increase the height. Since we start from two side of the array, we begin with the maximum width so we do not have to worry about the potential area increase when moving line inwards.

Consider this: we start with

[most_left, most_right]  
    The width is (n-1) - 0
    //The biggest width can be achieved

So, if we move the longer line of these two, not only will we lose the width, but also gain no increase in height. Hence, we can ignore this movement.

And there is nothing tricky left, the code is self-explained.

Java

public class Solution {
    public int maxArea(int[] height) {
        
        int min_height = 0, area = 0;
        int i = 0, j = height.length - 1;
        
        while (i < height.length && j >= 0){
        
                
                if (height[j] > height[i]){
                    area = Math.max(area, height[i] * (j-i));
                    i++;
                }
                else {
                    area = Math.max(area, height[j] * (j-i));
                    j--;
                }
            
            
        }
        return area;
    }
}

</br>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末砂豌,一起剝皮案震驚了整個(gè)濱河市吕漂,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌词身,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件冬殃,死亡現(xiàn)場(chǎng)離奇詭異腌巾,居然都是意外死亡昵济,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門窝稿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來楣富,“玉大人,你說我怎么就攤上這事伴榔∥坪” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵踪少,是天一觀的道長(zhǎng)塘安。 經(jīng)常有香客問我,道長(zhǎng)援奢,這世上最難降的妖魔是什么兼犯? 我笑而不...
    開封第一講書人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮集漾,結(jié)果婚禮上切黔,老公的妹妹穿的比我還像新娘。我一直安慰自己具篇,他們只是感情好纬霞,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著驱显,像睡著了一般诗芜。 火紅的嫁衣襯著肌膚如雪瞳抓。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,258評(píng)論 1 300
  • 那天绢陌,我揣著相機(jī)與錄音挨下,去河邊找鬼。 笑死脐湾,一個(gè)胖子當(dāng)著我的面吹牛臭笆,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播秤掌,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼愁铺,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了闻鉴?” 一聲冷哼從身側(cè)響起茵乱,我...
    開封第一講書人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎孟岛,沒想到半個(gè)月后瓶竭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡渠羞,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年斤贰,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片次询。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡荧恍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出屯吊,到底是詐尸還是另有隱情送巡,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布盒卸,位于F島的核電站骗爆,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏世落。R本人自食惡果不足惜淮腾,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望屉佳。 院中可真熱鬧谷朝,春花似錦、人聲如沸武花。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)体箕。三九已至专钉,卻和暖如春挑童,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背跃须。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工站叼, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人菇民。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓尽楔,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親第练。 傳聞我的和親對(duì)象是個(gè)殘疾皇子阔馋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354

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

  • 今天和客戶談了一下,她說了很多塞蹭,尤其是關(guān)于錢方面怔球,沒有錢,在最關(guān)鍵的時(shí)候浮还,是會(huì)氣死自己的,有好幾次闽巩,都會(huì)很氣钧舌,但是...
    海豚的世界_0820閱讀 461評(píng)論 0 0
  • 記得那天去的時(shí)候,大家臉上都面無表情涎跨,平平淡淡洼冻,但我知道還是有些許激動(dòng)的。終于可以參觀到731部隊(duì)的遺址隅很,在控訴侵...
    歐幾得里閱讀 135評(píng)論 1 5
  • 我見過的手?jǐn)?shù)不勝數(shù)撞牢,大手,小手叔营,皮膚細(xì)膩的手屋彪,皮膚粗糙的手,溫暖的手绒尊,冰涼的手........ 媽媽的手是靈巧的手...
    辣媽趙十八閱讀 184評(píng)論 2 2