func rob(nums []int) int {
if len(nums) == 1 {
return nums[0]
}
dp:=make(map[int]int,len(nums)+1)
var robme func(nums []int, cur int) int
robme = func(nums []int, cur int) int {
if cur >= len(nums) {
return 0
}
if val,ok:= dp[cur];ok{
return val
}
max1 := nums[cur] + robme(nums, cur+2)
max2 := robme(nums, cur+1)
dp[cur] = max(max1, max2)
return dp[cur]
}
// Rob the first house to the second-to-last house
max1 := robme(nums[:len(nums)-1], 0)
// Rob the second house to the last house
max2 := robme(nums[1:], 0)
fmt.Println("%v",dp)
// Return the maximum of the two cases
return max(max1, max2)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
I first implement iterartive approch (remove dp map references ) which works fine expect large test set.
then added dp map to hadle the large test set. It fails for [1,2,3] cant point exact issue. Please help
PS: I intend on working with recursion and memoization only