일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 프로그래머스
- 30. Substring with Concatenation of All Words
- Python Implementation
- Substring with Concatenation of All Words
- 43. Multiply Strings
- data science
- Python Code
- 파이썬
- shiba
- 시바견
- Protocol
- LeetCode
- 컴퓨터의 구조
- 109. Convert Sorted List to Binary Search Tree
- Generator
- Decorator
- DWG
- 운영체제
- 315. Count of Smaller Numbers After Self
- kaggle
- t1
- 밴픽
- Convert Sorted List to Binary Search Tree
- Python
- Regular Expression
- iterator
- concurrency
- attribute
- Class
- 715. Range Module
Archives
- Today
- Total
Scribbling
LeetCode: 347. Top K Frequent Elements 본문
빈도를 기억하기 위한 hashmap을 사용하자.
from collections import defaultdict
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
dic = defaultdict(int)
for num in nums:
dic[num] += 1
temp = []
for key, val in dic.items():
temp.append([val, key])
temp.sort(key=lambda x: -x[0])
temp = temp[:k]
return [t[1] for t in temp]
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 138. Copy List with Random Pointer (0) | 2021.11.16 |
---|---|
LeetCode: 295. Find Median from Data Stream (0) | 2021.11.16 |
LeetCode: 354. Russian Doll Envelopes (0) | 2021.11.15 |
LeetCode: 300. Longest Increasing Subsequence (0) | 2021.11.15 |
LeetCode: 132. Palindrome Partitioning II (0) | 2021.11.13 |