Biggest Cabin Party Respecting Everyone's Companion Limits
Greedy & IntervalsAuto-graded · 23 test casesLast updated
Find the largest group size k where at least k people accept exactly k-1 companions, using interval counting with a difference array in linear time.
A group of campers wants to share one cabin. There are n campers, and camper i will only stay in a cabin that holds at least min_others[i] and at most max_others[i] other campers besides themselves.
If you put k campers in the cabin together, each of them has exactly k - 1 cabin-mates. A camper i in that group is happy only if
min_others[i] <= k - 1 <= max_others[i].
Choose any subset of campers so that every chosen camper is happy. Return the largest possible size of such a subset.
Rules
- Any subset of campers may be chosen, and the cabin is large enough for all
nof them. Only the campers' own limits matter. - Campers who are not chosen place no requirement on the group.
- If no non-empty group makes all its members happy, return
0. - Whether a size works is not necessarily monotone: a size can be impossible while both a smaller and a larger size are possible.
Input
min_others: list ofnintegers, the minimum number of cabin-mates each camper accepts.max_others: list ofnintegers, the maximum number of cabin-mates each camper accepts. It has the same length asmin_others.
Output
An integer: the largest group size that can be formed, or 0 if none exists.
Constraints
1 <= n <= 10^5, wheren = len(min_others) = len(max_others)0 <= min_others[i] <= max_others[i] <= 10^9- Aim for
O(n)time.
Examples
Example 1
Input: largest_valid_party([0, 1, 1, 2, 2], [1, 2, 2, 4, 4])
Output: 3
Example 2
Input: largest_valid_party([2, 2, 2, 0], [2, 2, 3, 0])
Output: 3
Example 3
Input: largest_valid_party([1, 2], [1, 3])
Output: 0
Hints
Hint 1
Each camper's limits on other campers can be turned into a range of acceptable group sizes. Mind the off-by-one: a group of size k means k-1 cabin-mates.
Hint 2
For a fixed size k, count how many campers accept k. Any k of those campers form a valid group, so k is achievable exactly when that count is at least k.
Hint 3
Group sizes above n are never possible, so clip each range to n and apply all range additions with a difference array and one prefix-sum sweep. Do not binary search, because feasibility is not monotone.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A camper who tolerates between min_others[i] and max_others[i] cabin-mates is happy in a group of size k exactly when min_others[i] + 1 <= k <= max_others[i] + 1. So each camper corresponds to an interval of acceptable group sizes.
For a given k, let c(k) be the number of campers whose interval contains k. Only these campers can be in a group of size k, and any k of them make a valid group. So size k is achievable if and only if c(k) >= k. The answer is the largest k in 1..n with this property, or 0 if none exists.
To compute c(k) for all k quickly, treat each interval as a "+1 on a range" update and use a difference array:
- Clip the right end to
n, since a group can never have more thannmembers. This keeps the array small even whenmax_others[i]is as large as10^9. - Skip intervals that are empty after clipping (for example,
min_others[i] >= n). - Add
+1at the start and-1just past the end. - Sweep
kfrom1ton, keeping a running prefix sum equal toc(k), and recordkwheneverc(k) >= k.
Why it is correct
- Necessary: in a valid group of size
k, every member acceptsk, so at leastkcampers coverk, givingc(k) >= k. - Sufficient: if
c(k) >= k, pick anykcampers coveringk. Each has exactlyk - 1cabin-mates, within their limits. Unchosen campers impose nothing. - Clipping is safe: sizes above
nare never feasible, so cutting intervals atndoes not changec(k)for anyk <= n. - No binary search: feasibility is not monotone. For example, with ranges that allow sizes 1 and 3 but not 2, the set of feasible sizes has gaps, so every
kmust be checked.
Complexity
- Time:
O(n), one pass to apply the updates and one pass for the prefix sums. - Space:
O(n)for the difference array.
Reference solution
from typing import List
def largest_valid_party(min_others: List[int], max_others: List[int]) -> int:
n = len(min_others)
# diff's prefix sum at k = number of campers who accept a group of size k
diff = [0] * (n + 2)
for lo, hi in zip(min_others, max_others):
start = lo + 1 # smallest acceptable group size
end = min(hi + 1, n) # largest acceptable size, capped at n
if start <= end:
diff[start] += 1
diff[end + 1] -= 1
best = 0
running = 0
for k in range(1, n + 1):
running += diff[k]
if running >= k:
best = k
return best