Ad platform~45 min2s · 256 MBtarget O(R × C × 2^C) time, O(2^C) spacebit-manipulation
An ad page is a grid of slots, R rows by C columns. Some slots are reserved, and no ad may use them. Each ad creative fills two neighbouring free slots, side by side or one above the other.
A layout fills every free slot exactly once. Your team lead asks: "How many different layouts are there?" A page can have 100 rows, and the number of layouts is huge, so give it modulo 1,000,000,007.
Write count_layouts(grid).
grid is a list of equal-length strings. . is a free slot and # is a reserved slot.0 when no layout exists.Input: grid = ["...", "..."]
Output: 3The page has 2 rows and 3 columns, all free. The three layouts:
| Layout | Ads |
|---|---|
| 1 | Three vertical ads, one in each column |
| 2 | A vertical ad in column 1, and two horizontal ads in columns 2 and 3, one in each row |
| 3 | Two horizontal ads in columns 1 and 2, one in each row, and a vertical ad in column 3 |
Input: grid = ["..", "#."]
Output: 0There are 3 free slots, and each ad covers 2, so no layout exists.
Input: grid = ["."]
Output: 0One free slot cannot be covered by an ad of two slots.
1 ≤ len(grid) ≤ 1001 ≤ len(grid[i]) ≤ 12, and all rows have the same length. or #A grid of 1,200 slots has an astronomical number of layouts, so we cannot list them. But the grid is only 12 columns wide. A summary of the cells that stick out of the finished part fits in 12 bits, which is 4,096 states.