324. Deal Into K Hands
A card game uses a deck stored as a chain: each node holds a card value and a link to the next card, and head is the top card. The deck is to be dealt into k hands, but unlike normal dealing, each hand receives one consecutive block of cards from the deck, and the blocks follow the deck order from the first hand to the last.
Split the chain into exactly k consecutive parts and return them as an array of k chains, where part i is the i-th hand. The sizes of the parts must be as equal as possible: any two sizes differ by at most 1, and the hands that get an extra card are the earlier ones. If there are fewer cards than hands, some hands are empty and are returned as empty chains.
Example 1
- Input:
- head = [1,2,3,4,5,6,7,8,9,10], k = 3
- Output:
- [[1,2,3,4],[5,6,7],[8,9,10]]
- Explanation:
Ten cards for three hands: sizes 4, 3, 3. Hands: [1,2,3,4], [5,6,7], [8,9,10].
Example 2
- Input:
- head = [70,80,90], k = 5
- Output:
- [[70],[80],[90],[],[]]
- Explanation:
Only three cards for five hands: sizes 1, 1, 1, 0, 0. Hands: [70], [80], [90], [], [].
Constraints
0 ≤ chain length ≤ 105
-1000 ≤ node value ≤ 1000
1 ≤ k ≤ 105
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 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms
Expected complexity
- Time
- O(n + k)
- Space
- O(k)