MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / rob0

Function rob0

house_robber_198/solution.go:24–46  ·  view source on GitHub ↗
(nums []int)

Source from the content-addressed store, hash-verified

22}
23
24func rob0(nums []int) int {
25 if len(nums) == 0 {
26 return 0
27 }
28
29 if len(nums) == 1 {
30 return nums[0]
31 }
32
33 if len(nums) == 2 {
34 return int(math.Max(float64(nums[0]), float64(nums[1])))
35 }
36
37 dp := make([]int, len(nums))
38 dp[0] = nums[0]
39 dp[1] = int(math.Max(float64(nums[0]), float64(nums[1])))
40
41 for i := 2; i < len(nums); i++ {
42 dp[i] = int(math.Max(float64(dp[i-2]+nums[i]), float64(dp[i-1])))
43 }
44
45 return dp[len(dp)-1]
46}

Callers 1

Test_robFunction · 0.85

Calls

no outgoing calls

Tested by 1

Test_robFunction · 0.68