Skip to content

Commit 1d03474

Browse files
[yuseok89] WEEK 04 Solutions (#2739)
* contains duplicate solution * two sum solution * top k frequent elements solution * longest consecutive sequence solution * house robber solution * add newline * 리뷰 반영 * 리뷰 반영 * 개선 - AI assist * valid anagram solution * climbing stairs solution * product of array except self solution * 3sum solution * validate binary search tree solution * 공간복잡도 업데이트 * 공간복잡도 업데이트 * validate palindrome solution * number of 1 bits solution * combination sum solution * decode ways solution * maximum subarray solution * 복잡도 업데이트 * 재귀 로직 개선 * maximum subarray 풀이 개선 * valid palindrome 풀이 개선 * maximum subarray 풀이 개선 * merge two sorted lists solution * merge two sorted lists solution 리뷰 반영 * maximum depth of binary tree solution * find minimum in rotated sorted array solution * word search solution * coin change solution * 시간 공간 복잡도 업데이트 * 사전 필터링 * 코드 개선 * Apply suggestion from @parkhojeong Co-authored-by: Hojeong Park <parkhj062@gmail.com> --------- Co-authored-by: Hojeong Park <parkhj062@gmail.com>
1 parent c67b9ac commit 1d03474

5 files changed

Lines changed: 123 additions & 0 deletions

File tree

coin-change/yuseok89.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# TC: O(amount * len(coins))
2+
# SC: O(amount)
3+
class Solution:
4+
def coinChange(self, coins: List[int], amount: int) -> int:
5+
6+
from collections import deque
7+
8+
v = {0: 0}
9+
q = deque([0])
10+
11+
while q and amount not in v:
12+
13+
cur = q.popleft()
14+
15+
for coin in coins:
16+
17+
if cur + coin > amount or cur + coin in v:
18+
continue
19+
20+
v[cur + coin] = v[cur] + 1
21+
q.append(cur + coin)
22+
23+
return -1 if amount not in v else v[amount]
24+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# TC: O(logN)
2+
# SC: O(1)
3+
class Solution:
4+
def findMin(self, nums: List[int]) -> int:
5+
low = 0
6+
high = len(nums) - 1
7+
8+
while low < high - 1:
9+
mid = (low + high) // 2
10+
11+
if nums[low] < nums[mid] < nums[high]:
12+
return nums[low]
13+
14+
if nums[low] > nums[mid]:
15+
high = mid
16+
else:
17+
low = mid + 1
18+
19+
return min(nums[low], nums[high])
20+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# TC: O(N)
2+
# SC: O(H)
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, val=0, left=None, right=None):
6+
# self.val = val
7+
# self.left = left
8+
# self.right = right
9+
class Solution:
10+
def maxDepth(self, root: Optional[TreeNode]) -> int:
11+
12+
if not root:
13+
return 0
14+
15+
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
16+

merge-two-sorted-lists/yuseok89.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# TC: O(N+M)
2+
# SC: O(1)
3+
# Definition for singly-linked list.
4+
# class ListNode:
5+
# def __init__(self, val=0, next=None):
6+
# self.val = val
7+
# self.next = next
8+
class Solution:
9+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
10+
11+
ret = ListNode()
12+
cur = ret
13+
14+
while list1 and list2:
15+
if list1.val < list2.val:
16+
cur.next = list1
17+
list1 = list1.next
18+
else:
19+
cur.next = list2
20+
list2 = list2.next
21+
cur = cur.next
22+
23+
cur.next = list1 if list1 else list2
24+
25+
return ret.next
26+

word-search/yuseok89.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# TC: O(N*M*4^L)
2+
# SC: O(L)
3+
class Solution:
4+
def exist(self, board: List[List[str]], word: str) -> bool:
5+
6+
n = len(board)
7+
m = len(board[0])
8+
9+
board_counter = Counter(board[r][c] for r in range(n) for c in range(m))
10+
word_counter = Counter(word)
11+
12+
for c, cnt in word_counter.items():
13+
if board_counter[c] < cnt:
14+
return False
15+
16+
def rec(row, col, idx):
17+
if idx == len(word):
18+
return True
19+
20+
if row < 0 or row >= n or col < 0 or col >= m or board[row][col] != word[idx]:
21+
return False
22+
23+
board[row][col] = None
24+
25+
ret = rec(row + 1, col, idx + 1) or rec(row - 1, col, idx + 1) or rec(row, col + 1, idx + 1) or rec(row, col - 1, idx + 1)
26+
27+
board[row][col] = word[idx]
28+
29+
return ret
30+
31+
for row in range(0, n):
32+
for col in range(0, m):
33+
if board[row][col] == word[0] and rec(row, col, 0):
34+
return True
35+
36+
return False
37+

0 commit comments

Comments
 (0)