755. Pour Water

We are given an elevation map, heights[i] representing the height of the terrain at that index. The width at each index is 1. After V units of water fall at index K, how much water is at each index?

Water first drops at index K and rests on top of the highest terrain or water at that index. Then, it flows according to the following rules:

  • If the droplet would eventually fall by moving left, then move left.
  • Otherwise, if the droplet would eventually fall by moving right, then move right.
  • Otherwise, rise at it's current position.
  • Here, "eventually fall" means that the droplet will eventually be at a lower level if it moves in that direction. Also, "level" means the height of the terrain plus any water in that column.

We can assume there's infinitely high terrain on the two sides out of bounds of the array. Also, there could not be partial water being spread out evenly on more than 1 grid block - each unit of water has to be in exactly one block.

Example 1:

Input: heights = [2,1,1,2,1,2,2], V = 4, K = 3
Output: [2,2,2,3,2,2,2]
Explanation:
#       #
#       #
##  # ###
#########
 0123456    <- index

The first drop of water lands at index K = 3:

#       #
#   w   #
##  # ###
#########
 0123456    

When moving left or right, the water can only move to the same level or a lower level.
(By level, we mean the total height of the terrain plus any water in that column.)
Since moving left will eventually make it fall, it moves left.
(A droplet "made to fall" means go to a lower height than it was at previously.)

#       #
#       #
## w# ###
#########
 0123456    

Since moving left will not make it fall, it stays in place.  The next droplet falls:

#       #
#   w   #
## w# ###
#########
 0123456  

Since the new droplet moving left will eventually make it fall, it moves left.
Notice that the droplet still preferred to move left,
even though it could move right (and moving right makes it fall quicker.)

#       #
#  w    #
## w# ###
#########
 0123456  

#       #
#       #
##ww# ###
#########
 0123456  

After those steps, the third droplet falls.
Since moving left would not eventually make it fall, it tries to move right.
Since moving right would eventually make it fall, it moves right.

#       #
#   w   #
##ww# ###
#########
 0123456  

#       #
#       #
##ww#w###
#########
 0123456  

Finally, the fourth droplet falls.
Since moving left would not eventually make it fall, it tries to move right.
Since moving right would not eventually make it fall, it stays in place:

#       #
#   w   #
##ww#w###
#########
 0123456  

The final answer is [2,2,2,3,2,2,2]:

    #    
 ####### 
 ####### 
 0123456 

Example 2:

Input: heights = [1,2,3,4], V = 2, K = 2
Output: [2,3,3,4]

Explanation:
The last droplet settles at index 1, since moving further left would not cause it to eventually fall to a lower height.

Example 3:

Input: heights = [3,1,3], V = 5, K = 1
Output: [4,4,4]

Note:

  1. heights will have length in [1, 100] and contain integers in [0, 99].
  2. V will be in range [0, 2000].
  3. K will be in range [0, heights.length - 1].

一刷
題解:用遞歸來把值傳出去箭窜,用棧應(yīng)該也能做

class Solution {
    public int[] pourWater(int[] heights, int V, int K) {
        while(V>0){
            drop(heights, K, heights[K] + 1, true, true);
            V--;
        }
        return heights;
    }
    
    private boolean drop(int[] h, int i, int j, boolean l, boolean r){
        if(l && i>0 && h[i-1] <= h[i] && drop(h, i-1, h[i], l, false)) return true;
        if(r && i<h.length-1 && h[i+1]<h[i] && drop(h, i+1, h[i], false, r)) return true;
        
        if(h[i] == j) return false;// even, stop
        h[i]++;
        return true;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末烦感,一起剝皮案震驚了整個濱河市涩维,隨后出現(xiàn)的幾起案子铜秆,更是在濱河造成了極大的恐慌蔬胯,老刑警劉巖忘衍,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異铺纽,居然都是意外死亡柬帕,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進(jìn)店門狡门,熙熙樓的掌柜王于貴愁眉苦臉地迎上來陷寝,“玉大人,你說我怎么就攤上這事其馏》锱埽” “怎么了?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵叛复,是天一觀的道長仔引。 經(jīng)常有香客問我,道長褐奥,這世上最難降的妖魔是什么咖耘? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮撬码,結(jié)果婚禮上儿倒,老公的妹妹穿的比我還像新娘。我一直安慰自己呜笑,他們只是感情好夫否,可當(dāng)我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著叫胁,像睡著了一般凰慈。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上驼鹅,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天微谓,我揣著相機(jī)與錄音,去河邊找鬼谤民。 笑死堰酿,一個胖子當(dāng)著我的面吹牛疾宏,可吹牛的內(nèi)容都是我干的张足。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼坎藐,長吁一口氣:“原來是場噩夢啊……” “哼为牍!你這毒婦竟也來了哼绑?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤碉咆,失蹤者是張志新(化名)和其女友劉穎抖韩,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體疫铜,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡茂浮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了壳咕。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片席揽。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖谓厘,靈堂內(nèi)的尸體忽然破棺而出幌羞,到底是詐尸還是另有隱情,我是刑警寧澤竟稳,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布属桦,位于F島的核電站,受9級特大地震影響他爸,放射性物質(zhì)發(fā)生泄漏聂宾。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一讲逛、第九天 我趴在偏房一處隱蔽的房頂上張望亏吝。 院中可真熱鬧,春花似錦盏混、人聲如沸蔚鸥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽止喷。三九已至,卻和暖如春混聊,著一層夾襖步出監(jiān)牢的瞬間弹谁,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工句喜, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留预愤,地道東北人。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓咳胃,卻偏偏與公主長得像植康,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子展懈,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,435評論 2 359

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