LeetCode 42. Trapping Rain Water

來源:互聯網
上載者:User

標籤:

首先說說最初的但是不正確的思路,stk存值,初始化為輸入數組的第一位。從左往右讀入數組每一位,如果height[i]>stk.top(),說明stk.top()無法蓄水,pop掉,替換為height[i];如果height[i]<=stk.top(),那麼從i開始遍曆,直到找到大於等於stk.top()的height[i]或者直到最後一位,找的時候維護變數tmp存蓄水的量,但是如果沒有找到大於等於stk.top()的height[i]的話,實際上是無法蓄水的,也就是tmp是無意義的(因此要另開變數tmp而不是直接加給res),這種情況下stk要pop,然後push進原top值的下一個高度,所以還要開一個變數top_pos,存放stk.top()在數組中的位置;如果能夠找到大於等於stk.top()的height[i]的話,res+=tmp,stk要pop,push進當前大於等於stk.top()的height[i]。

其實不用開stack,stk應該全程只有一個資料,因為看到tag是stack所以開了一個,實際上int變數即可。

 1 class Solution { 2 public: 3     int trap(vector<int>& height) { 4         int size = height.size(); 5         if(size == 0) return 0; 6         int i = 0, top_pos = 0; 7         int res = 0; 8         stack<int> stk; 9         stk.push(height[i++]);10         for(; i < size; ++i){11             if(height[i] > stk.top()){12                 stk.pop();13                 stk.push(height[i]);14                 top_pos = i;15             }16             else{17                 int tmp = 0;18                 while(i <= size - 1 && height[i] < stk.top()){19                     tmp += stk.top() - height[i];20                     ++i;21                 }22                 if(i == size) i = size - 1;23                 if(height[i] < stk.top()){24                     stk.pop();25                     i = ++top_pos;26                     stk.push(height[i]);27                 }28                 else{29                     res += tmp;30                     stk.pop();31                     stk.push(height[i]);32                     top_pos = i;33                 }34             }35         }36         return res;37     }38 };

這種演算法140/315 test cases passed.

Input:[4,2,3]Output:0Expected:1  看了別人的演算法,不用stack純邏輯解決,說明一下。water的加部分:從左向右遍曆,water+=之前highest存目前最大值(不包括height[i]),也即只考慮當前高度和這之前最高高度之間的蓄水量。water的減部分:從右向左遍曆,water-=之前highest存目前最大值(包括height[i]),如果目前最大值<總體最大值,說明目前的高度在最高高度的右邊,有一部分水無法蓄住,這部分無法蓄住的水量為總體最大值-目前最大值;如果目前最大值>=總體最大值,說明目前的高度在最高高度的左邊或是就是最高高度,而在加部分中,蓄水量只和該高度左邊的最大值有關,因此之前加的蓄水量是合理的,就不需要減去什麼了。
 1 class Solution { 2 public: 3 int trap(vector<int>& height) { 4     int sz=height.size(), highest=0, water=0; 5     //from left to right, only consider the trap‘s left elevation 6     for(int i=0; i<sz; i++){ 7         if(height[i]<highest) water+=highest-height[i]; 8         highest=max(highest, height[i]); 9     }10 11     int prehighest=highest;12     highest=0;13     //from right to left, only consider the trap‘s right elevation, subtract the surplus water14     for(int i=sz-1; i>=0; i--){15         highest=max(height[i], highest);16         if(highest<prehighest) water-=prehighest-highest;17     }18     return water;19 }20 };

 

還有stack演算法未看,留坑。

LeetCode 42. Trapping Rain Water

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.