| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
- 파이썬알고리즘
- 투포인터
- Python3
- 니트코드
- 릿코드
- 코드업
- LeetCode
- SQL
- dfs
- 슬라이딩윈도우
- 생성형AI
- 알고리즘
- BFS
- 리트코드
- nlp
- Greedy
- 파이썬
- array
- heap
- graph
- Python
- GenAI
- GenerativeAI
- binary Tree
- stratascratch
- codeup
- sql코테
- tree
- Stack
- two-pointer
- Today
- Total
목록BFS (8)
Tech for good
def count_components(adjacency_dict): # Write your code here visited = set() cnt = 0 def dfs(node): visited.add(node) for neighbor in adjacency_dict[node]: if neighbor not in visited: dfs(neighbor) for node in adjacency_dict: if node not in visited: dfs(node) cnt += 1 return cnt
class Solution: def numIslands(self, grid: List[List[str]]) -> int: n_rows, n_cols = len(grid), len(grid[0]) res = 0 visited = set() def dfs(r, c): visited.add((r,c)) neighbors = [(r-1, c), (r+1, c), (r, c-1), (r, c+1)] # check if visited or not for n_r, n_c in neighbors: if n_r in range(n_rows) and n_c in ..
class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: # 1. Make a graph ## To figure out neighbor using hash-map ## 0 => [1,2] graph = defaultdict(list) for u, v in edges: graph[u].append(v) graph[v].append(u) # 2. BFS from source q = deque([source]) visited = se..
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: # Base case: decide where to stop if root is None: return None right = self.invertT..
Greedy + Adjacency Counting Approachclass Solution: def islandPerimeter(self, grid: List[List[int]]) -> int: # Initialize perimeter perimeter = 0 # Iterate over each cell in the grid for i in range(len(grid)): for j in range(len(grid[0])): # If the cell is land if grid[i][j] == 1: # Add 4 for the c..
Depth-First Search (DFS)class Solution: def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]: original_color = image[sr][sc] if original_color == color: return image # No need to do anything if the target color is the same rows, cols = len(image), len(image[0]) def dfs(r, c): if (r = row..
https://neetcode.io/problems/binary-tree-right-side-view NeetCode neetcode.io첫번째 풀이 방법: BFS - Level Order Traversal각 레벨에서 가장 오른쪽에 있는 노드의 값만 저장하면 된다.BFS(너비 우선 탐색, Queue;)을 이용하여 레벨별 탐색을 수행한다.매 레벨의 마지막 노드 값을 결과 리스트에 추가한다.# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right =..
https://neetcode.io/problems/level-order-traversal-of-binary-tree NeetCode neetcode.io Level-Order Traversal이란?Level-order traversal(레벨 순회) 는 트리(Tree) 를 탐색하는 방법 중 하나로, BFS(너비 우선 탐색, Breadth-First Search) 를 기반으로 한다.트리 탐색 (Tree > Traversal)의 대표적인 방법트리에서 노드를 방문하는 순서에 따라 탐색 방식이 여러 가지가 있다.DFS (깊이 우선 탐색, Depth-First Search)루트에서 시작해서 최대한 깊이 내려간 후 백트래킹(되돌아옴).구현 방법:전위 순회 (Preorder): Root → Left → Right중위..