Skip to content

[Hyun] Week 8 Solution Explanation #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions combination-sum/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// 39. Combination Sum
// https://leetcode.com/problems/combination-sum/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/27.
//

class Solution {
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
let sortedCandidates = candidates.sorted()
var answer: [[Int]] = []

func backtrack(_ sum: Int, _ start: Int, _ current: [Int]) {
if sum == target {
answer.append(current)
return
}

if sum > target {
return
}

for i in start ..< sortedCandidates.count {
let newSum = sum + sortedCandidates[i]
if newSum > target {
break
}
backtrack(newSum, i, current + [sortedCandidates[i]])
}
}

backtrack(0, 0, [])
return answer
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// 105. Construct Binary Tree from Preorder and Inorder Traversal
// https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/27.
//

/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
func buildTree(_ preorder: [Int], _ inorder: [Int]) -> TreeNode? {
let indices: [Int: Int] = Dictionary(uniqueKeysWithValues: inorder.enumerated().map { ($0.element, $0.offset) })

var preorderIndex = 0

func build(_ left: Int, _ right: Int) -> TreeNode? {
if left > right { return nil }

let rootValue = preorder[preorderIndex]
preorderIndex += 1
let root = TreeNode(rootValue)

let mid = indices[rootValue]!

root.left = build(left, mid - 1)
root.right = build(mid + 1, right)

return root
}

return build(0, inorder.count - 1)
}
}
64 changes: 64 additions & 0 deletions implement-trie-prefix-tree/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// 208. Implement Trie (Prefix Tree)
// https://leetcode.com/problems/implement-trie-prefix-tree/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/28.
//

// MARK: - Node

private class Node {
var children: [Character: Node]
let value: Character
var isEndOfWord: Bool

init(children: [Character: Node] = [:], value: Character, isEndOfWord: Bool = false) {
self.children = children
self.value = value
self.isEndOfWord = isEndOfWord
}
}

// MARK: - Trie

class Trie {
private var root: Node

init() {
root = .init(value: "$")
}

func insert(_ word: String) {
var node = root
for character in word {
if node.children[character] == nil {
node.children[character] = Node(value: character)
}
node = node.children[character]!
}
node.isEndOfWord = true
}

func search(_ word: String) -> Bool {
var node = root
for character in word {
if node.children[character] == nil {
return false
}
node = node.children[character]!
}
return node.isEndOfWord
}

func startsWith(_ prefix: String) -> Bool {
var node = root
for character in prefix {
if node.children[character] == nil {
return false
}
node = node.children[character]!
}
return true
}
}
34 changes: 34 additions & 0 deletions kth-smallest-element-in-a-bst/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// 230. Kth Smallest Element in a BST
// https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/27.
//

/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
private var triedCount: Int = 0
func kthSmallest(_ node: TreeNode?, _ k: Int) -> Int {
guard let node else { return -1 }
let number = kthSmallest(node.left, k)
if number != -1 { return number }
if triedCount + 1 == k { return node.val }
triedCount += 1
return kthSmallest(node.right, k)
}
}
49 changes: 49 additions & 0 deletions word-search/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// 79. Word Search
// https://leetcode.com/problems/word-search/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/28.
//

class Solution {
func exist(_ board: [[Character]], _ word: String) -> Bool {
let rows = board.count
let cols = board[0].count
let word = Array(word)
var visited = Array(repeating: Array(repeating: false, count: cols), count: rows)

for row in 0 ..< rows {
for col in 0 ..< cols {
if board[row][col] == word[0], dfs(board, word, row, col, 0, &visited) {
return true
}
}
}

return false
}

private func dfs(_ board: [[Character]], _ word: [Character], _ row: Int, _ col: Int, _ index: Int, _ visited: inout [[Bool]]) -> Bool {
if index == word.count {
return true
}

if row < 0 || row >= board.count || col < 0 || col >= board[0].count || visited[row][col] || board[row][col] != word[index] {
return false
}

visited[row][col] = true

let directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]

for (dx, dy) in directions {
if dfs(board, word, row + dx, col + dy, index + 1, &visited) {
return true
}
}

visited[row][col] = false
return false
}
}