package leetcode;

/**
 * https://leetcode.com/problems/combination-sum-iv/
 */
public class Problem377 {
    public int combinationSum4(int[] nums, int target) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem377 prob = new Problem377();
        System.out.println(prob.combinationSum4(new int[]{1, 2, 3}, 4)); // 7
    }
}

package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/combination-sum-iv/
 */
public class Problem377 {
    public int combinationSum4(int[] nums, int target) {
        return combinationSum(nums, target, new HashMap<>());
    }

    private int combinationSum(int[] nums, int target, Map<Integer, Integer> memo) {
        if (target < 0) {
            return 0;
        }
        if (target == 0) {
            return 1;
        }
        if (memo.containsKey(target)) {
            return memo.get(target);
        }
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            count += combinationSum(nums, target - nums[i], memo);
        }
        memo.put(target, count);
        return count;
    }

    public static void main(String[] args) {
        Problem377 prob = new Problem377();
        System.out.println(prob.combinationSum4(new int[]{1, 2, 3}, 4)); // 7
    }
}

Solve problem 377