其他分享
首页 > 其他分享> > go-接雨水

go-接雨水

作者:互联网

func CountMaxRain(t *testing.T){
    var height =[]int {0,1,0,2,1,0,1,3,2,1,2,1}
    leftMax:=make([]int,0,len(height))
    rightMax:=make([]int,len(height))
    for i:=range height{
        if i==0{
            leftMax=append(leftMax,height[i])
            continue
        }
        if height[i]>=leftMax[i-1]{
            leftMax=append(leftMax,height[i])
        }else{
            leftMax=append(leftMax,leftMax[i-1])
            continue
        }
    }

    for i:=len(height)-1;i>=1;i--{
        if i==len(height)-1{
            rightMax[i]=height[i]
            continue
        }
        if height[i]<=rightMax[i+1]{
            rightMax[i]=rightMax[i+1]
        }else{
            rightMax[i]=height[i]
            continue
        }
    }
    fmt.Println(leftMax)
    fmt.Println(rightMax)

    count:=0
    for i:=1;i<len(height)-1;i++{
        tmp:=GetMin(leftMax[i-1],rightMax[i+1])
        if tmp-height[i]>=0{
            count=count+tmp-height[i]
        }
    }
    fmt.Println(count)
}

func GetMin(a,b int) int{
if a>=b{ return b } else{ return a } }

 

https://leetcode.cn/problems/trapping-rain-water/submissions/

标签:leftMax,count,int,len,height,雨水,go,append
来源: https://www.cnblogs.com/-citywall123/p/16317808.html