LeetCode 269

Graphs: 286. Walls and Gates

286. Walls and Gates from collections import dequefrom typing import Listclass Solution: def wallsAndGates(self, rooms: List[List[int]]) -> None: """ Do not return anything, modify rooms in-place instead. """ """ 문제 설명: -1: 벽 또는 장애물로 이동 불가능한 위치를 나타냄 0: 게이트(문)가 있는 위치 INF (2147483647): 빈 공간으로, 게이트까지의 거리를 계산해야 함 목표: 각 빈 공간(21..

Heap-PrioiryQueue: 621. Task Scheduler ★★★

621. Task Scheduler class Solution: def leastInterval(self, tasks: List[str], n: int) -> int: # each task takes 1 unit of time # minimize idle time # 각 작업은 1 단위 시간만 소요되며, 유휴 시간을 최소화하는 것이 목표 # Count the frequency of each task # 각 작업의 빈도를 세어서 저장 count = Counter(tasks) # 예: {"A": 3, "B": 3, "C": 1} # Convert the task counts to a max heap (negativ..

Heap-PrioiryQueue: 215. Kth Largest Element in an Array ★

215. Kth Largest Element in an Array class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: # minheap 으로 만들고, k개 까지. 힙 개수 유지하게 하면, # k 개 보다 많으면 작은 것들 제거 # 그렇게 하면, minheap[0]에는 k번째 큰 것이 남아있음 heapq.heapify(nums) while( len(nums) > k): heapq.heappop(nums) return nums[0] 메모리 절약을 위해, k 개 만큼 우선 넣어서, minheap 만들고.1개씩 추가해서, k개 넘는..