Skip to content

Commit 2d459d9

Browse files
authored
Create Convert an Array Into a 2D Array With Conditions.py
1 parent 9ed5f37 commit 2d459d9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
Companies
3+
You are given an integer array nums. You need to create a 2D array from nums satisfying the following conditions:
4+
5+
The 2D array should contain only the elements of the array nums.
6+
Each row in the 2D array contains distinct integers.
7+
The number of rows in the 2D array should be minimal.
8+
Return the resulting array. If there are multiple answers, return any of them.
9+
10+
Note that the 2D array can have a different number of elements on each row.
11+
'''
12+
13+
class Solution:
14+
def findMatrix(self, nums: List[int]) -> List[List[int]]:
15+
nums.sort(reverse=True)
16+
lignes = []
17+
for num in nums:
18+
for ligne in lignes:
19+
if num not in ligne:
20+
ligne.append(num)
21+
break
22+
else:
23+
lignes.append([num])
24+
return lignes

0 commit comments

Comments
 (0)