377. Combination Sum IV

辣鸡题目 

也没说取模啊

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

Example:

nums = [1, 2, 3]
target = 4

The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)

Note that different sequences are counted as different combinations.

Therefore the output is 7.

 

Follow up:
What if negative numbers are allowed in the given array?
How does it change the problem?
What limitation we need to add to the question to allow negative numbers?

Credits:
Special thanks to @pbrother for adding this problem and creating all test cases.

 

class Solution {
public:
    int combinationSum4(vector<int>& nums, int target) {
        int dp[20000];
        memset(dp, 0, sizeof(dp));
        dp[0] = 1;
        for (int i = 1; i <= target; i ++) {
            for (int j = 0; j < nums.size(); j ++) {
                if (nums[j] <= i) {
                    if (dp[i-nums[j]]) {
                        //printf("i=%d,j=%d,dp[i-nums[j]]=%d,dp[i]=%d\n",i,j,dp[i-nums[j]],dp[i]);
                        dp[i] = dp[i]%100000000000 + dp[i - nums[j]]%100000000000;
                    }
                }
            }
        }
        return dp[target];
    }
};

 

全部评论

相关推荐

rndguy:个人思路,抛砖引玉。 要我的话我先问清楚需求:要什么精度,什么速度,什么环境。 如果精度要求很低,平台也有点柔性的话,只需要输出pwm,然后开个中断记录各多少个脉冲,如果脉冲时间不对齐了就反馈控制电流加减就行。要求同步要求稍微高点的话可以在脉冲间做个线性插值,同步精度会高些。 但总体来说,如果直流有刷只有脉冲没有好的编码器的话很难做精准定位什么的(除非用一些电机磁路结构相关的奇技淫巧如高频注入什么的),所以要求更高就需要大量参数辨识和校准,那就慢多了。
点赞 评论 收藏
分享
许愿面试顺利的小白很...:你是我在牛客上见过最美的女孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务