[C++] 5 lines DP concise solution


  • 0
    H
    class Solution {
    public:
        int combinationSum4(vector<int>& nums, int target) {
            vector<int> dp(target + 1, 0);
            dp[0] = 1;
            sort(nums.begin(), nums.end());
            for (int i = 1; i <= target; ++i) for (int n : nums) if (n <= i) dp[i] += dp[i - n];
            return dp[target];
        }
    };
    

Log in to reply
 

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