[알고리즘] DFS
깊이 우선 탐색이란? (DFS, Depth First Search) 미로 찾이과 같이, 한 우물만 끝까지 파고 아니면 돌아가는 방식 핵심 자료구조 : 스택 (Stack) 구현 def dfs(current_node, visited): # 1. 방문 처리 visited.add(current_node) print(cur...
깊이 우선 탐색이란? (DFS, Depth First Search) 미로 찾이과 같이, 한 우물만 끝까지 파고 아니면 돌아가는 방식 핵심 자료구조 : 스택 (Stack) 구현 def dfs(current_node, visited): # 1. 방문 처리 visited.add(current_node) print(cur...
너비 우선 탐색이란? (BFS, Breadth First Search) 시작 노드에서부터 인접한 노드를 먼저 탐색하며 넓히는 방법 핵심 자료구조 : 큐 (Queue) deque 라이브러리를 사용하는 것이 좋다 동작 과정 시작 노드를 큐에 삽입 후 방문 처리 큐에서 노드를 꺼내 인접 노드 중에서 방문하지 않은 모든 노드를 큐...
난이도:EasySolved My Solution class Solution: def search(self, nums: List[int], target: int) -> int: numMap = {} n = len(nums) for i in range(n): numMa...
난이도:EasySolved My Solution class Solution: def isAnagram(self, s: str, t: str) -> bool: if len(s) != len(t): return False return sorted(s) == sorted(t) Ot...
난이도:EasySolved My Solution # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # ...
이진탐색이란? (Binary Search) 검색이 매우 빠른 알고리즘이다. 다만, 이진탐색이 가능하기 위해서는 데이터가 sorted된 상태여야 한다. 작동 원리 target을 찾기 위해 인덱스의 중간값(mid)와 먼저 비교한다. 만약 target이 더 크다면 왼쪽으로, 작아면 오른쪽으로 이동하면서 검색한다. 이렇게 된다면 절반은 버리고 시작할 ...
len(a) # 객체의 수, 또는 길이 # range문법. 만약 안에 두개가 있으면 a, b 까지. 하나만 있으면 0 에서 a까지 range(a, b) range(a) mapping = {")":"(", "}":"{", "]":"["} # dictionary 문법으로 key:value mapping.values() # value 값 선택 map...
난이도:EasySolved My Solution class Solution: def isPalindrome(self, s: str) -> bool: L, R = 0, len(s) - 1 while L < R: if not s[L].isalnum(): ...
난이도:EasySolved My Solution class Solution: def maxProfit(self, prices: List[int]) -> int: min_price = float(inf) max_profit = 0 for price in prices: ...
Queue란? FIFO (First In First Out), 선입선출의 원칙을 따르는 자료구조이고 아래와 같이 두가지 중요한 작업이 있다. Enqueue : 큐의 끝에 항목 추가 Dequeue : 큐의 앞쪽에서 항목 제거 후 반환 즉, 큐에서는 가장 먼저 enqueue된 항목이 가장 먼저 dequeue 된다. Queue의 종류 ...