简历:The Google Resume: How to Prepare for a Career and Land a Job at Apple, Microsoft, Google, or any Top Tech Company
算法学习书籍:Introduction to Algorithms
编程珠玑:Programming Pearls (2nd Edition)
C++ 学习:The C++ Programming Language, 4th Edition
经典操作系统书籍,龙书:Operating System Concepts
创业:The Start-up of You: Adapt to the Future, Invest in Yourself, and Transform Your Career
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
Given
[0,1,0,2,1,0,1,3,2,1,2,1]
, return 6
.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.Thanks Marcos for contributing this image!
» Solve this problemFor this problem, the key point is to know how to use the number in the array to calculate the water trap.
We notice that, the water we can collect depends on the min(leftHeight[i], rightHeight[i])-A[i]. leftHeight[i] is the highest left bar of A[i]. So we can use two array to get the leftHeight and rightHeight. if min(leftHeight[i], rightHeight[i])-A[i] < 0, then res[i]=0. After we get the res[], we can just simply add every elements up.
No comments:
Post a Comment