Editor

One Truck per Town

#306 · Greedy · Intermediate · 70 XP · about 14 min

Parcels travel along a conveyor belt in a fixed order, and labels[i] is the letter of the town that parcel i is going to. The belt is cut into groups of neighbouring parcels, and each group is loaded onto its own truck. Every town must be served by a single truck, so all the parcels for one town must end up in the same group.

Write truck_loads(labels) that cuts the belt into as many groups as possible and returns a list of the group sizes, from the front of the belt to the back.

Examples

truck_loads('abacdcd')      # returns [3, 4]
truck_loads('xyz')          # returns [1, 1, 1]
truck_loads('abcab')        # returns [5]
truck_loads('aabbcbdeed')   # returns [2, 4, 4]

In the first example the groups are 'aba' and 'cdcd': town a needs the first and third parcels on the same truck, so the first group cannot be any shorter.

Constraints

  • 0 <= len(labels) <= 100,000
  • labels holds lowercase letters only

Note

For an empty belt, return an empty list.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.