Genome Fragment Transfer Costs
Minimum entry costs from one interval to every other interval
A genetics lab has DNA fragments positioned along a reference genome. Fragment i occupies the half-open interval [fragments[i][0], fragments[i][1]) and has an entry charge of tolls[i].
A molecular marker starts on fragment source, without paying that fragment's entry charge. It may transfer from its current fragment to any other fragment whose interval overlaps it over a positive length. Each transfer costs the entry charge of the destination fragment. Merely touching at an endpoint does not permit a transfer.
The marker may make any number of transfers. Charges are paid on every entry, even when revisiting a fragment.
Return an array costs in the original fragment order, where costs[i] is the minimum total charge needed to reach fragment i from source. Use -1 if fragment i cannot be reached. In particular, costs[source] is 0.
Examples
Example 1
Input: fragments = [[0,4],[3,7],[6,10],[20,22]], tolls = [9,5,2,1], source = 0 Output: [0,5,7,-1]
From fragment 0, the marker can enter fragment 1 for a charge of 5, then fragment 2 for an additional charge of 2. Fragment 3 is separated from all the others and cannot be reached. The starting fragment's charge is not paid.
Example 2
Input: fragments = [[0,2],[1,10],[1,4],[3,7],[6,12],[11,13]], tolls = [100,9,1,1,1,2], source = 0 Output: [0,9,1,2,3,5]
Fragment 1 provides a broad but expensive connection. To reach fragment 5 more cheaply, the marker can follow fragments 0, 2, 3, 4, and 5, paying charges of 1, 1, 1, and 2.
Example 3
Input: fragments = [[-3,0],[0,3],[-1,1]], tolls = [4,7,0], source = 1 Output: [4,0,0]
Fragments 0 and 1 only touch at coordinate 0, so a direct transfer is forbidden. Fragment 2 overlaps both and has no entry charge, allowing the marker to travel from fragment 1 through fragment 2 to fragment 0.
Constraints
- 1 <= fragments.length <= 100000
- tolls.length == fragments.length
- Each fragments[i] contains exactly two integers left and right with -1000000000 <= left < right <= 1000000000
- 0 <= tolls[i] <= 1000000000
- 0 <= source < fragments.length
- Fragments may be identical, nested, or supplied in any order
The intended solution takes O(n log n) time and O(n) extra space without constructing overlap edges. Total charges may exceed 32-bit integer limits, but fit exactly in JavaScript's safe integer range under these constraints.
Hints
Show hint 1Hint 1
Consider Dijkstra's algorithm without constructing the overlap graph. Every incoming transfer to a particular fragment has the same entry charge. What does that imply about its first relaxation from a settled fragment?
Show hint 2Hint 2
The union of all settled fragments is always one continuous interval. After handling the source's immediate neighbors, every undiscovered fragment lies entirely to its left or right. Sort those two groups by the endpoints nearest the union.
Follow-up questions
What an interviewer might ask once you have a working solution.
- How would you also return one minimum-charge transfer route to a requested fragment?
- Would finalizing a fragment at first discovery remain valid if transfer charges depended on both the current and destination fragments?
Practice this with an AI interviewer
Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.
Start this problem