Skip to content

[Hyun] Week 7 Solution Explanation #127

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 7 commits into from
Jun 21, 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
107 changes: 107 additions & 0 deletions binary-tree-level-order-traversal/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//
// 102. Binary Tree Level Order Traversal
// https://leetcode.com/problems/binary-tree-level-order-traversal/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/12.
//

// MARK: - Node

private class Node<T> {
let element: T
var next: Node<T>?

init(element: T, next: Node<T>? = nil) {
self.element = element
self.next = next
}
}

// MARK: - Queue

final class Queue<T> {
private var head: Node<T>?
private var tail: Node<T>?
private var count: Int = 0

var isEmpty: Bool {
count == 0
}

func enqueue(_ element: T) {
let newNode = Node(element: element)

if let tailNode = tail {
tailNode.next = newNode
} else {
head = newNode
}

tail = newNode
count += 1
}

func dequeue() -> T? {
guard let headNode = head
else {
return nil
}

let element = headNode.element
head = headNode.next

if head == nil {
tail = nil
}

count -= 1
return element
}

func peek() -> T? {
head?.element
}

func clear() {
head = nil
tail = nil
count = 0
}
}

// MARK: ExpressibleByArrayLiteral

extension Queue: ExpressibleByArrayLiteral {
convenience init(arrayLiteral elements: T...) {
self.init()
for element in elements {
enqueue(element)
}
}
}

class Solution {
func levelOrder(_ root: TreeNode?) -> [[Int]] {
guard let root else { return [] }
var array: [[Int]] = []
let queue: Queue<(node: TreeNode, layer: Int)> = [(root, 0)]

while let (node, layer) = queue.dequeue() {
// array index ๋ฒ”์œ„์— layer๊ฐ€ ๋“ค์–ด์žˆ์ง€ ์•Š์œผ๋ฉด, ๋งˆ์ง€๋ง‰ ์š”์†Œ์— ๋นˆ ๋ฐฐ์—ด ์ถ”๊ฐ€
if (array.indices ~= layer) == false {
array.append([])
}
array[layer].append(node.val)

if node.left != nil {
queue.enqueue((node.left!, layer + 1))
}
if node.right != nil {
queue.enqueue((node.right!, layer + 1))
}
}

return array
}
}
33 changes: 33 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// 235. Lowest Common Ancestor of a Binary Search Tree
// https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/12.
//

/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*/

class Solution {
func lowestCommonAncestor(_ root: TreeNode?, _ p: TreeNode?, _ q: TreeNode?) -> TreeNode? {
guard let root else { return nil }
if root.val < p!.val, root.val < q!.val {
return lowestCommonAncestor(root.right, p, q)
} else if root.val > p!.val, root.val > q!.val {
return lowestCommonAncestor(root.left, p, q)
}
return root
}
}
37 changes: 37 additions & 0 deletions remove-nth-node-from-end-of-list/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// 19. Remove Nth Node From End of List
// https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/10.
//

/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
let answerNode = ListNode(0, head)
var footprintNode = head

for _ in 0 ..< n {
footprintNode = footprintNode?.next
}

var targetNode: ListNode? = answerNode
while footprintNode != nil {
targetNode = targetNode?.next
footprintNode = footprintNode?.next
}
targetNode?.next = targetNode?.next?.next

return answerNode.next
}
}
43 changes: 43 additions & 0 deletions reorder-list/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// 143. Reorder List
// https://leetcode.com/problems/reorder-list/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/10.
//

/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func reorderList(_ head: ListNode?) {
guard head?.next != nil else { return }

var nodeList: [ListNode] = []
var tempNode = head

// 1. ๊ฐ ๋…ธ๋“œ๋ฅผ ์„ ํ˜•์œผ๋กœ ํ™•์ธํ•˜๋ฉฐ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
while let node = tempNode {
nodeList.append(node)
tempNode = node.next
}

// 2. ๊ฐ๊ฐ next ์„ค์ •์„ snailํ•˜๊ฒŒ ๋งŒ๋“ฆ
for index in 0 ..< nodeList.count >> 1 {
nodeList[nodeList.count - index - 1].next = nodeList[index].next === nodeList[nodeList.count - index - 1] ? nil : nodeList[index].next
nodeList[index].next = nodeList[nodeList.count - index - 1]
}

//ใ€€์ถ”๊ฐ€. ๋งŒ์•ฝ ๋…ธ๋“œ๊ฐ€ ํ™€์ˆ˜๋ผ๋ฉด ๊ฐ€์šด๋ฐ ๋…ธ๋“œ์˜ next๋ฅผ nil๋กœ ์„ค์ •
if nodeList.count & 1 == 1 {
nodeList[nodeList.count >> 1].next = nil
}
}
}
33 changes: 33 additions & 0 deletions validate-binary-search-tree/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// 98. Validate Binary Search Tree
// https://leetcode.com/problems/validate-binary-search-tree/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/12.
//

/**
* 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 previousNode: TreeNode?
func isValidBST(_ node: TreeNode?) -> Bool {
guard let node else { return true }
if isValidBST(node.left) == false { return false }
if let previousNode, previousNode.val >= node.val { return false }
previousNode = node
return isValidBST(node.right)
}
}