Skip to content

Commit 7938bc7

Browse files
authored
Merge pull request #1911 from sonjh1217/main
[sonjh1217] WEEK 09 solutions
2 parents 1ba4be0 + f0ea5e9 commit 7938bc7

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

linked-list-cycle/sonjh1217.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public var val: Int
5+
* public var next: ListNode?
6+
* public init(_ val: Int) {
7+
* self.val = val
8+
* self.next = nil
9+
* }
10+
* }
11+
*/
12+
13+
extension ListNode: Equatable {
14+
public static func == (lhs: ListNode, rhs: ListNode) -> Bool {
15+
return lhs === rhs
16+
}
17+
}
18+
19+
extension ListNode: Hashable {
20+
public func hash(into hasher: inout Hasher) {
21+
hasher.combine(ObjectIdentifier(self))
22+
}
23+
}
24+
25+
class Solution {
26+
// O(n) time / O(n) space
27+
func hasCycle(_ head: ListNode?) -> Bool {
28+
var visitedNodes = Set<ListNode>()
29+
var node = head
30+
31+
while node != nil {
32+
guard let currentNode = node else {
33+
return false
34+
}
35+
36+
if visitedNodes.contains(currentNode) {
37+
return true
38+
}
39+
40+
visitedNodes.insert(currentNode)
41+
node = currentNode.next
42+
}
43+
44+
return false
45+
}
46+
47+
// O(n) time / O(1) space
48+
func hasCycleFloyd(_ head: ListNode?) -> Bool {
49+
var slow = head
50+
var fast = head
51+
52+
while fast != nil && fast?.next != nil {
53+
slow = slow?.next
54+
fast = fast?.next?.next
55+
56+
if slow === fast {
57+
return true
58+
}
59+
}
60+
61+
return false
62+
}
63+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
class Solution {
2+
// O(m*n) time / O(m*n) space
3+
func pacificAtlanticBFS(_ heights: [[Int]]) -> [[Int]] {
4+
var result = [[Int]]()
5+
guard let first = heights.first else {
6+
return result
7+
}
8+
let m = heights.count
9+
let n = first.count
10+
11+
var goesPacific = Array(repeating: Array(repeating: false, count: n), count: m)
12+
var goesAtlantic = Array(repeating: Array(repeating: false, count: n), count: m)
13+
let directions = [(1,0), (-1,0), (0,1), (0,-1)]
14+
15+
func bfs(queue: [(Int, Int)], goes: inout [[Bool]]) {
16+
var queue = queue
17+
var head = 0
18+
for (i, j) in queue {
19+
goes[i][j] = true
20+
}
21+
22+
while head < queue.count {
23+
let (i, j) = queue[head]
24+
head += 1
25+
26+
for (di, dj) in directions {
27+
let ni = i + di
28+
let nj = j + dj
29+
if ni >= 0
30+
&& ni < m
31+
&& nj >= 0
32+
&& nj < n
33+
&& !goes[ni][nj]
34+
&& heights[ni][nj] >= heights[i][j] {
35+
goes[ni][nj] = true
36+
queue.append((ni, nj))
37+
}
38+
}
39+
}
40+
}
41+
42+
var pacificStarts = [(Int, Int)]()
43+
var atlanticStarts = [(Int, Int)]()
44+
for i in 0..<m {
45+
pacificStarts.append((i, 0))
46+
atlanticStarts.append((i, n-1))
47+
}
48+
49+
for j in 0..<n {
50+
pacificStarts.append((0, j))
51+
atlanticStarts.append((m-1, j))
52+
}
53+
54+
bfs(queue: pacificStarts, goes: &goesPacific)
55+
bfs(queue: atlanticStarts, goes: &goesAtlantic)
56+
57+
for i in 0..<m {
58+
for j in 0..<n {
59+
if goesPacific[i][j] && goesAtlantic[i][j] {
60+
result.append([i, j])
61+
}
62+
}
63+
}
64+
return result
65+
}
66+
67+
// O(m*n) time / O(m*n) space
68+
func pacificAtlanticDFS(_ heights: [[Int]]) -> [[Int]] {
69+
var result = [[Int]]()
70+
guard let first = heights.first else {
71+
return result
72+
}
73+
let m = heights.count
74+
let n = first.count
75+
76+
var goesPacific = Array(repeating: Array(repeating: false, count: n), count: m)
77+
var goesAtlantic = Array(repeating: Array(repeating: false, count: n), count: m)
78+
let directions = [(1,0), (-1,0), (0,1), (0,-1)]
79+
80+
func dfs(i: Int, j: Int, goes: inout [[Bool]]) {
81+
if goes[i][j] {
82+
return
83+
}
84+
goes[i][j] = true
85+
86+
for (di, dj) in directions {
87+
let ni = i + di
88+
let nj = j + dj
89+
if ni >= 0
90+
&& ni < m
91+
&& nj >= 0
92+
&& nj < n
93+
&& heights[ni][nj] >= heights[i][j] {
94+
dfs(i: ni, j: nj, goes: &goes)
95+
}
96+
}
97+
}
98+
99+
for i in 0..<m {
100+
dfs(i: i, j: 0, goes: &goesPacific)
101+
dfs(i: i, j: n-1, goes: &goesAtlantic)
102+
}
103+
104+
for j in 0..<n {
105+
dfs(i: 0, j: j, goes: &goesPacific )
106+
dfs(i: m-1, j: j, goes: &goesAtlantic )
107+
}
108+
109+
for i in 0..<m {
110+
for j in 0..<n {
111+
if goesPacific[i][j] && goesAtlantic[i][j] {
112+
result.append([i, j])
113+
}
114+
}
115+
}
116+
return result
117+
}
118+
}

0 commit comments

Comments
 (0)