All problems
0254MediumLinked List

Regroup the Bucket Line

Tracked in this browser only
Write code

Trains the technique from

LeetCode 328Odd Even Linked List

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

An aerial ropeway carries buckets in single file. Every bucket is coupled to the one behind it and the trailing bucket is coupled to nothing, so the line can only be walked from the leading bucket backwards.

The harness passes plain JSON, so the line arrives as the array loads, holding the load readings from the leading bucket to the trailing one, and your answer takes the same shape: the readings in their new order, leading bucket first, or an empty array when the ropeway is running empty.

Number the buckets by their place in the line, starting at 1 for the leading bucket. Re-couple the line so that every odd-numbered bucket comes first and every even-numbered bucket follows, with the buckets inside each of those two groups keeping the order they already had. The numbering refers to a bucket's place in the line, not to its load reading.

Do this by re-coupling the buckets you were handed rather than hanging a second line of new buckets, and hold only a fixed number of bucket references beyond the line itself while you work.

Examples

Example 1

Input
loads = [45, 12, 60, 8, 33]
Output
[45, 60, 33, 12, 8]

Buckets 1, 3 and 5 carry 45, 60 and 33, and buckets 2 and 4 carry 12 and 8, so the odd-numbered group leads in that order and the even-numbered group follows in that order.

Example 2

Input
loads = [4, 7, 6, 9]
Output
[4, 6, 7, 9]

Buckets 1 and 3 carry 4 and 6 and buckets 2 and 4 carry 7 and 9, which is the order they appear in the answer.

Example 3

Input
loads = []
Output
[]

An empty ropeway has nothing to re-couple.

Constraints

  • 0 <= loads.length <= 10^4
  • -10^6 <= loads[i] <= 10^6
  • Only a fixed number of bucket references may be held beyond the line itself

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def regroup_bucket_line(loads: list[int]) -> list[int]:
Java
public int[] regroupBucketLine(int[] loads)
September 7
Apply