From fe2362a97e0dd56ce76c8550c3d9007d53f779d3 Mon Sep 17 00:00:00 2001 From: baici1 <249337001@qq.com> Date: Wed, 3 Nov 2021 10:37:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A042=E6=8E=A5=E9=9B=A8=E6=B0=B4?= =?UTF-8?q?=20go=E7=89=88=E6=9C=AC=E7=9A=84=E5=8A=A8=E6=80=81=E8=A7=84?= =?UTF-8?q?=E5=88=92=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\346\216\245\351\233\250\346\260\264.md" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index 9b26bc6b01..ca2c99333b 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -604,7 +604,48 @@ func trap(height []int) int { } ``` +动态规划解法: + +```go +func trap(height []int) int { + sum:=0 + n:=len(height) + lh:=make([]int,n) + rh:=make([]int,n) + lh[0]=height[0] + rh[n-1]=height[n-1] + for i:=1;i=0;i--{ + rh[i]=max(rh[i+1],height[i]) + } + for i:=1;i0{ + sum+=h + } + } + return sum +} +func max(a,b int)int{ + if a>b{ + return a + } + return b +} +func min(a,b int)int{ + if a