일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- SQL
- 파이썬기초100제
- Python
- codeup
- GenerativeAI
- 리트코드
- tree
- gcp
- 투포인터
- 생성형AI
- GenAI
- LeetCode
- nlp
- 릿코드
- 슬라이딩윈도우
- sql코테
- Python3
- medium
- two-pointer
- stratascratch
- dfs
- slidingwindow
- Microsoft
- 코드업
- 자연어처리
- 파이썬알고리즘
- 파이썬
- 알고리즘
- heap
- 니트코드
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 |