Skip to content

Commit 8cf1244

Browse files
authored
Merge pull request DaleStudy#153 from WhiteHyun/whitehyun/week8
[Hyun] Week 8 Solution Explanation
2 parents 7bfc4a0 + 5eb22d1 commit 8cf1244

File tree

5 files changed

+230
-0
lines changed

5 files changed

+230
-0
lines changed

combination-sum/WhiteHyun.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// 39. Combination Sum
3+
// https://leetcode.com/problems/combination-sum/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/27.
7+
//
8+
9+
class Solution {
10+
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
11+
let sortedCandidates = candidates.sorted()
12+
var answer: [[Int]] = []
13+
14+
func backtrack(_ sum: Int, _ start: Int, _ current: [Int]) {
15+
if sum == target {
16+
answer.append(current)
17+
return
18+
}
19+
20+
if sum > target {
21+
return
22+
}
23+
24+
for i in start ..< sortedCandidates.count {
25+
let newSum = sum + sortedCandidates[i]
26+
if newSum > target {
27+
break
28+
}
29+
backtrack(newSum, i, current + [sortedCandidates[i]])
30+
}
31+
}
32+
33+
backtrack(0, 0, [])
34+
return answer
35+
}
36+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// 105. Construct Binary Tree from Preorder and Inorder Traversal
3+
// https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/27.
7+
//
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* public var val: Int
13+
* public var left: TreeNode?
14+
* public var right: TreeNode?
15+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
16+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
17+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
18+
* self.val = val
19+
* self.left = left
20+
* self.right = right
21+
* }
22+
* }
23+
*/
24+
class Solution {
25+
func buildTree(_ preorder: [Int], _ inorder: [Int]) -> TreeNode? {
26+
let indices: [Int: Int] = Dictionary(uniqueKeysWithValues: inorder.enumerated().map { ($0.element, $0.offset) })
27+
28+
var preorderIndex = 0
29+
30+
func build(_ left: Int, _ right: Int) -> TreeNode? {
31+
if left > right { return nil }
32+
33+
let rootValue = preorder[preorderIndex]
34+
preorderIndex += 1
35+
let root = TreeNode(rootValue)
36+
37+
let mid = indices[rootValue]!
38+
39+
root.left = build(left, mid - 1)
40+
root.right = build(mid + 1, right)
41+
42+
return root
43+
}
44+
45+
return build(0, inorder.count - 1)
46+
}
47+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// 208. Implement Trie (Prefix Tree)
3+
// https://leetcode.com/problems/implement-trie-prefix-tree/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/28.
7+
//
8+
9+
// MARK: - Node
10+
11+
private class Node {
12+
var children: [Character: Node]
13+
let value: Character
14+
var isEndOfWord: Bool
15+
16+
init(children: [Character: Node] = [:], value: Character, isEndOfWord: Bool = false) {
17+
self.children = children
18+
self.value = value
19+
self.isEndOfWord = isEndOfWord
20+
}
21+
}
22+
23+
// MARK: - Trie
24+
25+
class Trie {
26+
private var root: Node
27+
28+
init() {
29+
root = .init(value: "$")
30+
}
31+
32+
func insert(_ word: String) {
33+
var node = root
34+
for character in word {
35+
if node.children[character] == nil {
36+
node.children[character] = Node(value: character)
37+
}
38+
node = node.children[character]!
39+
}
40+
node.isEndOfWord = true
41+
}
42+
43+
func search(_ word: String) -> Bool {
44+
var node = root
45+
for character in word {
46+
if node.children[character] == nil {
47+
return false
48+
}
49+
node = node.children[character]!
50+
}
51+
return node.isEndOfWord
52+
}
53+
54+
func startsWith(_ prefix: String) -> Bool {
55+
var node = root
56+
for character in prefix {
57+
if node.children[character] == nil {
58+
return false
59+
}
60+
node = node.children[character]!
61+
}
62+
return true
63+
}
64+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// 230. Kth Smallest Element in a BST
3+
// https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/27.
7+
//
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* public var val: Int
13+
* public var left: TreeNode?
14+
* public var right: TreeNode?
15+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
16+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
17+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
18+
* self.val = val
19+
* self.left = left
20+
* self.right = right
21+
* }
22+
* }
23+
*/
24+
class Solution {
25+
private var triedCount: Int = 0
26+
func kthSmallest(_ node: TreeNode?, _ k: Int) -> Int {
27+
guard let node else { return -1 }
28+
let number = kthSmallest(node.left, k)
29+
if number != -1 { return number }
30+
if triedCount + 1 == k { return node.val }
31+
triedCount += 1
32+
return kthSmallest(node.right, k)
33+
}
34+
}

word-search/WhiteHyun.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// 79. Word Search
3+
// https://leetcode.com/problems/word-search/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/28.
7+
//
8+
9+
class Solution {
10+
func exist(_ board: [[Character]], _ word: String) -> Bool {
11+
let rows = board.count
12+
let cols = board[0].count
13+
let word = Array(word)
14+
var visited = Array(repeating: Array(repeating: false, count: cols), count: rows)
15+
16+
for row in 0 ..< rows {
17+
for col in 0 ..< cols {
18+
if board[row][col] == word[0], dfs(board, word, row, col, 0, &visited) {
19+
return true
20+
}
21+
}
22+
}
23+
24+
return false
25+
}
26+
27+
private func dfs(_ board: [[Character]], _ word: [Character], _ row: Int, _ col: Int, _ index: Int, _ visited: inout [[Bool]]) -> Bool {
28+
if index == word.count {
29+
return true
30+
}
31+
32+
if row < 0 || row >= board.count || col < 0 || col >= board[0].count || visited[row][col] || board[row][col] != word[index] {
33+
return false
34+
}
35+
36+
visited[row][col] = true
37+
38+
let directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
39+
40+
for (dx, dy) in directions {
41+
if dfs(board, word, row + dx, col + dy, index + 1, &visited) {
42+
return true
43+
}
44+
}
45+
46+
visited[row][col] = false
47+
return false
48+
}
49+
}

0 commit comments

Comments
 (0)