Ctrl K

Tight Trios

Medium
A chess club coach splits the players into teams of three. The players' ratings are given as ratings, whose length is a multiple of 3. The coach wants the ratings of any two players on the same team to differ by at most spread. Return the split as a list of trios. If several splits work, return the lexicographically smallest one: ratings inside each trio go in increasing order, and the trios are ordered increasingly as lists. If no such split exists, return an empty list.
Example 1
ratings=[10, 4, 6, 12, 5, 11], spread=2 → [[4, 5, 6], [10, 11, 12]]

The trios [4, 5, 6] and [10, 11, 12] each have a spread of 2.

Example 2
ratings=[3, 3, 3, 3, 9, 9], spread=5 → []

Four 3s cannot fit into one trio, so some 3 ends up with a 9, and the gap of 6 exceeds 5.

Example 3
ratings=[7, 1, 4], spread=6 → [[1, 4, 7]]
Constraints
  • 3 ≤ len(ratings) ≤ 10⁵, and len(ratings) is a multiple of 3
  • 1 ≤ ratings[i] ≤ 10⁵
  • 0 ≤ spread ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumStudios for Recording Sessions MediumLow, High, Low MediumStrictly Low, High, Low MediumFewest Meetings to Cancel Medium
loading Python…
Input
ratings=[10, 4, 6, 12, 5, 11], spread=2
Expected
[[4, 5, 6], [10, 11, 12]]

Run checks the first 3 tests in your browser. Submit checks all 94 tests on the server, hidden ones included.