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

Function rob

house_robber_198/solution.go:6–22  ·  view source on GitHub ↗

Solved alone for second time. Better solution.

(nums []int)

Source from the content-addressed store, hash-verified

4
5// Solved alone for second time. Better solution.
6func rob(nums []int) int {
7 if len(nums) == 0 {
8 return 0
9 }
10 if len(nums) == 1 {
11 return nums[0]
12 }
13
14 dp := make([]int, len(nums)+1)
15 dp[1] = nums[0]
16
17 for i := 2; i < len(dp); i++ {
18 dp[i] = int(math.Max(float64(dp[i-1]), float64(nums[i-1]+dp[i-2])))
19 }
20
21 return dp[len(dp)-1]
22}
23
24func rob0(nums []int) int {
25 if len(nums) == 0 {

Callers 1

Test_robFunction · 0.70

Calls

no outgoing calls

Tested by 1

Test_robFunction · 0.56