본문 바로가기

IT/코딩테스트

[Leetcode] 2610. Convert an Array Into a 2D Array With Conditions

728x90

https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class Solution:
    def findMatrix(self, nums: List[int]) -> List[List[int]]:
        result = []

        nums_dic = {}
        for n in nums:
            nums_dic[n] = nums_dic.get(n, 0) + 1

        for i in range(0, max(nums_dic.values())):
            result.append([])

        for key, value in nums_dic.items():
            for i in range(0, value):
                result[i].append(key)

        return result
728x90