C++ basic DP question, 3 lines is enough.

  • 0

    This is a typical dp problem. Let's crack it.

    int combinationSum4(vector<int> &nums, int target) {
        int tmp[target + 1]{1};
        for (int i = 1; i <= target; ++i)for (int n:nums)if (n <= i)tmp[i] += tmp[i - n]; //the number i consists of i-n and n
        return tmp[target];

Log in to reply

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