Maps app~25 min2s · 256 MBtarget O(n) time, O(n) spaceclassic twin: lc-138linked-lists · hashing
A maps app saves a route as a chain of stops. Each stop has a next pointer to the following stop, and a shortcut pointer to any other stop of the route, or to none. A shortcut can point forward, backward or at the stop itself.
The app keeps its stops in a table of slots, and a pointer is a slot number. Stops are added and moved as the user edits, so the slot order is not the route order.
A user can edit a copy of a route. The copy must be fully independent: it goes in new slots, and its pointers lead to copies, never to the original. A route can have up to 100,000 stops.
Write copy_route(head, values, after, shortcut).
n - 1, and every slot is on the route.head is the slot of the first stop, or -1 for an empty route.values[i] is the value of the stop in slot i.after[i] is the slot of the stop after it on the route, or -1 for the last stop.shortcut[i] is the slot that its shortcut points at, or -1 for none.n new stops. The copy of the stop at place k on the route (counting from 0 at head) goes in slot n + k. Each copy's next is the copy of the original's next, and each copy's shortcut is the copy of the stop that the original's shortcut points at.n to 2n - 1 as a list of 3 × n numbers. For each new slot in order, write its value, the slot of its next (or -1), and the slot of its shortcut (or -1).Every slot in the answer is n or more: a slot below n would be a pointer into the original. An empty route gives an empty list.
Input: head = 3, values = [9, 7, 5, 4, 2], after = [4, 2, -1, 0, 1], shortcut = [-1, 3, 1, 4, 2]
Output: [4, 6, 7, 9, 7, -1, 2, 8, 9, 7, 9, 5, 5, -1, 8]The route visits the slots 3, 0, 4, 1, 2, with the values 4, 9, 2, 7, 5. Their copies go in the slots 5 to 9 in that order. So the copy of slot 3 goes in slot 5, and the copy of slot 2 in slot 9.
| Step | Slot | Value | What happens |
|---|---|---|---|
| [1] | the route is built: 5 stops, from slot 3 | ||
| [2] | 3 | 4 | make a copy c3 that holds the value 4 |
| [2] | 0 | 9 | make a copy c0 that holds the value 9 |
| [2] | 4 | 2 | make a copy c4 that holds the value 2 |
| [2] | 1 | 7 | make a copy c1 that holds the value 7 |
| [2] | 2 | 5 | make a copy c2 that holds the value 5 |
| [3] | 3 | 4 | c3.next = c0, c3.shortcut = c4 |
| [3] | 0 | 9 | c0.next = c4, c0.shortcut = none |
| [3] | 4 | 2 | c4.next = c1, c4.shortcut = c2 |
| [3] | 1 | 7 | c1.next = c2, c1.shortcut = c3 |
| [3] | 2 | 5 | c2.next = none, c2.shortcut = c1 |
| [4] | c3, c0, c4, c1, c2 go in the slots 5 to 9: [4, 6, 7, 9, 7, -1, 2, 8, 9, 7, 9, 5, 5, -1, 8] |
Slot 5 holds the copy of slot 3: the value 4, next slot 6 (the copy of slot 0), and shortcut slot 7 (the copy of slot 4).
Input: head = 0, values = [8], after = [-1], shortcut = [0]
Output: [8, -1, 1]A stop whose shortcut points at itself: its copy, in slot 1, points at itself.
Input: head = -1, values = [], after = [], shortcut = []
Output: []0 ≤ n ≤ 10^5, and values, after and shortcut have the same length.0 ≤ values[i] ≤ 10^6.head is -1 when n = 0, and a slot from 0 to n - 1 otherwise.after forms one chain from head that visits every slot once. Each shortcut[i] is -1 or a slot from 0 to n - 1.Finding the copy of a shortcut's target by walking the chain from the head costs up to n hops for each stop. For 10^5 stops that is about 5 × 10^9 hops.
A table from each original stop to its copy finds the copy in one step, so the whole copy costs a few passes of 10^5 steps.