Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 취업리부트코스
- 99클럽
- 빈 조회 2개 이상
- 코딩테스트 준비
- jwttoken
- KPT회고
- spring batch 5.0
- 프로그래머스 이중우선순위큐
- 전략패턴 #StrategyPattern #디자인패턴
- DesignPattern
- 파이썬
- Spring multimodule
- jwt
- 커스텀 헤더
- @FeignClient
- 항해99
- 프로그래머스
- infcon 2024
- 개발자 취업
- 디자인 패턴
- 개발자부트캠프추천
- 디자인패턴
- 빈 충돌
- Python
- 단기개발자코스
- 1주일회고
- 구글 OAuth login
- 인프콘 2024
- TiL
- JavaScript
Archives
- Today
- Total
m1ndy5's coding blog
LeetCode 347. Top K Frequent Elements with Python 본문
https://leetcode.com/problems/top-k-frequent-elements/
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
dic = {}
answer = []
for num in nums:
if num not in dic:
dic[num] = 1
else:
dic[num] += 1
#[[1, 3번], [2, 2번], ...]
l = [[key, value] for key, value in dic.items()]
# 빈도수 기준 정렬(내림차순)
l.sort(key=lambda x: x[1], reverse = True)
# k개만 뽑기
for i in range(k):
answer.append(l[i][0])
return answer
그렇게 어려웠던 문제는 아니었던 것 같다!
'알고리즘 with python > 알고리즘 스터디' 카테고리의 다른 글
LeetCode 1337. The K Weakest Rows in a Matrix with Python (1) | 2024.01.05 |
---|---|
LeetCode 1464. Maximum Product of Two Elements in an Array with Python (0) | 2024.01.05 |
LeetCode 3. Longest Substring Without Repeating Characters with Python (1) | 2024.01.04 |
LeetCode 771. Jewels and Stones with Python (1) | 2024.01.04 |
LeetCode 739. Daily Temperatures with Python (1) | 2024.01.04 |