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/
等风起的那一天,我已准备好一切