21 / 75
Count number of combinations that sum to target where order matters (permutations).
Example:
Input:
nums=[1,2,3], target=4Output:
7 (ways: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2, 1+3, 3+1)Common Mistakes:
- Confusing with Combination Sum (order doesn't matter vs order matters)
- Wrong loop order - target must be outer loop for permutations
- Integer overflow - should check or use long
Notes:
Order matters = permutations, so target is outer loop. Time O(target * nums.length), Space O(target). Similar to Coin Change but counting ways.
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
Count number of combinations that sum to target where order matters (permutations).
Example:
Input:
nums=[1,2,3], target=4Output:
7 (ways: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2, 1+3, 3+1)Common Mistakes:
- Confusing with Combination Sum (order doesn't matter vs order matters)
- Wrong loop order - target must be outer loop for permutations
- Integer overflow - should check or use long
Notes:
Order matters = permutations, so target is outer loop. Time O(target * nums.length), Space O(target). Similar to Coin Change but counting ways.
21/75
Combination Sum IV