파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 8. 18. 23:45

https://leetcode.com/problems/reduce-array-size-to-the-half/

 

Reduce Array Size to The Half - LeetCode

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 minSetSize(self, arr: List[int]) -> int:
        cnt = Counter(arr)
        freq = sorted(cnt.values(), reverse=True)
        
        mid = len(arr) // 2
        ans = 0
        
        while mid > 0:
            mid -= freq[ans]
            ans += 1
        
        return ans
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.08.20
Today's Challenge  (0) 2022.08.19
Today's Challenge  (0) 2022.08.17
Today's Challenge  (0) 2022.08.16
Today's Challenge  (0) 2022.08.15
Comments