Ctrl K

Split Into Straight Runs

Medium
In a board game you hold number tiles: tiles[i] is the number on tile i. You must lay out all of them in rows of exactly size tiles so that the numbers in each row are consecutive: x, x + 1, …, x + size - 1. Every tile goes into exactly one row. Return True if such a layout exists and False otherwise.
Example 1
tiles=[5, 3, 4, 6, 4, 5], size=3 → True

The tiles form the runs [3, 4, 5] and [4, 5, 6].

Example 2
tiles=[1, 2, 4, 5], size=2 → True

The runs [1, 2] and [4, 5] work: numbers must be consecutive within a run, but separate runs are unrelated.

Example 3
tiles=[2, 2, 3, 4], size=2 → False

One 2 has to pair with the 3, and the other would need a 1 or a 3: there is no 1, and the 3 is taken.

Constraints
  • 1 ≤ size ≤ len(tiles) ≤ 10⁵
  • 1 ≤ tiles[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsJobs With a Cooldown MediumDeal Into Consecutive Sets MediumPair Each Number With Its Double MediumSpread Out the Labels MediumPick Within Tag Limits Medium
loading Python…
Input
tiles=[5, 3, 4, 6, 4, 5], size=3
Expected
True

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