Title Description
Preliminary analysis
Scenario 1: Before the global maximum value is not encountered
The situation only needs to save the current temporary maximum value of Tempmax, and then the water volume is tempmax-height[i].
Scenario 2: The global maximum has been traversed, and the subsequent value has a local extremum, but no more global maximum value
Scenario 3: The global maximum has been encountered, but the second extremum encountered is larger than the first extremum encountered before, then the volume of the water is the global maximum and the second encountered Extremum (that is, the second large value) between the volume
Summary of conditions 2 and 3, in the third encounter of the extremum to be compared with the sub-large value.
Scenario 4: Actually, this is the same as the situation 1.
Why is this listed here?
Because the topic requires only one iteration of the array, we are not sure that the global maximum is still encountered during a traversal, so it is particularly important to consider the relationship between the local extremum and the global maximum value.
Compared to conditions 2 and 3, the extremum to be encountered at the third time is compared with the maximum value.
We want to judge the local Extremum point in the process of one traversal, then compare the relationship between the local extremum point and the temporary maximum and the second big value, and make the corresponding action according to different situations.
Opposite
The above analysis seems to have taken the situation into account, but this has led to the spread of the problem. Especially when the stage extremum is not the maximum value is not the second big value situation cannot solve. Therefore, we should take the opposite direction and change the angle to simplify the problem.
We have been thinking about how to compare the subsequent storage volume when the maximum value is met, but it is better to reverse the calculation by setting a subscript at the end. So this has formed a solution to the problem of the core idea: set two subscript i,j from the beginning, the first comparison of the two subscript corresponding value of the size, by the corresponding value of the lower subscript first move, and record the current temporary maximum value of the underlying (such as I first move, record I_max), when encountered a new maximum value of the time , not only to the I to the local variable zeroing, but also to compare the maximum value and J corresponding value of the size relationship, and then the smaller value corresponding to the subscript continues to move, until the two subscript meet, traversed the array so far.
Program code
intVolume (int* Height,intN) {inti =0;intj = N1;intI_max = Height[i];intJ_max = Height[j];intVolume =0;Const intI_walk =1;Const intJ_walk =2;//i_max <= J_max, I go first intWho_walk = I_max <= j_max? I_walk:j_walk; while(I! = j) {if(Who_walk = = I_walk) {if(I_max > Height[i]) {volume + = i_max-height[i]; }Else{I_max = Height[i];if(I_max > J_max) {who_walk = J_walk;Continue; }} ++i; }Else if(Who_walk = = J_walk) {if(J_max > Height[j]) {volume + = j_max-height[j]; }Else{J_max = height[j];if(J_max >= I_max) {who_walk = I_walk;Continue; }}--j; } }returnVolume;}
reprint Please indicate the author Jason Ding and its provenance
Gitcafe Blog Home page (http://jasonding1354.gitcafe.io/)
GitHub Blog Home page (http://jasonding1354.github.io/)
CSDN Blog (http://blog.csdn.net/jasonding1354)
Jane Book homepage (http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)
Baidu Search jasonding1354 access to my blog homepage
"Jobs" Ali algorithm intern written test--water volume between walls