1606. Deal Into Value Bands
A sorting machine receives a linked list head of integers and an array cuts of m thresholds sorted in non-decreasing order. It deals the nodes into m + 1 bins: bin 0 takes values smaller than cuts[0], bin i takes values v with cuts[i-1] <= v < cuts[i], and bin m takes values at least cuts[m-1]. Repeated cut values simply leave empty bins between them.
Relink the existing nodes into m + 1 chains, keeping the original relative order inside every bin, and return an array of the m + 1 chain heads, using null for an empty bin. Choose each bin with binary search over cuts: O((n + m) log m) time overall.
Example 1
- Input:
- head = [5,1,9,3,7], cuts = [3,6]
- Output:
- [[1],[5,3],[9,7]]
- Explanation:
Values below 3 go to bin 0, 3 to 5 to bin 1, and 6 or more to bin 2, each in original order.
Example 2
- Input:
- head = [2,2,2], cuts = [2]
- Output:
- [[],[2,2,2]]
- Explanation:
A value equal to the cut belongs to the upper bin, so bin 0 is empty and bin 1 holds all three 2s.
Constraints
0 ≤ length ≤ 105; 1 ≤ m ≤ 105; cuts sorted non-decreasing; -109 ≤ val, cuts[i] ≤ 109.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 6,000 msC++ 1,500 msJava 3,000 msJavaScript 3,000 msTypeScript 3,000 ms
Expected complexity
- Time
- O(n log m)
- Space
- O(m)