-3
/*
Description: Write a function canSum(targetSum, numbers) that takes in a
targetSum and an array of numbers as arguments.

The function should return a boolean indicating whether or not it is possible  
to generate the targetSum using numbers from the array

You may use an element of the slice as many times as needed.
Assume that all input numbers are non-negative.

*/
package main

import "fmt"

func canSum(targetSum int, numbers ...int) bool {
    if targetSum == 0 {
        return true
    }
    if targetSum < 0 {
        return false
    }
    for index := 0; index < len(numbers); index++ {
        if canSum(targetSum-numbers[index], numbers...) == true {
            return true
        }
    }
    return false
}

//var memo = map[int]bool{} //Getting wrong values due to shared function scope from package scope
func memo_canSum(targetSum int, numbers ...int) bool {
    memo := map[int]bool{} //Defeats dp func since we continue to reinitialize during recursive calls
    if _, exists := memo[targetSum]; exists {
        return memo[targetSum]
    }

    if targetSum == 0 {
        return true
    }
    if targetSum < 0 {
        return false
    }
    for index := 0; index < len(numbers); index++ {
        if memo_canSum(targetSum-numbers[index], numbers...) == true {
            memo[targetSum] = true
            return memo[targetSum]
        }
    }
    memo[targetSum] = false
    return false
}

func main() {

    //Non-Dp Solution
    fmt.Println(canSum(21, 2, 4, 8))
    fmt.Println(canSum(80, 2, 4, 8))
    fmt.Println(canSum(300, 7, 14))

    //Dp Solution
    fmt.Println(memo_canSum(21, 2, 4, 8))
    fmt.Println(memo_canSum(80, 2, 4, 8))
    fmt.Println(memo_canSum(300, 7, 14))
}

So I've ran into an issue while programming with golang, if i declare my memoized map outside of my function then all function calls share the same map and it leads to error's with the results. If i declare the map inside the function then each recursive call reinitializes the map. Is there any way to make the map a static local variable?

nipuna
  • 3,697
  • 11
  • 24
  • No. Just pass it as a parameter when you make the recursive call. – Adrian May 20 '21 at 21:08
  • yeah i was afraid of that, guess ill have to rewrite the problem in c++ since i have to fit the problem description. – Victor Calixtro May 20 '21 at 21:31
  • 1
    I don't see why... in your case, you could just use a package map and initialize it at the start of `memo_canSum`. A more resilient solution would be to have `memo_canSum` call a function which takes a map as a parameter, which does the actual work & recursion, thus keeping the signature of `memo_canSum` intact. – Adrian May 20 '21 at 21:39

1 Answers1

1

Write a different function with map parameter and do recursive in it. Initially call that function inside your memo_canSum function with declaring new map.

//var memo = map[int]bool{} //Getting wrong values due to shared function scope from package scope
func memo_canSum(targetSum int, numbers ... int) bool {
    memo := map[int]bool{} //Defeats dp func since we continue to reinitialize during recursive calls
    return momoWithMap(memo, targetSum, numbers...)
}

func momoWithMap(memo map[int]bool, targetSum int, numbers ... int) bool{
    if _, exists:=memo[targetSum]; exists{
        return memo[targetSum]
    }

    if targetSum == 0 {
        return true
    }
    if targetSum < 0{
        return false
    }
    for index:= 0; index < len(numbers); index++{
        if momoWithMap(memo, targetSum - numbers[index], numbers...) == true {
            memo[targetSum] = true
            return true
        }
    }
    memo[targetSum] = false
    return false
}
nipuna
  • 3,697
  • 11
  • 24