Advantage Shuffle
Real-world version: Slotting Machines Against The Rival BenchThe statement here is our own: the same inputs, outputs and technique.
A trade fair runs a head-to-head bench test. There is one slot per machine on each side, and rival[i] is the throughput figure of the rival machine already booked into slot i. The rival booking is published and cannot be changed. mine lists the throughput figures of our own machines, and we may book our machines into the slots in any order we like, one machine per slot.
A slot is won when the figure of our machine in it is strictly greater than the figure of the rival machine in it. Equal figures are not a win.
Return a booking, as an array whose entry i is the figure of the machine we put in slot i, that wins as many slots as possible.
Several bookings may win that same number of slots, so exactly one of them is asked for. Compare two bookings like this: take the slots in decreasing order of their rival figure, putting the earlier slot first when two rival figures are equal, read off the figure each booking put into each of those slots, and at the first place where the two readings differ the booking with the smaller figure there is the smaller booking. Return the smallest booking that wins as many slots as possible.
Examples
Example 1
- Input
- mine = [6, 3, 9], rival = [4, 4, 4]
- Output
- [3, 6, 9]
In this booking slot 1 holds our 6 and slot 2 holds our 9, and each of those clears the rival figure of 4, so two slots are won; slot 0 holds our 3, which does not clear 4. All three rival figures are equal, so the comparison reads the slots in the order 0, 1, 2, giving 3, 6, 9.
Example 2
- Input
- mine = [5, 5], rival = [5, 5]
- Output
- [5, 5]
Every figure on both benches is 5, so whichever way our machines are booked each slot pairs 5 against 5, and equal figures are not a win, so no slot is won.
Example 3
- Input
- mine = [2, 8], rival = [7, 3]
- Output
- [2, 8]
Slot 1 holds our 8 against a rival figure of 3, which is a win, and slot 0 holds our 2 against a rival figure of 7, which is not, so one slot is won. Slot 0 has the larger rival figure, so the comparison reads slot 0 first, giving 2, 8.
Constraints
- 1 <= mine.length <= 10^5
- rival.length == mine.length
- 0 <= mine[i], rival[i] <= 10^9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def slot_machines(mine: list[int], rival: list[int]) -> list[int]:public int[] slotMachines(int[] mine, int[] rival)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 18 test cases: 18 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's