Mid/SeniorArrayHash Table

Convert an Array Into a 2D Array With Conditions

You are given an integer array nums. You need to create a 2D array from nums satisfying the following conditions:

  • The 2D array should contain only the elements of the array nums.
  • Each row in the 2D array contains distinct integers.
  • The number of rows in the 2D array should be minimal.

Return the resulting array. If there are multiple answers, return any of them.

Note that the 2D array can have a different number of elements on each row.

Example 1
Inputnums = [1,3,4,1,2,3,1]
Output[[1,3,4,2],[1,3],[1]]
All elements of nums were used, each row contains distinct integers, and it can be shown that fewer than 3 rows is impossible.
Example 2
Inputnums = [1,2,3,4]
Output[[4,3,2,1]]
All elements of the array are distinct, so they can all be kept in the first row of the 2D array.

Constraints

  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= nums.length

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate