Zero to Hero
article thumbnail
7. Reverse Integer
Algorithm 2021. 4. 17. 21:44

Reverse Integer - 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. 내 풀이 class Solution: def reverse(self, x: int) -> int: x = int(str(x)[::-1]) if str(x)[-1] != "-" else int("-" + str(x)[::-1][:-1]) return x if abs(x) < 2 ** 31 else 0

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..

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][..