Zero to Hero
article thumbnail
155. Min Stack
Algorithm 2021. 4. 30. 17:04

Min Stack - 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. event를 flag로 해서 처리하는 풀이 from heapq import * class MinStack: def __init__(self): self.stack = [] self.min_stack = [] self.events = [0] * 30000 self.event_cnt = 0 def push(self, val: int) -> None: self.stack.append((val, ..

article thumbnail
160. Intersection of Two Linked Lists
Algorithm 2021. 4. 30. 13:52

Intersection of Two Linked Lists - 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. 객체 ID값을 이용한 분기점 반환 class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: a_id_list = [] b_id_list = [] p_a, p_b = headA, headB while p_a: a_id_list.append(id..

article thumbnail
70. Climbing Stairs
Algorithm 2021. 4. 29. 17:25

Climbing Stairs - 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. Dynamic Programming class Solution: def climbStairs(self, n: int) -> int: dp = [[0] * 2 for _ in range(46)] dp[1][0] = 1 dp[2][0] = 1 dp[2][1] = 1 for i in range(3, n + 1): dp[i][0] = sum(dp[i - 1]) dp[i][1] = sum..

article thumbnail
543. Diameter of Binary Tree
Algorithm 2021. 4. 28. 15:04

Diameter 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. 실패한 코드 # Definition for a binary tree node. from typing import * from collections import deque # Definition for a binary tree node. class TreeNode: def __init__(self, val=0, left=None, right=None): self.va..

article thumbnail
121. Best Time to Buy and Sell Stock
Algorithm 2021. 4. 27. 11:10

leetcode.com/problems/best-time-to-buy-and-sell-stock/ Best Time to Buy and Sell Stock - 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 maxProfit(self, prices: List[int]) -> int: min_val, profit = prices[0], 0 for price in prices: min_val = min(min_val..

article thumbnail
448. Find All Numbers Disappeared in an Array
Algorithm 2021. 4. 26. 14:12

Find All Numbers Disappeared in an Array - 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 findDisappearedNumbers(self, nums: List[int]) -> List[int]: return list(set([i for i in range(1, len(nums) + 1)]) - set(nums)) 하지만 집합 연산을 위한 추가 공간을 사용하고 있..