Ctrl K

Rows Without Repeats

Medium
You are given an integer array items. Distribute all of its elements into rows so that: - every element of items goes into exactly one row; - no row contains the same value twice; - the number of rows is as small as possible. Rows may have different lengths. Return the resulting list of rows. The order of the rows and the order of numbers within a row do not matter.
Example 1
items=[2, 5, 2, 1, 5, 2] → [[2, 5, 1], [2, 5], [2]]

The value 2 appears three times, so at least three rows are needed, for example [2, 5, 1], [2, 5], [2].

Example 2
items=[3, 1, 2] → [[3, 1, 2]]

All values are distinct, so a single row is enough.

Example 3
items=[1, 1] → [[1], [1]]
Constraints
  • 1 ≤ len(items) ≤ 200
  • 1 ≤ items[i] ≤ len(items)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSudoku Board Without Clashes MediumAnagram Buckets MediumZero Out the Crosses MediumTree From Pre-Order and In-Order Walks MediumTree From In-Order and Post-Order Walks Medium
loading Python…
Input
items=[2, 5, 2, 1, 5, 2]
Expected
[[2, 5, 1], [2, 5], [2]]

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