755. Pour Water

Leetcode: 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 the waterfall 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 its 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].
Solution:
class Solution {
    public int[] pourWater(int[] heights, int V, int K) {
        for (int i = 0; i < V; i++) {
            int minLeft = findLeftMin(K, heights);
            if (minLeft != K) {
                heights[minLeft]++;
            } else {
                int rightMin = findRightMin(K, heights);
                if (rightMin != K) {
                    heights[rightMin]++;
                } else {
                    heights[K]++;
                }
            }
        }
        return heights;
    }

    private int findRightMin(int k, int[] heights) {
        int index = k;
        for (int i = k + 1; i < heights.length; i++) {
            if (heights[i] < heights[index]) {
                index = i;
            } else if (heights[i] > heights[index]) {
                break;
            }
        }
        return index;
    }

    private int findLeftMin(int k, int[] heights) {
        int index = k;
        for (int i = k - 1; i >= 0; i--) {
            if (heights[i] < heights[index]) {
                index = i;
            } else if (heights[i] > heights[index]) {
                break;
            }
        }
        return index;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末吸奴,一起剝皮案震驚了整個(gè)濱河市孙乖,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖茁帽,帶你破解...
    沈念sama閱讀 216,591評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件苇羡,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡锣吼,警方通過(guò)查閱死者的電腦和手機(jī)得湘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)杖玲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)顿仇,“玉大人淘正,你說(shuō)我怎么就攤上這事摆马。” “怎么了鸿吆?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,823評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵囤采,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我惩淳,道長(zhǎng)蕉毯,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,204評(píng)論 1 292
  • 正文 為了忘掉前任思犁,我火速辦了婚禮代虾,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘激蹲。我一直安慰自己棉磨,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布学辱。 她就那樣靜靜地躺著乘瓤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪策泣。 梳的紋絲不亂的頭發(fā)上衙傀,一...
    開(kāi)封第一講書(shū)人閱讀 51,190評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音萨咕,去河邊找鬼统抬。 笑死,一個(gè)胖子當(dāng)著我的面吹牛危队,可吹牛的內(nèi)容都是我干的蓄喇。 我是一名探鬼主播,決...
    沈念sama閱讀 40,078評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼交掏,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼妆偏!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起盅弛,我...
    開(kāi)封第一講書(shū)人閱讀 38,923評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤钱骂,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后挪鹏,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體见秽,經(jīng)...
    沈念sama閱讀 45,334評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評(píng)論 2 333
  • 正文 我和宋清朗相戀三年讨盒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了解取。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,727評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡返顺,死狀恐怖禀苦,靈堂內(nèi)的尸體忽然破棺而出蔓肯,到底是詐尸還是另有隱情,我是刑警寧澤振乏,帶...
    沈念sama閱讀 35,428評(píng)論 5 343
  • 正文 年R本政府宣布蔗包,位于F島的核電站,受9級(jí)特大地震影響慧邮,放射性物質(zhì)發(fā)生泄漏调限。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評(píng)論 3 326
  • 文/蒙蒙 一误澳、第九天 我趴在偏房一處隱蔽的房頂上張望耻矮。 院中可真熱鬧,春花似錦忆谓、人聲如沸淘钟。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,672評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)米母。三九已至,卻和暖如春毡琉,著一層夾襖步出監(jiān)牢的瞬間铁瞒,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,826評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工桅滋, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留慧耍,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,734評(píng)論 2 368
  • 正文 我出身青樓丐谋,卻偏偏與公主長(zhǎng)得像芍碧,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子号俐,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評(píng)論 2 354

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