From c2408fb4c926df7b7b43fb2ebcf044cfd400bf29 Mon Sep 17 00:00:00 2001 From: Jihyun Son Date: Tue, 16 Sep 2025 12:08:12 +0900 Subject: [PATCH 1/3] solve --- linked-list-cycle/sonjh1217.swift | 63 +++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 linked-list-cycle/sonjh1217.swift diff --git a/linked-list-cycle/sonjh1217.swift b/linked-list-cycle/sonjh1217.swift new file mode 100644 index 000000000..895f43f4d --- /dev/null +++ b/linked-list-cycle/sonjh1217.swift @@ -0,0 +1,63 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * public var val: Int + * public var next: ListNode? + * public init(_ val: Int) { + * self.val = val + * self.next = nil + * } + * } + */ + +extension ListNode: Equatable { + public static func == (lhs: ListNode, rhs: ListNode) -> Bool { + return lhs === rhs + } +} + +extension ListNode: Hashable { + public func hash(into hasher: inout Hasher) { + hasher.combine(ObjectIdentifier(self)) + } +} + +class Solution { + // O(n) time / O(n) space + func hasCycle(_ head: ListNode?) -> Bool { + var visitedNodes = Set() + var node = head + + while node != nil { + guard let currentNode = node else { + return false + } + + if visitedNodes.contains(currentNode) { + return true + } + + visitedNodes.insert(currentNode) + node = currentNode.next + } + + return false + } + + // O(n) time / O(1) space + func hasCycleFloyd(_ head: ListNode?) -> Bool { + var slow = head + var fast = head + + while fast != nil && fast?.next != nil { + slow = slow?.next + fast = fast?.next?.next + + if slow === fast { + return true + } + } + + return false + } +} From a5e742a7e06e254d2a5d656e7d629bc7662fc1c7 Mon Sep 17 00:00:00 2001 From: Jihyun Son Date: Thu, 18 Sep 2025 15:35:01 +0900 Subject: [PATCH 2/3] solve --- pacific-atlantic-water-flow/sonjh1217.swift | 53 +++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 pacific-atlantic-water-flow/sonjh1217.swift diff --git a/pacific-atlantic-water-flow/sonjh1217.swift b/pacific-atlantic-water-flow/sonjh1217.swift new file mode 100644 index 000000000..72b83f44f --- /dev/null +++ b/pacific-atlantic-water-flow/sonjh1217.swift @@ -0,0 +1,53 @@ +class Solution { + // O(m*n) time / O(m*n) space + func pacificAtlantic(_ heights: [[Int]]) -> [[Int]] { + var result = [[Int]]() + guard let first = heights.first else { + return result + } + let m = heights.count + let n = first.count + + var goesPacific = Array(repeating: Array(repeating: false, count: n), count: m) + var goesAtlantic = Array(repeating: Array(repeating: false, count: n), count: m) + let directions = [(1,0), (-1,0), (0,1), (0,-1)] + + func dfs(i: Int, j: Int, goes: inout [[Bool]]) { + if goes[i][j] { + return + } + goes[i][j] = true + + for (di, dj) in directions { + let ni = i + di + let nj = j + dj + if ni >= 0 + && ni < m + && nj >= 0 + && nj < n + && heights[ni][nj] >= heights[i][j] { + dfs(i: ni, j: nj, goes: &goes) + } + } + } + + for i in 0.. Date: Thu, 18 Sep 2025 15:59:13 +0900 Subject: [PATCH 3/3] =?UTF-8?q?BFS=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pacific-atlantic-water-flow/sonjh1217.swift | 67 ++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/pacific-atlantic-water-flow/sonjh1217.swift b/pacific-atlantic-water-flow/sonjh1217.swift index 72b83f44f..6d8e7958a 100644 --- a/pacific-atlantic-water-flow/sonjh1217.swift +++ b/pacific-atlantic-water-flow/sonjh1217.swift @@ -1,6 +1,71 @@ class Solution { // O(m*n) time / O(m*n) space - func pacificAtlantic(_ heights: [[Int]]) -> [[Int]] { + func pacificAtlanticBFS(_ heights: [[Int]]) -> [[Int]] { + var result = [[Int]]() + guard let first = heights.first else { + return result + } + let m = heights.count + let n = first.count + + var goesPacific = Array(repeating: Array(repeating: false, count: n), count: m) + var goesAtlantic = Array(repeating: Array(repeating: false, count: n), count: m) + let directions = [(1,0), (-1,0), (0,1), (0,-1)] + + func bfs(queue: [(Int, Int)], goes: inout [[Bool]]) { + var queue = queue + var head = 0 + for (i, j) in queue { + goes[i][j] = true + } + + while head < queue.count { + var (i, j) = queue[head] + head += 1 + + for (di, dj) in directions { + let ni = i + di + let nj = j + dj + if ni >= 0 + && ni < m + && nj >= 0 + && nj < n + && !goes[ni][nj] + && heights[ni][nj] >= heights[i][j] { + goes[ni][nj] = true + queue.append((ni, nj)) + } + } + } + } + + var pacificStarts = [(Int, Int)]() + var atlanticStarts = [(Int, Int)]() + for i in 0.. [[Int]] { var result = [[Int]]() guard let first = heights.first else { return result