Skip to content

Commit 84d7266

Browse files
authored
Update Solution2.go
1 parent c35ccf7 commit 84d7266

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
func rob(nums []int) int {
2-
f, g := 0, 0
3-
for _, x := range nums {
4-
f, g = max(f, g), f+x
2+
n := len(nums)
3+
f := make([]int, n+1)
4+
f[1] = nums[0]
5+
for i := 2; i <= n; i++ {
6+
f[i] = max(f[i-1], f[i-2]+nums[i-1])
57
}
6-
return max(f, g)
7-
}
8+
return f[n]
9+
}

0 commit comments

Comments
 (0)