1. If 39 questions are completed today, you still need to keep working on them. Today I will analyze the question 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 [,], return 6. thought: Compare the area filled with water with the area not filled with water. Rain = water_sum-nowater_sum Effective Calculation of water_sum is the key to this problem. 2. The water in a region is formed from the largest to the smallest to the largest pit. Define right_max [0 -- n-1] To save the maximum value obtained from right to left. For example, right_max [I] indicates that the highest column value on the right is right_max [I]. similarly, left_max [0 -- n-1] I is defined as min (left_max [I], right_max [I]) after the column is filled with water, because of the short board effect. In this way, the sum of min (I) is obtained for 0 to n-1, and water_sum is obtained, and the answer is easily obtained. The Code is as follows. The main idea is to convert the problem of rain into the demand for water and water. Using the two matrices left_max, right_max saves the intermediate results, cleverly reducing a lot of computing workload. Copy the Code # 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) {// consider extreme situations! If (n = 0) return 0; int * right_max = new int [n]; int tmp_max = A [n-1]; right_max [n-1] = A [n-1]; // area 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;} // calculate the area int water_sum = 0 when there is water; 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 ;}