Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int trap(int* height, int heightSize){
- int *max=(int*)malloc(heightSize*sizeof(int));
- int *maxpos=(int*)malloc(heightSize*sizeof(int));
- int mx=-1;
- int mxpos=-1;
- int ans=0;
- for(int i=heightSize-1;i>=0;i--){
- if(height[i]>mx){
- mx=height[i];
- max[i]=mx;
- mxpos=i;
- maxpos[i]=i;
- }else{
- max[i]=mx;
- maxpos[i]=mxpos;
- }
- }
- for (int i=0;i<heightSize;i++){
- //printf("%d %d\n",max[i],maxpos[i]);
- }
- int i=0;
- int j=0;
- while(i<heightSize-1){
- mx=height[i];
- //printf("mx=%d\n",mx);
- if(max[i+1]>=mx){
- i++;
- while(height[i]<mx&&i<heightSize-1){
- //printf("%d凹槽ans+=%d,mx=%d\n",i,-height[i]+mx,mx);
- ans+=mx-height[i];
- i++;
- }
- }else{
- j=i;
- //printf("hitj\n");
- while(i<maxpos[j+1]&&i<heightSize-1){
- i++;
- //printf("ans+=%d\n",max[j+1]-height[i]);
- ans+=max[j+1]-height[i];
- }
- }
- }
- int m=0;
- for (int j=0;j<30;j++){
- m-=1000;
- for (int n=0;n<1000;n++){
- m+=1;
- }
- }
- printf("%d",m);
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement