일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- Stack
- Python3
- 생성형AI
- medium
- 자연어처리
- GenAI
- heap
- codeup
- 릿코드
- Python
- sql코테
- 슬라이딩윈도우
- SQL
- 알고리즘
- gcp
- GenerativeAI
- 리트코드
- tree
- 니트코드
- 파이썬기초100제
- dfs
- 파이썬알고리즘
- LeetCode
- two-pointer
- 파이썬
- stratascratch
- slidingwindow
- 투포인터
- nlp
- 코드업
Archives
- Today
- Total
Tech for good
[Leetcode/Heap] 451. Sort Characters By Frequency 본문
class Solution:
def frequencySort(self, s: str) -> str:
# Step 1: Count frequency of each character
freq = Counter(s)
# Step 2: Build max-heap based on frequency
# Use -count to simulate max-heap
heap = [(-count, char) for char, count in freq.items()]
heapq.heapify(heap)
# Step 3: Build the result string from the heap
result = []
while heap:
count, char = heapq.heappop(heap)
result.append(char * (-count)) # Multiply char by its original count
return ''.join(result)
'IT > Computer Science' 카테고리의 다른 글
[Neetcode/Heap] Kth Largest Element in a Stream (0) | 2025.04.11 |
---|---|
[Leetcode/Heap] 1642. Furthest Building You Can Reach (0) | 2025.04.09 |
[Leetcode/Heap] 3264. Final Array State After K Multiplication Operations I (0) | 2025.04.07 |
[Leetcode/Sort, Heap] 2974. Minimum Number Game (0) | 2025.04.06 |
[Leetcode/Heap] 2558. Take Gifts From the Richest Pile (0) | 2025.04.04 |