Skip to content

Commit e0383fb

Browse files
committed
rob solution
1 parent bb8d394 commit e0383fb

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

house-robber/youngDaLee.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package youngDaLee
2+
3+
func rob(nums []int) int {
4+
if len(nums) == 0 {
5+
return 0
6+
}
7+
if len(nums) == 1 {
8+
return nums[0]
9+
}
10+
11+
dp := make([]int, len(nums))
12+
dp[0] = nums[0]
13+
dp[1] = max(nums[0], nums[1])
14+
15+
for i := 2; i < len(nums); i++ {
16+
dp[i] = max(dp[i-1], dp[i-2]+nums[i])
17+
}
18+
19+
return dp[len(nums)-1]
20+
}
21+
22+
func max(a, b int) int {
23+
if a > b {
24+
return a
25+
}
26+
return b
27+
}

0 commit comments

Comments
 (0)