Skip to content

Commit 32bf681

Browse files
authored
Merge pull request #1354 from JustHm/main
2 parents 698f669 + a5bcbc9 commit 32bf681

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

coin-change/JustHm.swift

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// time: O(n*amount) space: O(amount)
2+
class Solution {
3+
func coinChange(_ coins: [Int], _ amount: Int) -> Int {
4+
var queue: [(count: Int, total: Int)] = [(0, 0)] // (동전 개수, 누적 금액)
5+
var visited = Set<Int>() // 중복 제거용
6+
var index = 0 // 큐의 head
7+
8+
while index < queue.count {
9+
let (count, total) = queue[index]
10+
index += 1
11+
12+
if total == amount {
13+
return count
14+
}
15+
if visited.contains(total) {
16+
continue
17+
}
18+
visited.insert(total)
19+
// BFS 방식
20+
for coin in coins { // 모든 코인을 현재 누적금액에 한 번씩 더해서 큐에 저장
21+
let newTotal = total + coin
22+
if newTotal <= amount {
23+
queue.append((count + 1, newTotal))
24+
}
25+
}
26+
}
27+
return -1
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// time: O(n), space: O(1)
2+
// 투포인터로 양 끝에서 하나씩 줄여가며 가장 작은값 찾아 반환하기.
3+
class Solution {
4+
func findMin(_ nums: [Int]) -> Int {
5+
guard nums.count != 1 else { return nums.first! }
6+
7+
var answer = Int.max
8+
var left = 0
9+
var right = nums.count
10+
11+
while right - left > 0 {
12+
let temp = min(nums[left], nums[right - 1])
13+
answer = min(answer, temp)
14+
left += 1
15+
right -= 1
16+
}
17+
18+
return answer
19+
}
20+
}
21+
// time: O(log n), space: O(1)
22+
// 중간 값과 오른쪽 값중 가장 작은쪽을 기준으로 잘라가며 탐색함 (이진 탐색)
23+
class Solution {
24+
func findMin(_ nums: [Int]) -> Int {
25+
guard nums.count != 1 else { return nums.first! }
26+
27+
var left = 0
28+
var right = nums.count - 1
29+
30+
while left < right {
31+
let mid = (left + right) / 2
32+
if nums[mid] < nums[right] { right = mid }
33+
else { left = mid + 1 }
34+
}
35+
return nums[right]
36+
}
37+
}
38+
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// time: O(n) space: O(n)
2+
class Solution {
3+
func maxDepth(_ root: TreeNode?) -> Int {
4+
return dfs(root, 1)
5+
}
6+
7+
func dfs(_ node: TreeNode?, _ n: Int) -> Int {
8+
guard let node = node else { return n - 1 }
9+
return max(dfs(node.left, n+1), dfs(node.right, n+1))
10+
}
11+
}

merge-two-sorted-lists/JustHm.swift

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// time: O(n+m) space: O(1)
2+
class Solution {
3+
func mergeTwoLists(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? {
4+
// 전처리
5+
guard list1 != nil, list2 != nil else { return list1 ?? list2 }
6+
// 변수 정의
7+
var answer: ListNode? = ListNode(0)
8+
var top: ListNode? = answer
9+
var list1 = list1
10+
var list2 = list2
11+
// 2개의 ListNode를 순회하면서 값을 보고 작은 순으로 저장 (ListNode에)
12+
while list1 != nil && list2 != nil {
13+
if let value1 = list1?.val, let value2 = list2?.val {
14+
if value1 < value2 {
15+
answer?.next = ListNode(value1)
16+
list1 = list1?.next
17+
}
18+
else {
19+
answer?.next = ListNode(value2)
20+
list2 = list2?.next
21+
}
22+
answer = answer?.next
23+
}
24+
}
25+
// 남은 노드들 연결하기
26+
answer?.next = list1 ?? list2
27+
return top?.next
28+
}
29+
}

word-search/JustHm.swift

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// time: O(N * M * 4^L) space: O(N * M + L)
2+
// N 행 - M 열 - L word의 길이 (4방향을 탐색하기에 4^L)
3+
class Solution {
4+
func exist(_ board: [[Character]], _ word: String) -> Bool {
5+
let words = Array(word)
6+
var visited = Array(repeating: Array(repeating: false, count: board[0].count), count: board.count)
7+
var startPoints = [(Int, Int)]()
8+
// 먼저 시작 가능한 인덱스들 찾기 - O(n^2)
9+
for i in board.indices {
10+
for j in board[0].indices {
11+
if board[i][j] == words[0] {
12+
startPoints.append((i,j))
13+
}
14+
}
15+
}
16+
// 문자 찾아가기위한 DFS 내부함수 (backtracking)
17+
func dfs(_ row: Int, _ col: Int, _ index: Int) -> Bool {
18+
if row < 0 || col < 0 || row >= board.count || col >= board[0].count { return false }
19+
if visited[row][col] { return false }
20+
if board[row][col] != words[index] { return false }
21+
if index + 1 == word.count { return true }
22+
23+
visited[row][col] = true
24+
25+
for dir in [(0, 1), (1, 0), (0, -1), (-1, 0)] {
26+
let nextRow = row + dir.0
27+
let nextCol = col + dir.1
28+
if dfs(nextRow, nextCol, index + 1) {
29+
return true
30+
}
31+
}
32+
33+
visited[row][col] = false
34+
return false
35+
}
36+
// 문자 찾기
37+
for pos in startPoints {
38+
if dfs(pos.0, pos.1, 0) { return true }
39+
}
40+
return false
41+
}
42+
}

0 commit comments

Comments
 (0)