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, ..
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..
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..
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..
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..
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)) 하지만 집합 연산을 위한 추가 공간을 사용하고 있..