Minimize Car Removals for a Two-Car Bridge
BnsfSoftware Engineer · Online AssessmentLast updated
Determine the minimum number of cars to remove from a queue so that no single retained car nor any adjacent retained pair exceeds a bridge's weight limit.
A freight rail company operates a one-way bridge that can support at most two train cars simultaneously. To ensure bridge safety, two conditions must be met:
- Every individual car crossing the bridge must have a weight less than or equal to
limit. - Every adjacent pair of cars crossing the bridge together must have a combined weight less than or equal to
limit.
You are given an array of positive integers weights, representing the weights of cars in the order they arrive at the bridge, and an integer limit representing the maximum weight capacity of the bridge.
You may remove cars from anywhere in the queue, but you cannot reorder the remaining cars. Return the minimum number of cars that must be removed so that the retained cars safely cross the bridge.
Function Signature
def min_car_removals(weights: list[int], limit: int) -> int:
pass
Input and Output Format
- Input:
weights: A list of integers whereweights[i]is the weight of thei-th car.limit: An integer representing the bridge capacity.
- Output: An integer denoting the minimum number of cars removed.
Constraints
1 <= len(weights) <= 200,0001 <= weights[i] <= 10^91 <= limit <= 10^9
Examples
Example 1
weights = [5, 6, 2, 3, 7]
limit = 8
Output: 2
Explanation:
- If we remove cars with weights
6and7, the retained queue is[5, 2, 3]. - Each individual weight is
<= 8(5 <= 8,2 <= 8,3 <= 8). - Each adjacent pair sum is
<= 8(5 + 2 = 7 <= 8,2 + 3 = 5 <= 8). - The minimum number of removals is
5 - 3 = 2.
Example 2
weights = [9, 10, 4, 5, 2]
limit = 7
Output: 3
Explanation:
- Cars
9and10individually exceedlimitand must be removed. - From
[4, 5, 2], keeping both4and5is invalid because4 + 5 = 9 > 7. - We can keep
[4, 2](or[5, 2]), which has length2. - Total cars removed:
5 - 2 = 3.
Edge Cases
- All cars exceed
limit: all cars must be removed; output islen(weights). - All adjacent pairs already satisfy
w[i] + w[i+1] <= limit:0removals needed. - Only one car can be safely kept across the entire sequence.
Hints
Hint 1
Minimizing the number of cars removed is equivalent to finding the longest valid subsequence of cars.
Hint 2
For any car of weight w, a valid preceding car in the subsequence must have a weight at most limit - w.
Hint 3
Process cars from left to right using dynamic programming optimized with coordinate compression and a Fenwick tree (Binary Indexed Tree) or Segment Tree.
Follow-up questions
- How would you modify the solution to return the actual sequence of retained car indices rather than just the count?
- How would the problem change if the bridge could hold up to three cars simultaneously instead of two?
- If the cars could be rearranged in any order instead of preserving their initial arrival order, what would be the optimal strategy?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.