aboutsummaryrefslogtreecommitdiff
path: root/convert_arr_2d_cond.py
blob: 94c46488a1d22d796c0ce128a23dbe44159c0a33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 2610. Convert an Array Into a 2D Array With Conditions

"""
given an integer array 'nums', you should 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.
"""

class Solution(object):
    def findMatrix(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        um = {}
        ans = []
        for i in nums:
            um[i] = um.get(i, 0) + 1
        while um:
            tmp = []
            to_erase = []
            for f, s in um.items():
                tmp.append(f)
                s -= 1
                if s == 0:
                    to_erase.append(f)
                um[f] = s
            ans.append(tmp)
            for i in to_erase:
                del um[i]
        return ans

if __name__ == "__main__":
    obj = Solution()
    print(obj.findMatrix(nums = [1,3,4,1,2,3,1])) # expect: [[1,3,4,2],[1,3],[1]]
    print(obj.findMatrix(nums = [1,2,3,4])) # expect: [[4,3,2,1]]