From 59d8483b1e688cc5119be20e37fcd83d8c1b1889 Mon Sep 17 00:00:00 2001 From: Jihyun Son Date: Mon, 6 Oct 2025 13:58:10 +0900 Subject: [PATCH 1/6] solve --- same-tree/sonjh1217.swift | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 same-tree/sonjh1217.swift diff --git a/same-tree/sonjh1217.swift b/same-tree/sonjh1217.swift new file mode 100644 index 000000000..74154ee3c --- /dev/null +++ b/same-tree/sonjh1217.swift @@ -0,0 +1,30 @@ +/** + * 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 { + // O(n) time, O(h) space h = 트리의 높이. 최악: n, 평균: log n + func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool { + switch (p, q) { + case (nil, nil): + return true + case let (p?, q?): + return p.val == q.val + && isSameTree(p.left, q.left) + && isSameTree(p.right, q.right) + default: + return false + } + } +} From ae26fb8c0f96253f176ae8a8d6903b8aaad2e694 Mon Sep 17 00:00:00 2001 From: Jihyun Son Date: Tue, 7 Oct 2025 14:35:41 +0900 Subject: [PATCH 2/6] solve --- .../sonjh1217.swift | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 remove-nth-node-from-end-of-list/sonjh1217.swift diff --git a/remove-nth-node-from-end-of-list/sonjh1217.swift b/remove-nth-node-from-end-of-list/sonjh1217.swift new file mode 100644 index 000000000..2bba29006 --- /dev/null +++ b/remove-nth-node-from-end-of-list/sonjh1217.swift @@ -0,0 +1,50 @@ +/** + * 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 { + // O(n) time / O(n) space + func removeNthFromEndList(_ head: ListNode?, _ n: Int) -> ListNode? { + var nodes = [ListNode]() + var node = head + while let current = node { + nodes.append(current) + node = current.next + } + + let indexToRemove = nodes.count - n + + if indexToRemove == 0 { + return head?.next + } + + nodes[indexToRemove - 1].next = indexToRemove + 1 < nodes.count ? nodes[indexToRemove + 1] : nil + return head + } + + // O(n) time / O(1) space + func removeNthFromEndPointer(_ head: ListNode?, _ n: Int) -> ListNode? { + let dummy = ListNode(0) + dummy.next = head + var post: ListNode? = dummy + var prev: ListNode? = dummy + + for _ in 0...n { + post = post?.next + } + + while post != nil { + prev = prev?.next + post = post?.next + } + + prev?.next = prev?.next?.next + return dummy.next + } +} From 450f2ca2718827be03df61d2330c66b352927927 Mon Sep 17 00:00:00 2001 From: Jihyun Son Date: Wed, 8 Oct 2025 15:49:16 +0900 Subject: [PATCH 3/6] solve --- .../sonjh1217.swift | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 number-of-connected-components-in-an-undirected-graph/sonjh1217.swift diff --git a/number-of-connected-components-in-an-undirected-graph/sonjh1217.swift b/number-of-connected-components-in-an-undirected-graph/sonjh1217.swift new file mode 100644 index 000000000..aea43f945 --- /dev/null +++ b/number-of-connected-components-in-an-undirected-graph/sonjh1217.swift @@ -0,0 +1,41 @@ +class Solution { + // O(n+m) (m = edges.count) time / O(n+m) space + func countComponents(_ n: Int, _ edges: [[Int]]) -> Int { + var graph = [[Int]](repeating: [], count: n) + for edge in edges { + graph[edge[0]].append(edge[1]) + graph[edge[1]].append(edge[0]) + } + + var visited = [Bool](repeating: false, count: n) + var connectedComponents = 0 + + for i in 0.. head { + let current = queue[head] + head += 1 + + for node in graph[current] { + if !visited[node] { + visited[node] = true + queue.append(node) + } + } + } + connectedComponents += 1 + } + + return connectedComponents + } +} + +print(Solution().countComponents(3, [[0,1], [0,2]])) +print(Solution().countComponents(6, [[0,1], [1,2], [2, 3], [4, 5]] )) From 04b00a3b50850de69c3ab4260d176ea749f8bac3 Mon Sep 17 00:00:00 2001 From: Jihyun Son Date: Thu, 9 Oct 2025 14:25:51 +0900 Subject: [PATCH 4/6] solve --- non-overlapping-intervals/sonjh1217.swift | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 non-overlapping-intervals/sonjh1217.swift diff --git a/non-overlapping-intervals/sonjh1217.swift b/non-overlapping-intervals/sonjh1217.swift new file mode 100644 index 000000000..b1354860a --- /dev/null +++ b/non-overlapping-intervals/sonjh1217.swift @@ -0,0 +1,23 @@ +class Solution { + /// 구간을 다루는데, 겹치지 않게 최대/최소로 뽑는 문제 -> 끝점 기준 정렬 + 그리디 + /// 가장 빨리 끝나는 것들을 선택해야 최대한 많이 선택할 수 있음 + /// 끝점 기준 오름차순 정렬 nlogn + /// 순차적으로 돌면서 겹치면 제거(앞 것들보다 끝점이 더 뒤라서, 시작만 확인하면 됨) + /// O(nlogn) time / O(n) space + func eraseOverlapIntervals(_ intervals: [[Int]]) -> Int { + let intervals = intervals.sorted { $0[1] < $1[1] } + var end = intervals[0][1] + var removal = 0 + + for i in 1.. Date: Thu, 9 Oct 2025 14:28:21 +0900 Subject: [PATCH 5/6] =?UTF-8?q?print=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sonjh1217.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/number-of-connected-components-in-an-undirected-graph/sonjh1217.swift b/number-of-connected-components-in-an-undirected-graph/sonjh1217.swift index aea43f945..616e285aa 100644 --- a/number-of-connected-components-in-an-undirected-graph/sonjh1217.swift +++ b/number-of-connected-components-in-an-undirected-graph/sonjh1217.swift @@ -37,5 +37,3 @@ class Solution { } } -print(Solution().countComponents(3, [[0,1], [0,2]])) -print(Solution().countComponents(6, [[0,1], [1,2], [2, 3], [4, 5]] )) From f444988da327f91ffbdde4fc09a3317454eb1be2 Mon Sep 17 00:00:00 2001 From: Jihyun Son Date: Fri, 10 Oct 2025 11:23:53 +0900 Subject: [PATCH 6/6] solve --- .../sonjh1217.swift | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 serialize-and-deserialize-binary-tree/sonjh1217.swift diff --git a/serialize-and-deserialize-binary-tree/sonjh1217.swift b/serialize-and-deserialize-binary-tree/sonjh1217.swift new file mode 100644 index 000000000..b2a7152e5 --- /dev/null +++ b/serialize-and-deserialize-binary-tree/sonjh1217.swift @@ -0,0 +1,87 @@ +/** + * 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 Codec { + // O(n) time / O(n) space + func serialize(_ root: TreeNode?) -> String { + var serializedNodes = [String]() + var queueNodes = [TreeNode?]() + queueNodes.append(root) + var head = 0 + + while head < queueNodes.count { + let node = queueNodes[head] + head += 1 + + if let node { + serializedNodes.append("\(node.val)") + + queueNodes.append(node.left) + queueNodes.append(node.right) + } else { + serializedNodes.append("null") + } + + } + + return serializedNodes.joined(separator: ",") + } + + // O(n) time / O(n) space + func deserialize(_ data: String) -> TreeNode? { + let serializedTree = data + var nodeVals = serializedTree.split(separator: ",") + + guard let first = nodeVals.first, + let firstVal = Int(first) else { + return nil + } + + let root = TreeNode(firstVal) + var queueNodes = [TreeNode]() + queueNodes.append(root) + var head = 0 + var isLeft = true + + for i in 1..