Skip to content

Commit 27ece99

Browse files
authored
Merge pull request #806 from KwonNayeon/main
[KwonNayeon] Week 4
2 parents 4acfa7b + b13a229 commit 27ece99

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

coin-change/KwonNayeon.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Constraints:
3+
1. 1 <= coins.length <= 12
4+
2. 1 <= coins[i] <= 2^31 - 1
5+
3. 0 <= amount <= 10^4
6+
7+
Time Complexity: O(N*M)
8+
- N은 amount
9+
- M은 동전의 개수 (coins.length)
10+
11+
Space Complexity: O(N)
12+
- N은 amount
13+
14+
To Do:
15+
- DP 문제 복습하기
16+
- Bottom-up과 Top-down 방식의 차이점 이해하기
17+
- 그리디와 DP의 차이점 복습하기
18+
"""
19+
20+
class Solution:
21+
def coinChange(self, coins: List[int], amount: int) -> int:
22+
dp = [sys.maxsize // 2] * (amount + 1)
23+
dp[0] = 0
24+
25+
for i in range(1, amount + 1):
26+
for coin in coins:
27+
if i >= coin:
28+
dp[i] = min(dp[i], dp[i-coin] + 1)
29+
30+
return dp[amount] if dp[amount] != sys.maxsize // 2 else -1

merge-two-sorted-lists/KwonNayeon.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Constraints:
3+
1. 0 <= list1.length, list2.length <= 50
4+
2. -100 <= Node.val <= 100
5+
3. list1 and list2 are sorted in non-decreasing order
6+
7+
Time Complexity: n과 m이 각각 list1과 list2의 길이를 나타낼 때, O(n + m)
8+
- 각 노드를 한 번씩만 방문하기 때문
9+
10+
Space Complexity: O(1)
11+
- 새로운 노드를 만들지 않고 기존 노드들의 연결만 바꾸기 때문
12+
13+
풀이 방법:
14+
1. 더미 노드를 만들어서 결과 리스트의 시작점으로 사용
15+
2. 두 리스트를 앞에서부터 순회하면서 값을 비교
16+
3. 더 작은 값을 가진 노드를 결과 리스트에 연결하고, 해당 리스트의 포인터를 다음으로 이동
17+
4. 한쪽 리스트가 끝나면, 남은 리스트를 그대로 결과 리스트 뒤에 연결
18+
5. 더미 노드의 next를 반환 (실제 정렬된 리스트의 시작점)
19+
"""
20+
21+
# Definition for singly-linked list.
22+
# class ListNode:
23+
# def __init__(self, val=0, next=None):
24+
# self.val = val
25+
# self.next = next
26+
class Solution:
27+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
28+
result = ListNode()
29+
current = result
30+
31+
while list1 and list2:
32+
if list1.val <= list2.val:
33+
current.next = list1
34+
list1 = list1.next
35+
else:
36+
current.next = list2
37+
list2 = list2.next
38+
current = current.next
39+
40+
if list1:
41+
current.next = list1
42+
if list2:
43+
current.next = list2
44+
45+
return result.next

missing-number/KwonNayeon.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Constraints:
3+
1. n equals the length of array nums
4+
2. n is between 1 and 10^4 inclusive
5+
3. Each element nums[i] is between 0 and n inclusive
6+
4. All numbers in nums are unique (no duplicates)
7+
8+
Time Complexity: O(nlogn)
9+
- 정렬에 nlogn, 순회에 n이 필요하므로 전체적으로 O(nlogn)
10+
Space Complexity: O(1)
11+
- 추가 공간을 사용하지 않고 입력 배열만 사용
12+
13+
풀이 방법:
14+
1. 배열을 정렬하여 0부터 n까지 순서대로 있어야 할 위치에 없는 숫자를 찾음
15+
2. 인덱스와 해당 위치의 값을 비교하여 다르다면 그 인덱스가 missing number
16+
3. 모든 인덱스를 확인했는데도 없다면 n이 missing number
17+
"""
18+
19+
class Solution:
20+
def missingNumber(self, nums: List[int]) -> int:
21+
nums.sort()
22+
23+
for i in range(len(nums)):
24+
if nums[i] != i:
25+
return i
26+
27+
return len(nums)
28+

word-search/KwonNayeon.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Constraints:
3+
1. m equals board length (number of rows)
4+
2. n equals board[i] length (number of columns)
5+
3. m and n are between 1 and 6 inclusive
6+
4. word length is between 1 and 15 inclusive
7+
5. board and word contain only lowercase and uppercase English letters
8+
9+
Time Complexity: O(N * 3^L)
10+
- N은 board의 모든 cell (m * n)
11+
- L은 word의 길이
12+
- 각 cell에서 시작하여 word의 각 글자마다 세방향으로 탐색 (이미 방문한 방향 제외)
13+
14+
Space Complexity: O(L)
15+
- L은 word의 길이로, 재귀 호출 스택의 깊이
16+
17+
To Do:
18+
- DFS와 백트래킹 개념 복습하기
19+
- 다른 스터디원분들의 답안 참조하여 다른 풀이방법 복습하기
20+
"""
21+
22+
class Solution:
23+
def exist(self, board: List[List[str]], word: str) -> bool:
24+
rows, cols = len(board), len(board[0])
25+
26+
def dfs(i, j, k):
27+
if k == len(word):
28+
return True
29+
30+
if (i < 0 or i >= rows or
31+
j < 0 or j >= cols or
32+
board[i][j] != word[k]):
33+
return False
34+
35+
temp = board[i][j]
36+
board[i][j] = '#'
37+
38+
result = (dfs(i+1, j, k+1) or
39+
dfs(i-1, j, k+1) or
40+
dfs(i, j+1, k+1) or
41+
dfs(i, j-1, k+1))
42+
43+
board[i][j] = temp
44+
return result
45+
46+
for i in range(rows):
47+
for j in range(cols):
48+
if board[i][j] == word[0]:
49+
if dfs(i, j, 0):
50+
return True
51+
return False

0 commit comments

Comments
 (0)