Skip to content

Commit aa5706c

Browse files
committed
feat: add current week problems
1 parent 394f06c commit aa5706c

4 files changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def findMin(self, nums: list[int]) -> int:
3+
"""
4+
정렬된 리스트에서 n번 돌아간 상태에서 기존의 첫번째 값 (가장 작은)을 찾는 함수
5+
6+
방법:
7+
Hint2의 Can you think of an algorithm which has O(logN) search complexity?
8+
에서 binary search!
9+
10+
Args:
11+
nums (list[int]): n번 돌아간 정수 리스트
12+
13+
Returns:
14+
int: 최초 정렬된 리스트의 첫번째 값
15+
"""
16+
if nums[0] <= nums[-1]:
17+
return nums[0]
18+
start, end = 0, len(nums) - 1
19+
while start < end:
20+
if start == end:
21+
break
22+
mid = start + (end - start) // 2
23+
if nums[mid] > nums[end]:
24+
start = mid + 1
25+
else:
26+
end = mid
27+
return nums[start]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, val=0, left=None, right=None):
4+
self.val = val
5+
self.left = left
6+
self.right = right
7+
8+
9+
class Solution:
10+
def maxDepth(self, root: TreeNode | None) -> int:
11+
"""주어진 binary tree의 최대 깊이를 찾는 함수
12+
13+
방법:
14+
1. 재귀함수를 생각함. 인자로 현재 head node와 현재의 depth를 받는 재귀함수를 만들어 재귀 반복
15+
2. 이 함수 자체가 재귀가 될 수 있다고 판단, 별도의 재귀함수를 삭제함.
16+
depth를 넘겨주지 않아도, 앞에 +1을 함으로써 depth가 계산됨.
17+
18+
Args:
19+
root (TreeNode | None): binary tree
20+
21+
Returns:
22+
int: 최대 깊이
23+
"""
24+
if root is None:
25+
return 0
26+
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))

merge-two-sorted-lists/kangdaia.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Definition for singly-linked list.
2+
class ListNode:
3+
def __init__(self, val=0, next=None):
4+
self.val = val
5+
self.next = next
6+
7+
8+
class Solution:
9+
def mergeTwoLists(self, list1: ListNode | None, list2: ListNode | None) -> ListNode | None:
10+
"""두개의 링크드 리스트를 합쳐서 하나의 sorted 링크드 리스트를 만드는 함수
11+
12+
방법:
13+
1. 각각의 리스트 head에서 값을 비교하면서,
14+
1번보다 2번의 값이 작으면 1번의 head를 움직이고,
15+
반대의 경우 2번의 head를 움직이도록 함.
16+
움직이면서 합치는 리스트에 tail.next로 붙여줌
17+
18+
Args:
19+
list1 (ListNode | None): 정렬된 양수 링크드 리스트
20+
list2 (ListNode | None): 정렬된 양수 링크드 리스트 2
21+
22+
Returns:
23+
ListNode | None: 하나로 합쳐진 정렬된 링크드 리스트
24+
"""
25+
final = ListNode()
26+
tail = final
27+
curr1, curr2 = list1, list2
28+
while curr1 and curr2:
29+
if curr1.val < curr2.val:
30+
tail.next = curr1
31+
curr1 = curr1.next
32+
else:
33+
tail.next = curr2
34+
curr2 = curr2.next
35+
tail = tail.next
36+
tail.next = curr1 if curr1 else curr2
37+
return final.next

word-search/kangdaia.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution:
2+
def exist(self, board: list[list[str]], word: str) -> bool:
3+
"""m*n 사이즈 보드가 주어졌을 때, word를 만들 수 있는지 true/false로 return 하는 함수
4+
5+
1. bfs로 queue에 모든 element를 추가하고, 매번 path를 파악해서 word인지 판단하도록 함
6+
-> 시간 효율이 안나옴
7+
2. dfs 방식 적용. 현재 탐색 중일때, 보드를 #으로 치환해서, 이미 방문한 곳임을 체크
8+
- 시작점이 word[0]와 같을 때만 시작
9+
- dfs는 전체 path를 기록하지 않고, word가 볼 index를 인자로 받도록 함.
10+
11+
Args:
12+
board (list[list[str]]): 글자를 element로 가진 2d array
13+
word (str): 찾아야 할 글자 목록
14+
15+
Returns:
16+
bool: word가 board에 있는지 여부
17+
"""
18+
m, n = len(board[0]), len(board)
19+
20+
def dfs(row, col, idx):
21+
if board[row][col] != word[idx]:
22+
return False
23+
if idx == len(word) - 1:
24+
return True
25+
visited = board[row][col]
26+
board[row][col] = "#"
27+
for r, c in [(row - 1, col), (row + 1, col), (row, col - 1), (row, col + 1)]:
28+
if 0 <= r < n and 0 <= c < m:
29+
if dfs(r, c, idx + 1):
30+
board[row][col] = visited
31+
return True
32+
board[row][col] = visited
33+
return False
34+
35+
for ir in range(n):
36+
for ic in range(m):
37+
if board[ir][ic] == word[0] and dfs(ir, ic, 0):
38+
return True
39+
return False

0 commit comments

Comments
 (0)