C# O(n^2) with O(n) memory


  • 0
    H
    public int CombinationSum4(int[] nums, int target) {
            int[] counts = new int [target + 1];
            counts[0] = 1;
            
            for(int i = 0; i < target; i++) {
                for(int j = 0; j < nums.Length; j++) {
                    if ((i + nums[j]) <= target) counts[i + nums[j]] += counts[i];
                }
            }
            
            return counts[target];
        }
    

Log in to reply
 

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