C# - recursive with memorization, top down


  • 0
    public int CombinationSum4(int[] nums, int target) 
    {
        return Helper(nums, target, new Dictionary<int,int>());
    }
    
    public int Helper(int[] nums, int target, Dictionary<int, int> map) 
    {
        if (target == 0) return 1;
        if (map.ContainsKey(target)) return map[target];
        
        int count = 0;
        for (int i = 0; i < nums.Length; i++)
        {
            if (nums[i] <= target)
            {
                count += Helper(nums, target - nums[i], map);
            }
        }
        map[target] = count;
        return count;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.