Backpack VI

重复选择+不同排列+装满可能性总数

Description

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

A number in the array can be used multiple times in the combination. Different orders are counted as different combinations.

Example

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

The possible combination ways are:
[1, 1, 1, 1]
[1, 1, 2]
[1, 2, 1]
[2, 1, 1]
[2, 2]
[4]

return6

Analysis & Solution

和Backpack IV不同,那一题是唯一的排列,这一题里相同元素组合的不同排列也要计入总数。

因此,外层循环不再是元素本身,而是大小.

Dynamic Programming (Bottom Up Approach)

Combination Sum VI

Backpack VI

Search + Memoization (Top Down Approach)

Using HashMap to store results (Runtime: 4 ms, faster than 18.47 %)

Better Performance - Using dp[] array (Runtime: 0 ms, faster than 100.00%)

Last updated

Was this helpful?