Count Tile-Filling Combinations for a Target Length
Dynamic ProgrammingAuto-graded · 26 test casesLast updated
Tests unbounded-knapsack DP by counting unordered multisets of tile widths, drawn from a given list, that sum exactly to a target corridor length.
Problem Statement
You are tiling a narrow corridor of integer length corridor_len. A list tile_sizes gives the available tile widths. Every tile width in the list may be used an unlimited number of times.
Two fillings are considered the same combination if and only if they use exactly the same multiset of tile widths; the physical order the tiles are laid down does not matter. For example, placing widths [2, 1] and [1, 2] is one combination, not two.
Write a function that returns the total number of distinct combinations of tile widths from tile_sizes that sum exactly to corridor_len. Return 0 if no such combination exists.
Parameters
| Name | Type | Description |
|---|---|---|
corridor_len | int | Total length to fill. |
tile_sizes | List[int] | Available tile widths, one entry per tile type. |
How duplicate entries in tile_sizes are handled
If the same width appears more than once in tile_sizes, treat each occurrence as a separate pass over the DP table — do not deduplicate the list before processing. This matches the expected outputs in the test cases below.
Constraints
1 <= len(tile_sizes) <= 3001 <= tile_sizes[i] <= 50000 <= corridor_len <= 5000- Each width in
tile_sizesmay be reused an unlimited number of times within a single combination. - Order within a combination is irrelevant; only the multiset of widths matters.
Edge Cases
- If
corridor_lenis0, return1. There is exactly one valid combination: the empty selection (no tiles placed). - If
corridor_lenis greater than0and no combination of the given widths sums to it, return0. - Any tile width that exceeds
corridor_lencannot contribute to any valid combination and is effectively skipped.
Examples
Example 1
Input: count_tile_combinations(5, [1, 2, 5])
Output: 4
Example 2
Input: count_tile_combinations(3, [2])
Output: 0
Example 3
Input: count_tile_combinations(0, [1, 2, 5])
Output: 1
Example 4
Input: count_tile_combinations(10, [2, 5, 3, 6])
Output: 5
Hints
Hint 1
Define dp[i] as the number of distinct tile multisets summing to length i, built from the widths seen so far. Pin down dp[0] before worrying about the rest.
Hint 2
To ensure a multiset is counted exactly once, tile widths must be the outer loop. Think about what goes wrong if you put the target lengths on the outside instead.
Hint 3
Inside each tile's pass, sweep i upward from that tile's width to corridor_len. When you read dp[i - tile] during this sweep, it has already been updated by the current tile — what does that imply about how many times the current width can appear in a combination?
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Defining the DP State
Let dp[i] be the number of distinct tile multisets — considering only the widths processed so far — whose total equals i.
- Base case:
dp[0] = 1. There is exactly one way to cover a corridor of length zero: pick no tiles. All other entries start at0. - Answer:
dp[corridor_len]after every entry intile_sizeshas been processed.
Transition
For each tile width t, sweep i from t up to corridor_len:
dp[i] += dp[i - t]
Before this update, dp[i] holds the count of combinations that include no copy of t. The value dp[i - t] counts combinations of length i - t that may already contain zero or more copies of t, because dp[i - t] was updated earlier in this same left-to-right pass. Appending one more copy of t to each of those gives every multiset of length i containing at least one t. The two groups are disjoint and together cover every possibility, so the recurrence is exact.
Why the Loop Order Prevents Double-Counting
Tile widths are the outer loop. Before width t is introduced, dp already encodes all combinations built from every earlier width. A multiset like {2, 2, 3} is assembled only when 2 is processed in the outer loop (contributing two copies) and 3 is a later entry — never the reverse — so it is counted exactly once.
If the loops were swapped (lengths outside, tiles inside), dp[i] would count every ordering of tiles as a separate combination, giving a permutation count instead of a combination count. For corridor_len = 3 and widths [1, 2] that yields 3 instead of the correct 2.
Handling Duplicate Entries
Each occurrence of a width in tile_sizes triggers its own full ascending sweep. This is intentional and matches the expected test-case outputs. Deduplicating the list first would produce different results.
Correctness on Edge Cases
corridor_len = 0:dp[0] = 1is returned immediately without any loop body executing.- Empty
tile_sizes: the outer loop never runs;dp[0]stays1and everything else stays0. - Widths exceeding
corridor_len:range(t, corridor_len + 1)is empty, so those widths are silently skipped.
Complexity
- Time: O(n · corridor_len), where n =
len(tile_sizes). Worst case is 300 × 5000 = 1.5 million updates. - Space: O(corridor_len) for the one-dimensional array. The full two-dimensional table would require O(n · corridor_len), but rolling the width dimension into a single in-place array is safe because each row of the conceptual 2D table depends only on the row above it.
Reference Solution
from typing import List
def count_tile_combinations(corridor_len: int, tile_sizes: List[int]) -> int:
dp = [0] * (corridor_len + 1)
dp[0] = 1 # one way to cover length 0: choose no tiles
# Outer loop over tile widths — each multiset is counted exactly once.
for tile in tile_sizes:
# Ascending sweep allows unlimited reuse of the current tile width.
for i in range(tile, corridor_len + 1):
dp[i] += dp[i - tile]
return dp[corridor_len]