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
Input
nums = [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
Input
nums = [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