Practice
def carPooling(trips, capacity):
max_location = 0
for _, start, end in trips:
max_location = max(max_location, end)
diff = [0] * (max_location + 1)
for num, start, end in trips:
diff[start] += num
diff[end] -= num
current_passengers = 0
for i in range(max_location + 1):
current_passengers += diff[i]
if current_passengers > capacity:
return false
return true
print(carPooling([[2,1,5],[3,3,7]], 4))
Solution
Step 1: Build difference array
diff after processing trips: diff[1]+=2, diff[5]-=2, diff[3]+=3, diff[7]-=3Step 2: Sweep through diff to track passengers
At location 1: current_passengers=2; at 3: current_passengers=5 (2+3), which exceeds capacity=4 -> return falseFinal Answer:
Option C -> Option CQuick Check:
Passenger count exceeds capacity at location 3 [OK]
- Off-by-one in diff array indexing
- Checking capacity only at trip start
- Forgetting to decrement at trip end
from typing import List
def intervalIntersection(A: List[List[int]], B: List[List[int]]) -> List[List[int]]:
i, j = 0, 0
result = []
while i < len(A) and j < len(B):
if A[i][1] < B[j][0]:
i += 1
elif B[j][1] < A[i][0]:
j += 1
else:
start = max(A[i][0], B[j][0])
end = min(A[i][1], B[j][1])
result.append([start, end])
if A[i][1] < B[j][1]:
i += 1
else:
j += 1
return result
A = [[0,2],[5,10],[13,23],[24,25]]
B = [[1,5],[8,12],[15,24],[25,26]]
print(intervalIntersection(A, B))
What is the final output printed by this code?Solution
Step 1: Trace first intersection
Compare [0,2] and [1,5]: overlap is [1,2], add to result.Step 2: Advance pointers and find all intersections
Following pointer moves and overlaps yield [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]].Final Answer:
Option D -> Option DQuick Check:
Matches manual step-by-step intersection results [OK]
- Including intervals beyond intersection
- Merging adjacent but non-overlapping intervals
- Off-by-one in pointer increments
Solution
Step 1: Understand the problem requires tracking overlapping intervals to find minimum rooms.
Brute force checks all pairs but is inefficient; greedy by end time alone doesn't track concurrent overlaps.Step 2: Recognize that sorting by start time and using a min-heap to track earliest ending meeting allows reusing rooms optimally.
This approach efficiently manages room allocation by freeing rooms as meetings end.Final Answer:
Option A -> Option AQuick Check:
Min-heap approach is standard optimal solution for this problem [OK]
- Assuming greedy by end time alone suffices
- Thinking brute force is efficient enough
- Confusing DP for interval scheduling with room allocation
Solution
Step 1: Understand the problem requires tracking concurrent trains
The key is to find the maximum number of trains present at the station simultaneously.Step 2: Recognize that sorting events and sweeping through them tracks concurrency efficiently
By creating arrival and departure events and sorting them, we can increment or decrement the count of trains present, capturing the peak count.Final Answer:
Option B -> Option BQuick Check:
Sweep line tracks concurrency optimally [OK]
- Assuming greedy assignment without sorting works
- Trying DP which is unnecessary overhead
- Using nested loops which is inefficient
Solution
Step 1: Understand sorting criteria
Sorting intervals by start ascending and end descending ensures intervals with the same start are ordered from longest to shortest, so coverage can be detected in one pass.Step 2: Single pass coverage check
By scanning intervals in sorted order and tracking the maximum end seen, we can count intervals that are not covered by any previous interval.Final Answer:
Option B -> Option BQuick Check:
Sorting by start ascending and end descending enables O(n log n) sorting plus O(n) scanning [OK]
- Sorting only by start ascending misses coverage when starts equal
- Using nested loops causes TLE on large inputs
