LeetCode 329

[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

[LeetCode 75] Medium - 236. Lowest Common Ancestor of a Binary Tree

236. Lowest Common Ancestor of a Binary Tree LCA(최저 공통 조상) 찾기노드 자신도 공통조상이 될수 있음재귀로..일단 루트가 none 이면 return 또는 return root루트가 p 또는 q 인지 체크해서 맞으면 루트 반환아니면, ..class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': if not root: # 빈 노드면 return ## return root if root == p or root == q: return root # 좌측에..

LeetCode/공통 2024.11.17