Merged Departure Board
A small station's departure screen merges two feeds, trains and buses, into one list sorted by time. Times are minutes after midnight, so 6:05 is 365.
Departures that share a time show as rows like 365, 365, but the screen has room for one row per time. Its tiny controller cannot afford a second list, so it must clean up the one it has.
Your task
Write dedupeBoard(times). times is sorted from earliest to latest. Change it in place so it holds each distinct time exactly once, still sorted, and nothing else. Return nothing.
Do not build a second list. Overwrite values inside times, then cut off the leftover tail (in JavaScript set times.length; in Python use del times[k:], where k is how many times you kept).
dedupeBoard(times: int[]) → void (changes times in place)
Examples
times = [365, 365, 410, 455, 455, 455, 520]
[365, 410, 455, 520]
365 appears twice and 455 three times. Keep one of each: 365, 410, 455, 520. The list shrinks from 7 entries to 4.
times = [480, 495, 510]
[480, 495, 510]
No time repeats, so the list is unchanged.
Constraints
- 0 ≤ times.length ≤ 10,000
- 0 ≤ times[i] ≤ 1,439
- times is sorted in non-decreasing order.
Start with brute force
Walk the list and delete each entry equal to the one before it (splice in JavaScript, del or pop(i) in Python).
Every delete shifts all later entries left. 10,000 entries that are almost all repeats means close to 10,000 deletes, about 50 million moves. Writing each kept entry once, straight to its final spot, avoids the shifting.
Hints
0 of 4 shown- Hint 1
Look at
[365, 365, 410]. Since the list is sorted, where can a repeat of 365 be? Could one appear after 410? - Hint 2
In a sorted list, equal values sit together. A value is new exactly when it differs from the last value you kept.
- Hint 3
Picture two fingers. One reads every entry from left to right. The other marks the next free spot in the cleaned front part. The writing finger never gets ahead of the reading finger.
- Hint 4
Keep a write index
w, starting at 0. For each read indexr: ifwis 0 ortimes[r]differs fromtimes[w - 1], copy it totimes[w]and add 1 tow. At the end, shorten the list to lengthw.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Split the list into a cleaned front, times[0..w-1], holding each time once, and the unread rest. A read index r scans everything. Since the list is sorted, an entry is new exactly when it differs from the last kept one, times[w - 1].
Trace [365, 365, 410, 455, 455, 455, 520]:
- At
r= 0 the time is 365 and nothing has been kept yet, so write 365 to slot 0 and setw= 1. - At
r= 1 the time is 365, the same as the last kept time, so skip it. - At
r= 2 the time is 410, which is new, so write it to slot 1 and setw= 2. - At
r= 3 the time is 455, which is new, so write it to slot 2 and setw= 3. - At
r= 4 andr= 5 the time is 455 again, so skip both. - At
r= 6 the time is 520, which is new, so write it to slot 3 and setw= 4.
The list is now [365, 410, 455, 520, 455, 455, 520]. Cut it to length 4: [365, 410, 455, 520].
Writing never overtakes reading (w ≤ r), so you never overwrite an entry you still need.
function dedupeBoard(times) {
let w = 0; // next free slot in the cleaned front part
for (let r = 0; r < times.length; r++) {
if (w === 0 || times[r] !== times[w - 1]) {
times[w] = times[r];
w++;
}
}
times.length = w; // cut off the leftover tail
}def dedupe_board(times):
w = 0 # next free slot in the cleaned front part
for r in range(len(times)):
if w == 0 or times[r] != times[w - 1]:
times[w] = times[r]
w += 1
del times[w:] # cut off the leftover tailRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.