LeetCode 377 · Medium
Combination Sum IV Explained (LeetCode 377)
Updated Nov 28, 2026 · 3 min read
Let ways[t] be the number of ordered sequences that add up to t, with ways[0] = 1 for the empty sequence. Every sequence ending at t has some last number n, so ways[t] is the sum of ways[t - n] for every n in nums with n <= t. Fill the table from 1 up to target; for nums [1, 2, 3] and target 4 it reads 1, 1, 2, 4, 7, in O(target times len(nums)) time.

Key points
- ●Order matters: 1 then 3 and 3 then 1 are two different ways.
- ●Group every sequence by its last number, and the groups never overlap.
- ●ways[0] = 1, then ways[t] = sum of ways[t - n] for each n <= t.
- ●For [1, 2, 3] and target 4 the table is 1, 1, 2, 4, 7.
- ●O(target x len(nums)) time, O(target) space.

The riddle: 4 stairs, hops of 1, 2 or 3
Zoe is climbing 4 stairs. Each hop covers 1, 2 or 3 stairs, and the order of hops counts: 1 then 3 is a different climb from 3 then 1. Her coach says there are 3 ways up. Zoe says 7.
Zoe is right, and that's LeetCode 377, Combination Sum IV. You get a list of distinct positive numbers and a target, and you count how many ordered sequences of those numbers add up to the target. You can reuse numbers as often as you like.
The seven climbs are 1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2, 1+3 and 3+1.
The slow way: list every sequence
You could build every sequence with recursion and count the ones that land on the target. It works for 4, but the number of sequences grows exponentially with the target. With a target of 1,000 you'd never finish. Worse, you keep rebuilding the same tails: every climb that reaches stair 2 then repeats the same work for the last two stairs.
The idea: ask what the last hop was
Instead of listing climbs, Zoe asks one question: what was my last hop?
- Last hop 1: she came from stair 3.
- Last hop 2: she came from stair 2.
- Last hop 3: she came from stair 1.
Every climb to stair 4 ends in exactly one of those hops, so the three groups never overlap and together cover everything. That gives the rule:
ways[4] = ways[3] + ways[2] + ways[1]
In general, ways[t] is the sum of ways[t - n] for every number n that fits (n <= t). The base case is ways[0] = 1: there's one way to be on the ground, which is to do nothing.
Fill the table bottom up
| t | last hop 1 | last hop 2 | last hop 3 | ways[t] |
|---|---|---|---|---|
| 0 | 1 | |||
| 1 | ways[0] = 1 | 1 | ||
| 2 | ways[1] = 1 | ways[0] = 1 | 2 | |
| 3 | ways[2] = 2 | ways[1] = 1 | ways[0] = 1 | 4 |
| 4 | ways[3] = 4 | ways[2] = 2 | ways[1] = 1 | 7 |
Step 4 of the visual sheet above fills the same table, and Step 6 dry-runs the last row.
The code in Python
def combinationSum4(nums, target):
ways = [1] + [0] * target
for t in range(1, target + 1):
for n in nums:
if n <= t:
ways[t] += ways[t - n]
return ways[target]
On nums [1, 2, 3] and target 4 this returns 7. With nums [9] and target 3, nothing ever fits, so every entry after ways[0] stays 0 and the answer is 0.
Complexity.
- Time: O(target x len(nums)). Four targets times three hops is 12 steps here.
- Space: O(target) for the ways list.
From exponential to 12 additions by storing one count per stair.
Cousins and follow-ups
If nums were just [1, 2], this would be Climbing Stairs: ways[t] = ways[t - 1] + ways[t - 2]. Combination Sum IV is the same idea with any set of hop sizes. Once you see "count the ways, and the order of steps matters", the last-step question is usually the way in.
The follow-up: negative numbers. LeetCode asks what changes if negative numbers were allowed. With 1 and -1 in the list, you could add 1, subtract 1, and repeat forever, so there would be infinitely many sequences that reach any target. To make the question answerable again, you'd have to cap how long a sequence can be.
The trap: loop order
Swap the loops, numbers outside and targets inside, and you get a different answer. That version counts each set of numbers once, ignoring order, which is Coin Change II. For [1, 2, 3] and target 4 it gives 4 instead of 7. Keep the target loop outside whenever order counts.
Frequently asked questions
Why is Combination Sum IV counting permutations, not combinations?
Because the problem counts different orders as different answers. Despite the name, 1 + 3 and 3 + 1 are two separate ways, which is why the answer for [1, 2, 3] and target 4 is 7, not 4.
Why does ways[0] start at 1?
There is exactly one way to make 0: pick nothing. That empty sequence is what every real sequence grows from, so ways[1] = ways[0] = 1 when nums contains 1.
What is the difference between Combination Sum IV and Coin Change II?
The loop order. Putting the target loop outside and the numbers loop inside counts every order (Combination Sum IV). Putting the coins loop outside counts each set of coins once (Coin Change II).
What is the time complexity of Combination Sum IV?
O(target x len(nums)) time, since each target tries each number once, and O(target) space for the ways array. For target 4 and three numbers that's 12 steps.