Zero to Hero
article thumbnail
104. Maximum Depth of Binary Tree
Algorithm 2021. 4. 16. 10:51

Maximum Depth of Binary Tree - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. DFS를 이용한 내 풀이 class Solution: answer = 0 def maxDepth(self, root: TreeNode) -> int: def dfs(cur, depth): self.answer = max(self.answer, depth) if cur.left: dfs(cur.left, depth + 1) if cur.right: dfs(c..

13549번: 숨바꼭질 3
Algorithm 2021. 4. 15. 15:04

13549번: 숨바꼭질 3 수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일 www.acmicpc.net import sys from collections import deque r = sys.stdin.readline src, dst = map(int, r().split()) visited = set() q = deque([]) q.append((src, 0)) result = 0 while q: cur_pos, cur_val = q.popleft() if cur_pos == dst: result = cur_val break if 0

article thumbnail
283. Move Zeroes
Algorithm 2021. 4. 15. 11:11

Move Zeroes - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 우선순위 큐를 이용해 zero index를 관리하는 해결법 from typing import * from heapq import * class Solution: def moveZeroes(self, nums: List[int]) -> None: q = [] for i in range(len(nums)): if nums[i] == 0: q.append(i) heapify(q) for i ..

article thumbnail
787. Cheapest Flights Within K Stops
Algorithm 2021. 4. 14. 18:34

Cheapest Flights Within K Stops - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Dijkstra와 경유지 횟수를 고려한 최단 거리 알고리즘 (실패) 원인 결국 최단거리 배열에 저장되는 거리는 경유지 횟수를 고려하지 않은 최단거리가 될 수밖에 없음 from typing import * from heapq import * from collections import defaultdict # k번 이하의 경유지를 이용할 수 있을 경우의 ..

article thumbnail
763. Partition Labels
Algorithm 2021. 4. 13. 11:25

Partition Labels - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 문자의 첫 출현, 마지막 출현 정보 + Queue를 이용한 선형탐색 from typing import * from collections import deque, defaultdict class Solution: def partitionLabels(self, s: str) -> List[int]: answer = [] q = [] dict1 = defaultdict(list) f..

article thumbnail
743. Network Delay Time
Algorithm 2021. 4. 11. 08:16

Network Delay Time - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Floyd-Warshall from typing import * class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: dp = [[float("inf")] * (n + 1) for _ in range(n + 1)] for i in range(n + 1): dp[i][..