Skip to content

Commit 4bcdf88

Browse files
authored
Merge pull request DaleStudy#154 from WhiteHyun/whitehyun/week9
[Hyun] Week 9 Solution Explanation
2 parents 296255a + 1fa3e67 commit 4bcdf88

File tree

5 files changed

+247
-0
lines changed

5 files changed

+247
-0
lines changed

clone-graph/WhiteHyun.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// 133. Clone Graph
3+
// https://leetcode.com/problems/clone-graph/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/28.
7+
//
8+
9+
/**
10+
* Definition for a Node.
11+
* public class Node {
12+
* public var val: Int
13+
* public var neighbors: [Node?]
14+
* public init(_ val: Int) {
15+
* self.val = val
16+
* self.neighbors = []
17+
* }
18+
* }
19+
*/
20+
21+
class Solution {
22+
var cache: [Int: Node] = [:]
23+
24+
func cloneGraph(_ originalNode: Node?) -> Node? {
25+
guard let originalNode
26+
else {
27+
return nil
28+
}
29+
30+
guard cache[originalNode.val] == nil
31+
else { return cache[originalNode.val]! }
32+
33+
let node = Node(originalNode.val)
34+
cache[originalNode.val] = node
35+
36+
37+
node.neighbors = originalNode.neighbors.map(cloneGraph)
38+
39+
return node
40+
}
41+
}

course-schedule/WhiteHyun.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// 207. Course Schedule
3+
// https://leetcode.com/problems/course-schedule/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/30.
7+
//
8+
9+
class Solution {
10+
func canFinish(_ numCourses: Int, _ prerequisites: [[Int]]) -> Bool {
11+
var graph: [Int: [Int]] = [:]
12+
var visited: [Int: Bool] = [:]
13+
14+
// 그래프 구성
15+
for prereq in prerequisites {
16+
let course = prereq[0]
17+
let prerequisite = prereq[1]
18+
graph[course, default: []].append(prerequisite)
19+
}
20+
21+
func dfs(_ course: Int) -> Bool {
22+
if visited[course] == true { return false }
23+
if visited[course] == false { return true }
24+
25+
visited[course] = true
26+
27+
if let prerequisites = graph[course] {
28+
for prereq in prerequisites {
29+
if !dfs(prereq) { return false }
30+
}
31+
}
32+
33+
visited[course] = false
34+
return true
35+
}
36+
37+
for course in 0 ..< numCourses {
38+
if !dfs(course) { return false }
39+
}
40+
41+
return true
42+
}
43+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// 211. Design Add and Search Words Data Structure
3+
// https://leetcode.com/problems/design-add-and-search-words-data-structure/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/28.
7+
//
8+
9+
private final class Node {
10+
var children: [Character: Node]
11+
var endOfWord: Bool
12+
13+
init(
14+
children: [Character: Node] = [:],
15+
endOfWord: Bool = false
16+
) {
17+
self.children = children
18+
self.endOfWord = endOfWord
19+
}
20+
}
21+
22+
final class WordDictionary {
23+
private let root: Node
24+
25+
init() {
26+
root = Node()
27+
}
28+
29+
func addWord(_ word: String) {
30+
var node = root
31+
for character in word {
32+
if node.children[character] == nil {
33+
node.children[character] = Node()
34+
}
35+
node = node.children[character]!
36+
}
37+
node.endOfWord = true
38+
}
39+
40+
func search(_ word: String) -> Bool {
41+
let word = Array(word)
42+
func dfs(index: Int, node: Node) -> Bool {
43+
guard index < word.count else { return node.endOfWord }
44+
45+
let character = word[index]
46+
if character == "." {
47+
return node.children.values.contains { dfs(index: index + 1, node: $0) }
48+
} else if let nextNode = node.children[character] {
49+
return dfs(index: index + 1, node: nextNode)
50+
}
51+
return false
52+
}
53+
return dfs(index: 0, node: root)
54+
}
55+
}

number-of-islands/WhiteHyun.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// 200. Number of Islands
3+
// https://leetcode.com/problems/number-of-islands/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/28.
7+
//
8+
9+
class Solution {
10+
func numIslands(_ grid: [[Character]]) -> Int {
11+
var visited: [[Bool]] = .init(
12+
repeating: .init(repeating: false, count: grid[0].count),
13+
count: grid.count
14+
)
15+
var islands = 0
16+
17+
for row in 0 ..< grid.count {
18+
for col in 0 ..< grid[row].count where grid[row][col] == "1" && !visited[row][col] {
19+
visited[row][col] = true
20+
dfs(grid, &visited, row, col)
21+
islands += 1
22+
}
23+
}
24+
25+
return islands
26+
}
27+
28+
private func dfs(
29+
_ grid: [[Character]],
30+
_ visited: inout [[Bool]],
31+
_ x: Int,
32+
_ y: Int
33+
) {
34+
for (dx, dy) in [(-1, 0), (1, 0), (0, -1), (0, 1)] {
35+
let nx = dx + x
36+
let ny = dy + y
37+
guard grid.indices ~= nx,
38+
grid[x].indices ~= ny,
39+
visited[nx][ny] == false,
40+
grid[nx][ny] == "1"
41+
else {
42+
continue
43+
}
44+
visited[nx][ny] = true
45+
dfs(grid, &visited, nx, ny)
46+
}
47+
}
48+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// 417. Pacific Atlantic Water Flow
3+
// https://leetcode.com/problems/pacific-atlantic-water-flow/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/29.
7+
//
8+
9+
class Solution {
10+
func pacificAtlantic(_ heights: [[Int]]) -> [[Int]] {
11+
var result: [[Int]] = []
12+
for i in heights.indices {
13+
for j in heights[i].indices where isValid(i, j, heights) {
14+
result.append([i, j])
15+
}
16+
}
17+
18+
return result
19+
}
20+
21+
private func isValid(_ i: Int, _ j: Int, _ heights: [[Int]]) -> Bool {
22+
var visited: [[Bool]] = .init(
23+
repeating: .init(repeating: false, count: heights[i].count),
24+
count: heights.count
25+
)
26+
visited[i][j] = true
27+
var pacificOceanFlag = i == 0 || j == 0
28+
var atlanticOceanFlag = i == heights.count - 1 || j == heights[i].count - 1
29+
30+
func dfs(_ x: Int, _ y: Int) {
31+
if pacificOceanFlag, atlanticOceanFlag { return }
32+
for (dx, dy) in [(-1, 0), (1, 0), (0, -1), (0, 1)] {
33+
let nx = x + dx
34+
let ny = y + dy
35+
36+
guard heights.indices ~= nx,
37+
heights[nx].indices ~= ny,
38+
visited[nx][ny] == false,
39+
heights[nx][ny] <= heights[x][y]
40+
else {
41+
continue
42+
}
43+
44+
visited[nx][ny] = true
45+
if nx == 0 || ny == 0 {
46+
pacificOceanFlag = true
47+
}
48+
if nx == heights.endIndex - 1 || ny == heights[nx].endIndex - 1 {
49+
atlanticOceanFlag = true
50+
}
51+
52+
dfs(nx, ny)
53+
}
54+
}
55+
56+
dfs(i, j)
57+
58+
return pacificOceanFlag && atlanticOceanFlag
59+
}
60+
}

0 commit comments

Comments
 (0)