-
-
Notifications
You must be signed in to change notification settings - Fork 203
[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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
91671e5
feat: Add solution for LeetCode problem 143
WhiteHyun 766b248
feat: Add solution for LeetCode problem 19
WhiteHyun 5747010
feat: Add solution for LeetCode problem 235
WhiteHyun 4cbc15e
feat: Add solution for LeetCode problem 102
WhiteHyun bc6055c
feat: Add solution for LeetCode problem 98
WhiteHyun b24e110
refactor: Improve performance of isValidBST algorithm
WhiteHyun 8301c33
refactor: Optimize lowestCommonAncestor algorithm for BST
WhiteHyun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
lowest-common-ancestor-of-a-binary-search-tree/WhiteHyun.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
WhiteHyun marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.