[leetcode] 407. Trapping Rain Water II

來源:互聯網
上載者:User

標籤:

https://leetcode.com/contest/6/problems/trapping-rain-water-ii/

看到這題,我很高興,因為我做過!哈哈!其實我現在也寫不出來,知道大概思想。

這題是google apactest 2017 round A 的第二題。https://code.google.com/codejam/contest/11274486/dashboard#s=p1

然後,簡單一次調試,就ac了。不知道這算不算作弊,做完,我就看見什麼instruction,說是可以看別人代碼,然後舉報作弊,有點怕!

分析:這題算是動態規劃吧,讀懂題意後,(其實這個題目描述的不清楚啊,圖的樣本倒是挺好),可以觀察,可以從外圈往裡圈縮,因為這個過程牆的高度是非遞減的,然後,你應該想到用優先隊列(priority_queue或者set,又是討厭的set),先把外圈所有點加入,然後取出高度最小的點,更新四周的點,注意標記這個點是否訪問過,這個過程中記錄留言板增加的高度就是最後的積水量。

哎!鹹魚也是有夢想的!

 1   int a[120][120]; 2     bool v[120][120]; 3     int dx[] = {1, -1, 0, 0}; 4     int dy[] = {0, 0, 1, -1}; 5     6 class Solution { 7 public: 8  bool in(int x, int y, int r, int c) { 9         return 0 <= x && x < r && 0 <= y && y < c;10     }11    int trapRainWater(vector<vector<int>>& h) {12         priority_queue<pair<int, pair<int, int> > >    q;13         int m = h.size();14         if(m == 0) return 0;15         int n = h[0].size();16        17         memset(a, 0, sizeof a);18         memset(v, 0, sizeof v);19         for (int i = 0; i < m; i++) {20             for (int j = 0; j < n; j++) {21                 if(i == 0 || j == 0 || i == m - 1 || j == n - 1) {22                     q.push(make_pair(-h[i][j], make_pair(i, j)));23                     a[i][j] = h[i][j];24                     v[i][j] = 1;25                 }26             }27         }28         // cout << n << " " << m << endl;29         while(q.size()) {30             pair<int, pair<int, int> > u = q.top();31             q.pop();32             int x = u.second.first;33             int y = u.second.second;34             for (int k = 0; k < 4; k++) {35                 int nx = x + dx[k];36                 int ny = y + dy[k];37                 if (in(nx, ny, m, n) && !v[nx][ny]) {38                     if (h[nx][ny] < a[x][y]) {39                         a[nx][ny] = a[x][y];40                     } else {41                         a[nx][ny] = h[nx][ny];42                     }43                     v[nx][ny] = 1;44                     q.push(make_pair(-a[nx][ny], make_pair(nx, ny)));45                 }46             }47         }48         int ans = 0;49         for (int i = 0; i < m; i++) {50             for (int j = 0; j < n; j++) {51                 ans += a[i][j] - h[i][j];52 //                printf("%d ", a[i][j]);53             }54 //            printf("\n");55          }56          return ans;57     }58 };

 

[leetcode] 407. Trapping Rain Water II

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.