Skip to content

Commit d9e68ec

Browse files
authored
Merge pull request #2132 from radiantchoi/main
[radiantchoi] WEEK 04 Solutions
2 parents 447de77 + 849a6ae commit d9e68ec

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

โ€Žcoin-change/radiantchoi.pyโ€Ž

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from collections import deque
2+
3+
class Solution:
4+
def coinChange(self, coins: List[int], amount: int) -> int:
5+
# BFS๋ฅผ ์ด์šฉํ•œ ์ตœ์†Œ ๋™์ „ ๊ฐœ์ˆ˜ ์ฐพ๊ธฐ
6+
# ๋™์ „์„ ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€ํ•˜๋ฉด์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ธˆ์•ก์„ ํƒ์ƒ‰ (๋ ˆ๋ฒจ๋ณ„ ํƒ์ƒ‰)
7+
# BFS ํŠน์„ฑ์ƒ ๋จผ์ € ๋„๋‹ฌํ•˜๋Š” ๊ฒฝ๋กœ๊ฐ€ ์ตœ์†Œ ๋™์ „ ๊ฐœ์ˆ˜ ๋ณด์žฅ
8+
# dp[i]: amount๊ฐ€ i์ผ ๋•Œ ์ตœ์†Œ ๋™์ „ ์‚ฌ์šฉ ํšŸ์ˆ˜
9+
# ๋ชจ๋“  ๊ฒฝ์šฐ ๋ฐฉ๋ฒ•์ด ์—†๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  -1๋กœ ์ดˆ๊ธฐํ™”
10+
dp = [-1 for _ in range(amount + 1)]
11+
12+
# bfs ์…‹์—… - ํ•ฉ์ด 0์ธ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ , ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
13+
dp[0] = 0
14+
# ํ์˜ ๊ฐ ์š”์†Œ: [์‚ฌ์šฉํ•œ ๋™์ „ ๊ฐœ์ˆ˜, ํ˜„์žฌ๊นŒ์ง€์˜ ํ•ฉ]
15+
# ์‹œ์ž‘ ์ƒํƒœ: ๋™์ „ 0๊ฐœ, ํ•ฉ 0
16+
q = deque([[0, 0]])
17+
18+
while q:
19+
# current[0]: ์‚ฌ์šฉํ•œ ๋™์ „ ๊ฐœ์ˆ˜, current[1]: ํ˜„์žฌ๊นŒ์ง€์˜ ํ•ฉ
20+
current = q.popleft()
21+
22+
# ๊ฐ๊ฐ์˜ ๋™์ „์€ ๋ฌดํ•œํžˆ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ๋ชจ๋‘ ํƒ์ƒ‰
23+
for coin in coins:
24+
estimated = current[1] + coin
25+
26+
# estimated <= amount: ๋ชฉํ‘œ ๊ธˆ์•ก์„ ์ดˆ๊ณผํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋งŒ ํƒ์ƒ‰
27+
# dp[estimated] == -1: ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ธˆ์•ก (BFS ํŠน์„ฑ์ƒ ์ตœ์ดˆ ๋„๋‹ฌ์ด ์ตœ์ )
28+
# ๋‘ ์กฐ๊ฑด ๋ชจ๋‘ ํ†ต๊ณผํ•˜๋ฉด ํ์— ์ถ”๊ฐ€ํ•˜์—ฌ ๊ณ„์† ํƒ์ƒ‰
29+
if estimated <= amount and dp[estimated] == -1:
30+
used = current[0] + 1
31+
dp[estimated] = used
32+
33+
new = [used, estimated]
34+
q.append(new)
35+
36+
# dp[amount]๊ฐ€ -1์ด๋ฉด ๋ชจ๋“  ๊ฒฝ์šฐ ๋ฐฉ๋ฒ•์ด ์—†๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ -1 ๋ฐ˜ํ™˜
37+
# ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด dp[amount] ๋ฐ˜ํ™˜
38+
return dp[amount]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public var val: Int
5+
* public var left: TreeNode?
6+
* public var right: TreeNode?
7+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
8+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
9+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
10+
* self.val = val
11+
* self.left = left
12+
* self.right = right
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
func maxDepth(_ root: TreeNode?) -> Int {
18+
var result = 0
19+
20+
traverse(root, 0, &result)
21+
22+
return result
23+
}
24+
25+
func traverse(_ node: TreeNode?, _ current: Int, _ result: inout Int) {
26+
guard let node else {
27+
result = max(current, result)
28+
return
29+
}
30+
31+
let newCurrent = current + 1
32+
33+
traverse(node.left, newCurrent, &result)
34+
traverse(node.right, newCurrent, &result)
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
class Solution:
7+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
8+
head = ListNode()
9+
result = head
10+
11+
while list1 and list2:
12+
if list1.val <= list2.val:
13+
head.next = ListNode(list1.val)
14+
head = head.next
15+
list1 = list1.next
16+
else:
17+
head.next = ListNode(list2.val)
18+
head = head.next
19+
list2 = list2.next
20+
21+
while list1:
22+
head.next = ListNode(list1.val)
23+
head = head.next
24+
list1 = list1.next
25+
26+
while list2:
27+
head.next = ListNode(list2.val)
28+
head = head.next
29+
list2 = list2.next
30+
31+
return result.next
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Solution {
2+
func exist(_ board: [[Character]], _ word: String) -> Bool {
3+
let target = Array(word) // Swift String์€ ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•œ ์ ‘๊ทผ์— ์–ด๋ ค์›€์ด ์žˆ์–ด, Array๋กœ ๋ณ€ํ™˜
4+
let threshold = target.count
5+
var board = board // inout ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜๋กœ ๋ณ€ํ™˜
6+
7+
for i in 0..<board.count {
8+
for j in 0..<board[i].count {
9+
// ๋ฐฑํŠธ๋ž˜ํ‚น ์ˆœํšŒ ํ•จ์ˆ˜
10+
// ์กฐ๊ฑด์— ๋งž๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์žˆ์œผ๋ฉด ๋ฐ”๋กœ true ๋ฐ˜ํ™˜ - early return
11+
if traverse(&board, 0, i, j, threshold, target) {
12+
return true
13+
}
14+
}
15+
}
16+
17+
// ๋ชจ๋“  ์ขŒํ‘œ๋ฅผ ์ˆœํšŒํ•œ ํ›„์—๋„ ๋งค์นญ๋˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์—†์œผ๋ฉด false ๋ฐ˜ํ™˜
18+
return false
19+
}
20+
21+
// threshold๋ฅผ target.count๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋‚˜, ๋งค๋ฒˆ ๊ณ„์‚ฐํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๋ถ€๋ชจ ํ•จ์ˆ˜์—์„œ ํ•œ ๋ฒˆ๋งŒ ๊ณ„์‚ฐํ•˜๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ์— ํฌํ•จ
22+
func traverse(_ board: inout [[Character]], _ checked: Int, _ row: Int, _ col: Int, _ threshold: Int, _ target: [Character]) -> Bool {
23+
// ์ง€๊ธˆ๊นŒ์ง€ ์ฒดํฌํ•œ ๊ธ€์ž ์ˆ˜๊ฐ€ ๋‹จ์–ด์˜ ์ด ๊ธ€์ž ์ˆ˜์™€ ๊ฐ™๋‹ค๋ฉด, true ๋ฐ˜ํ™˜
24+
// ์•„๋ž˜์˜ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋งค์นญ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๊ทธ ์ด์ „์— false๊ฐ€ ๋ฐ˜ํ™˜๋˜์—ˆ์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ
25+
if checked == threshold {
26+
return true
27+
}
28+
29+
// ํ˜„์žฌ ์ขŒํ‘œ๊ฐ€ ๋ณด๋“œ์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ๋‹ค๋ฉด false ๋ฐ˜ํ™˜
30+
guard (0..<board.count) ~= row && (0..<board[0].count) ~= col else {
31+
return false
32+
}
33+
34+
// ํ˜„์žฌ ์ขŒํ‘œ์˜ ๊ธ€์ž๊ฐ€ ๋‹จ์–ด์˜ ํ˜„์žฌ ์ฒดํฌํ•ด์•ผ ํ•  ๊ธ€์ž์™€ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋ฐ”๋กœ false ๋ฐ˜ํ™˜
35+
guard board[row][col] == target[checked] else {
36+
return false
37+
}
38+
39+
// ๋ฐฑํŠธ๋ž˜ํ‚น ์Šคํ… 1: ํ˜„์žฌ ์ขŒํ‘œ์˜ ๊ธ€์ž๋ฅผ ์ž„์‹œ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๊ณ , ํ˜„์žฌ ์ขŒํ‘œ์˜ ๊ธ€์ž๋ฅผ "#"๋กœ ๋ณ€๊ฒฝ
40+
let temp = board[row][col]
41+
board[row][col] = "#"
42+
43+
// ๋ฐฑํŠธ๋ž˜ํ‚น ์Šคํ… 2: ํ˜„์žฌ ์ขŒํ‘œ์˜ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ, ๋งค์นญ๋˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์žˆ์œผ๋ฉด true ๋ฐ˜ํ™˜
44+
let result = traverse(&board, checked + 1, row + 1, col, threshold, target)
45+
|| traverse(&board, checked + 1, row - 1, col, threshold, target)
46+
|| traverse(&board, checked + 1, row, col + 1, threshold, target)
47+
|| traverse(&board, checked + 1, row, col - 1, threshold, target)
48+
49+
// ๋ฐฑํŠธ๋ž˜ํ‚น ์Šคํ… 3: ํ˜„์žฌ ์ขŒํ‘œ์˜ ๊ธ€์ž๋ฅผ ์›๋ž˜ ๊ฐ’์œผ๋กœ ๋ณต๊ตฌ
50+
board[row][col] = temp
51+
52+
return result
53+
}
54+
}

0 commit comments

Comments
ย (0)