Ctrl K

Pallets After the Moves

Medium
Warehouse cells are numbered with integers. The array pallets lists the cells where pallets stand at the start; a cell may hold several pallets. Then moves happen one after another. On step i a forklift takes **all** pallets from cell from_cells[i] and puts them into cell to_cells[i]. At the moment of step i, cell from_cells[i] is guaranteed to hold at least one pallet. Return the cells that hold at least one pallet after all moves, in increasing order.
Example 1
pallets=[3, 8, 8, 12], from_cells=[8, 3, 20], to_cells=[20, 12, 4] → [4, 12]

Both pallets from cell 8 go to 20, the one from 3 goes to 12, then both from 20 go to 4. Cells 4 and 12 are occupied.

Example 2
pallets=[5, 6], from_cells=[5, 6], to_cells=[6, 5] → [5]

First everything gathers in cell 6, then all of it moves to cell 5.

Example 3
pallets=[7], from_cells=[7], to_cells=[7] → [7]

Moving into the same cell changes nothing.

Constraints
  • 1 ≤ len(pallets) ≤ 10⁵
  • 1 ≤ len(from_cells) = len(to_cells) ≤ 10⁵
  • 1 ≤ pallets[i], from_cells[i], to_cells[i] ≤ 10⁹
  • At step i, cell from_cells[i] is not empty
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCross Out the Smallest First MediumShelf Total After Orders MediumEnd of the Doubling Chain EasyClearing the Stacks Evenly EasyBusiest Meeting Room Hard
loading Python…
Input
pallets=[3, 8, 8, 12], from_cells=[8, 3, 20], to_cells=[20, 12, 4]
Expected
[4, 12]

Run checks the first 3 tests in your browser. Submit checks all 100 tests on the server, hidden ones included.