Sort an Array
Real-world version: Tool Offset OrderingThe statement here is our own: the same inputs, outputs and technique.
A machine shop fills in a card for every finishing pass it runs. Each card carries one signed offset in microns: negative when the pass left the part under the nominal size, positive when it left the part over.
You are given offsets, the figures in the order the cards were filed. Return the same figures rearranged from lowest to highest. Every card keeps its own entry, so a figure filed three times has to appear three times in what you return.
Arrange the figures yourself: do not call a library sorting routine or ordering helper. Your routine must run in O(n log n) time and use as little extra room as you can manage.
Examples
Example 1
- Input
- offsets = [12, -4, 12, 0, -4]
- Output
- [-4, -4, 0, 12, 12]
Both -4 cards and both 12 cards survive, so the answer holds five figures rising from -4 to 12.
Example 2
- Input
- offsets = [7, 3, 9, 1, 8, 2, 6]
- Output
- [1, 2, 3, 6, 7, 8, 9]
The seven figures are all different, so each one lands in its own place in the rising run.
Example 3
- Input
- offsets = [42]
- Output
- [42]
A single card is already in order on its own.
Constraints
- 1 <= offsets.length <= 5 * 10^4
- -5 * 10^4 <= offsets[i] <= 5 * 10^4
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 tool_offset_ordering(offsets: list[int]) -> list[int]:public int[] toolOffsetOrdering(int[] offsets)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 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
More on the same topics
- Query Kth Smallest Trimmed NumberReal-world version: Kth Smallest Serial by Its TailMedium
- Top K Frequent ElementsReal-world version: Most Reported Calibration OffsetsMedium
- Kth Largest Element in an ArrayReal-world version: Kth Highest Daily SwingMedium
- Top K Frequent WordsReal-world version: Most Drawn Part CodesMedium