Problem 377: Combination Sum IV
Last updated
Was this helpful?
Last updated
Was this helpful?
这道题理论上是可以用递归来解决的,但是运行的时候发现超时。所以我们考虑用动规来解。
只要 target > nums[i],我们就可以拆分出更小的 target。
开头用 -1 来标记数组,表明没有访问过该元素。
每次 helper 结束之前要给dp[target]
赋值,不能直接返回 res,因为这是个递归方程,下一次别人还要用之前的结果。
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?
专门总结一下,这里的讲解不错