카테고리 없음

[LeetCode] - 상위 K 빈도 요소

zzoming 2023. 11. 16. 16:51

https://www.youtube.com/watch?v=HraOg7W3VAM

 

이번 문제는 해시와 관련된 문제이다. 해시에 대해서 많은 글을 읽었지만, 자료구조에 취약한 나는 먼소리인가.. 머리가 어지럽던 와중에 10분만에 간단하고 바로 이해되게 해주는 영상을 만났다!! 


문제) 

 

풀이) 

import collections 

class Solution(object):
    def topKFrequent(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        answer = collections.Counter(nums).most_common(k)
        return [answer[i][0] for i in range(len(answer))]

유사한 문제 - 프로그래머스 : 완주하지 못한 선수 

https://school.programmers.co.kr/learn/courses/30/lessons/42576

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

import collections 

def solution(participant, completion):
    answer = collections.Counter(participant) - collections.Counter(completion) 
    return list(answer.keys())[0]