LeetCode::Trapping Rain Water

來源:互聯網
上載者:User

標籤:style   blog   class   code   java   color   

1、到今天完成39題,還需要不停的加油。今天再分析下裝雨水這道題

Given n non-negative integers representing an elevation map where the width 
of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

思想:將裝滿水的面積減去沒有裝水的面積。

rain = water_sum - nowater_sum

有效地計算water_sum是本問題的關鍵。

2、一個地區的內能夠承水是因為形成了由大到小再到大的凹坑。

定義 right_max[0--n-1],用來儲存從右往左搜尋得到的最大值。如right_max[i],表示在i這個柱子,右邊的最高柱子值為right_max[i].

同理定義 left_max[0--n-1]

i柱子灌水後的地區面積為min(left_max[i], right_max[i]), 因為短板效應。

如此對0至n-1求此min(i)的和,便得到了water_sum,答案也就輕易得出了。代碼如下。

主要思想在於把雨水的問題轉化成有水無水的求差。

藉助兩個矩陣left_max, right_max 儲存中間結果,巧妙地減少了許多計算量。

 

#include "stdafx.h"#include <iostream>using namespace std;class Solution {public:    int trap(int A[], int n);};// Accepted, O(n)int Solution::trap(int A[], int n) {    // 注意極端情況的考慮哦!    if(n == 0) return 0;    int *right_max = new int[n];    int tmp_max = A[n-1];    right_max[n-1] = A[n-1];    // 沒有水時的面積    int nowater_sum = A[n-1];    for(int i = n-2; i >= 0; --i) {        if(A[i] > tmp_max)            tmp_max = A[i];        right_max[i] = tmp_max;        nowater_sum += A[i];    }    int *left_max = new int[n];    tmp_max = A[0];    left_max[0] = A[0];    for(int i = 1; i < n; ++i) {        if(A[i] > tmp_max)            tmp_max = A[i];        left_max[i] = tmp_max;    }    // 計算有水時的面積    int water_sum = 0;    for(int i = 0; i < n; ++i) {        water_sum += min(left_max[i], right_max[i]);    }    delete []right_max;    delete []left_max;    return water_sum - nowater_sum;}int _tmain(int argc, _TCHAR* argv[]){    int A[12] = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};    Solution slu;    int result = slu.trap(A, 12);    return 0;}

 

聯繫我們

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