From 91671e5ab07557725b8269dc9b7319aa5b31be59 Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Mon, 10 Jun 2024 16:54:49 +0900 Subject: [PATCH 1/7] feat: Add solution for LeetCode problem 143 --- reorder-list/WhiteHyun.swift | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 reorder-list/WhiteHyun.swift diff --git a/reorder-list/WhiteHyun.swift b/reorder-list/WhiteHyun.swift new file mode 100644 index 000000000..d6438c6c6 --- /dev/null +++ b/reorder-list/WhiteHyun.swift @@ -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 + } + } +} From 766b2482997c9c44bdad65a063cc3734b9f23884 Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Mon, 10 Jun 2024 18:10:10 +0900 Subject: [PATCH 2/7] feat: Add solution for LeetCode problem 19 --- .../WhiteHyun.swift | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 remove-nth-node-from-end-of-list/WhiteHyun.swift diff --git a/remove-nth-node-from-end-of-list/WhiteHyun.swift b/remove-nth-node-from-end-of-list/WhiteHyun.swift new file mode 100644 index 000000000..7e3879535 --- /dev/null +++ b/remove-nth-node-from-end-of-list/WhiteHyun.swift @@ -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 + } +} From 5747010e420c21426f0ff7c8598ac99e4d02c8fb Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Wed, 12 Jun 2024 21:09:09 +0900 Subject: [PATCH 3/7] feat: Add solution for LeetCode problem 235 --- .../WhiteHyun.swift | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/WhiteHyun.swift diff --git a/lowest-common-ancestor-of-a-binary-search-tree/WhiteHyun.swift b/lowest-common-ancestor-of-a-binary-search-tree/WhiteHyun.swift new file mode 100644 index 000000000..81f75e997 --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/WhiteHyun.swift @@ -0,0 +1,35 @@ +// +// 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? { + if root == nil { return nil } + if root == p { return p } + if root == q { return q } + let left = lowestCommonAncestor(root?.left, p, q) + let right = lowestCommonAncestor(root?.right, p, q) + + if left != nil, right != nil { return root } + else if left != nil { return left } + else { return right } + } +} From 4cbc15e61e3147e8eb5330f1a075c664e5cf3ffc Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Wed, 12 Jun 2024 23:50:29 +0900 Subject: [PATCH 4/7] feat: Add solution for LeetCode problem 102 --- .../WhiteHyun.swift | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 binary-tree-level-order-traversal/WhiteHyun.swift diff --git a/binary-tree-level-order-traversal/WhiteHyun.swift b/binary-tree-level-order-traversal/WhiteHyun.swift new file mode 100644 index 000000000..641b8a7fc --- /dev/null +++ b/binary-tree-level-order-traversal/WhiteHyun.swift @@ -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 { + let element: T + var next: Node? + + init(element: T, next: Node? = nil) { + self.element = element + self.next = next + } +} + +// MARK: - Queue + +final class Queue { + private var head: Node? + private var tail: Node? + 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 + } +} \ No newline at end of file From bc6055c9094c260fad4b37d5da3ef01835d7f279 Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Thu, 13 Jun 2024 10:06:03 +0900 Subject: [PATCH 5/7] feat: Add solution for LeetCode problem 98 --- validate-binary-search-tree/WhiteHyun.swift | 45 +++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 validate-binary-search-tree/WhiteHyun.swift diff --git a/validate-binary-search-tree/WhiteHyun.swift b/validate-binary-search-tree/WhiteHyun.swift new file mode 100644 index 000000000..e8951254e --- /dev/null +++ b/validate-binary-search-tree/WhiteHyun.swift @@ -0,0 +1,45 @@ +// +// 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 { + func isValidBST(_ node: TreeNode?) -> Bool { + guard let node else { return true } + + var left = node.left + var right = node.right + + while left?.right != nil { + left = left?.right + } + while right?.left != nil { + right = right?.left + } + + if left?.val ?? .min < node.val, + node.val < right?.val ?? .max { + return isValidBST(node.left) && isValidBST(node.right) + } + + return false + } +} From b24e1100dc83af7707bd776b8262acabc492e3a4 Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Thu, 13 Jun 2024 10:26:38 +0900 Subject: [PATCH 6/7] refactor: Improve performance of isValidBST algorithm - Replaced redundant while loops with a single traversal. - Added a previousNode variable to keep track of the last visited node. --- validate-binary-search-tree/WhiteHyun.swift | 22 +++++---------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/validate-binary-search-tree/WhiteHyun.swift b/validate-binary-search-tree/WhiteHyun.swift index e8951254e..83a9e8fb9 100644 --- a/validate-binary-search-tree/WhiteHyun.swift +++ b/validate-binary-search-tree/WhiteHyun.swift @@ -22,24 +22,12 @@ * } */ class Solution { + private var previousNode: TreeNode? func isValidBST(_ node: TreeNode?) -> Bool { guard let node else { return true } - - var left = node.left - var right = node.right - - while left?.right != nil { - left = left?.right - } - while right?.left != nil { - right = right?.left - } - - if left?.val ?? .min < node.val, - node.val < right?.val ?? .max { - return isValidBST(node.left) && isValidBST(node.right) - } - - return false + if isValidBST(node.left) == false { return false } + if let previousNode, previousNode.val >= node.val { return false } + previousNode = node + return isValidBST(node.right) } } From 8301c3335138583eda463f02e9257d494fbf7967 Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Thu, 13 Jun 2024 10:52:16 +0900 Subject: [PATCH 7/7] refactor: Optimize lowestCommonAncestor algorithm for BST - Simplified the logic by leveraging BST properties. --- .../WhiteHyun.swift | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lowest-common-ancestor-of-a-binary-search-tree/WhiteHyun.swift b/lowest-common-ancestor-of-a-binary-search-tree/WhiteHyun.swift index 81f75e997..7cdff05fd 100644 --- a/lowest-common-ancestor-of-a-binary-search-tree/WhiteHyun.swift +++ b/lowest-common-ancestor-of-a-binary-search-tree/WhiteHyun.swift @@ -22,14 +22,12 @@ class Solution { func lowestCommonAncestor(_ root: TreeNode?, _ p: TreeNode?, _ q: TreeNode?) -> TreeNode? { - if root == nil { return nil } - if root == p { return p } - if root == q { return q } - let left = lowestCommonAncestor(root?.left, p, q) - let right = lowestCommonAncestor(root?.right, p, q) - - if left != nil, right != nil { return root } - else if left != nil { return left } - else { return right } + 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 } }