LeetCode/LeetCode75 34

[LeetCode 75] Medium - 2300. Successful Pairs of Spells and Potions

2300. Successful Pairs of Spells and Potions https://youtu.be/OKnm5oyAhWg?si=k_d-0f6Nt10xOCD6 class Solution: def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]: # spell * potion >= success 를 만족하는 쌍을 찾아야 함 # 이를 변형하면: potion >= success / spell 이 됨 # 즉, potion의 값이 특정 기준값 이상인 경우를 찾아야 함. # 1. potions 리스트를 정렬하여 이진 탐색을 수행 가능하도록 준비..

LeetCode/LeetCode75 2024.11.22

[LeetCode 75] Medium - 2462. Total Cost to Hire K Workers

2462. Total Cost to Hire K Workers class Solution: def totalCost(self, costs: List[int], k: int, candidates: int) -> int: n = len(costs) # 근로자의 총 수 # **교차점 처리**: # 만약 앞부분(pre)과 뒷부분(suf)이 겹치는 경우: # 전체를 정렬한 뒤, k명의 최소 비용을 더한 값을 반환. if 2 * candidates + k > n: costs.sort() # 비용 정렬 return sum(costs[:k]) # k개의 최소값을 반환 # **초기화..

LeetCode/LeetCode75 2024.11.19

[LeetCode 75] Medium - 1926. Nearest Exit from Entrance in Maze

1926. Nearest Exit from Entrance in Maze # 최단거리 => BFS => queue # 시작점을 que 에 넣고. pop 하면서 검색 # pop 할때마다. 4방향 검색: 1레벨 up # 방문 셀 체크 해야함  https://youtu.be/fCfjOdi6V3g?si=RKIvqX7H_LOurZrM class Solution: def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int: # 최단거리 => buffer search => queue # 시작점을 que 에 넣고. pop 하면서 검색 # pop 할때마다. 4방향 검색: 1레벨 up # 방문 셀..

LeetCode/LeetCode75 2024.11.19

[LeetCode 75] Medium - 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: nums.sort() return nums[-k] 정렬 하지 않고 푸는 법- Heap / Priority Queue 사용 - 힙 큐, 우선순위 큐 class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: heap = [] for n in nums: heapq.heappush(heap, -n) # - 붙임으로써 큰->작은순으로 정렬 ..

LeetCode/LeetCode75 2024.11.18