Probability of Adjacent Car Groups in a Random Parking Row
Dynamic ProgrammingAuto-graded · 24 test casesLast updated
Count placements of k items in n slots with at least m maximal runs of length 2 or more, using a counting DP or combinatorics, and return the exact probability.
Random Parking Row
A parking row has n spaces in a line. Exactly k cars are parked in it, at most one per space. Every way of choosing which k of the n spaces are occupied is equally likely. Only the set of occupied spaces matters, and the cars are not labeled.
A group is a maximal block of consecutive occupied spaces that contains at least 2 spaces. "Maximal" means the block cannot be extended on either side. For example, with n = 5:
11100has exactly one group, of length 3. It does not count as two overlapping pairs.10101has no groups.11011has two groups.
Write a function that takes n, k and m, and returns the probability that a random arrangement contains at least m groups.
This is the exact upper-tail probability of the group count under uniform random placement. A p-value for observing m groups would be defined the same way.
Details
- The result is the number of arrangements with at least
mgroups, divided by the total number of arrangements,C(n, k). - If
m <= 0, every arrangement qualifies and the answer is1.0. - If
mis larger than any arrangement can reach, the answer is0. - Return a float. A value of
0is acceptable when the probability is zero.
Constraints
1 <= k < nnandkare small enough that an exact count is feasible (for examplenup to a few dozen).mis an integer, normallym >= 1.
Examples
Example 1
Input: adjacent_group_probability(3, 2, 1)
Output: 0.6666666666666666
Example 2
Input: adjacent_group_probability(5, 2, 1)
Output: 0.4
Example 3
Input: adjacent_group_probability(5, 3, 1)
Output: 0.9
Example 4
Input: adjacent_group_probability(5, 3, 2)
Output: 0
Hints
Hint 1
Each arrangement is a binary string of length n with exactly k ones. The answer is a ratio of counts, so count the favorable strings exactly instead of simulating.
Hint 2
Scan the row left to right. Track how many cars have been placed, how many groups have been formed so far, and whether the current run of occupied spaces has length 0, 1, or 2 or more.
Hint 3
A new group is created exactly when a car is placed right after a run of length 1, which makes the run length 2. Placing more cars into a run that is already 2 or longer adds no new group. Divide the final count by C(n, k).
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Every arrangement is a binary string of length n with exactly k ones, and all C(n, k) strings are equally likely. The answer is therefore
(number of strings with at least m groups) / C(n, k).
To count the favorable strings, use a left-to-right DP over the spaces. The state is:
placed: how many cars have been parked so far,groups: how many groups have been formed so far,run: the length of the current block of occupied spaces, capped at 2 (so 0, 1, or "2 or more").
Transitions for each new space:
- Leave it empty. The run resets to 0. Nothing else changes.
- Park a car (only if
placed < k):- If
run == 0, a new run starts andrunbecomes 1. - If
run == 1, the run reaches length 2, so a new group is created.groupsgoes up by 1 andrunbecomes 2. - If
run == 2, the car extends a group that was already counted.groupsis unchanged.
- If
Why it is correct
A maximal run of length at least 2 is counted exactly once, at the moment its second car is placed. Longer runs never add more, so 111 counts as one group. Each binary string follows exactly one path through the DP, so the DP counts every string exactly once. At the end, the strings with placed == k and groups >= m are exactly the favorable ones.
When m <= 0 every string qualifies, so the result is 1. When m is too large, no state qualifies and the result is 0.
Complexity
There are O(n) steps. The state space is O(k * G * 3), where G <= k // 2 is the maximum number of groups. The time is O(n * k * k) and the space is O(k * k).
Reference solution
from math import comb
def adjacent_group_probability(n: int, k: int, m: int) -> float:
# State: (cars placed, groups so far, current run length capped at 2)
dp = {(0, 0, 0): 1}
for _ in range(n):
nxt = {}
for (placed, groups, run), ways in dp.items():
# Leave this space empty: the current run ends.
key = (placed, groups, 0)
nxt[key] = nxt.get(key, 0) + ways
# Park a car here.
if placed < k:
if run == 1:
key = (placed + 1, groups + 1, 2) # a new group begins
elif run == 2:
key = (placed + 1, groups, 2) # extends an existing group
else:
key = (placed + 1, groups, 1) # starts a fresh run
nxt[key] = nxt.get(key, 0) + ways
dp = nxt
favorable = 0
for (placed, groups, run), ways in dp.items():
if placed == k and groups >= m:
favorable += ways
return favorable / comb(n, k)