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

article thumbnail
21. Merge Two Sorted Lists
Algorithm 2021. 4. 26. 12:56

Merge Two Sorted 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. 내 Trash Garbage 코드 class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: zero_node = ListNode() cur = zero_node while l1 and l2: if l1.val >= l2.val: cur.next = l2 l2 = l2.next el..

article thumbnail
169. Majority Element
Algorithm 2021. 4. 23. 11:01

Majority Element - 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. 해시맵 스타일(Dict, Counter 사용) from collections import Counter class Solution: def majorityElement(self, nums: List[int]) -> int: counter = Counter() for num in nums: counter[num] += 1 return counter.most_common(1)[0]..