Skip to content

Commit 9d339f3

Browse files
authored
Merge pull request #168 from WhiteHyun/whitehyun/week10
[Hyun] Week 10 Solution Explanation
2 parents 14b4e50 + 2a98001 commit 9d339f3

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

graph-valid-tree/WhiteHyun.swift

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// 261. Graph Valid Tree
3+
// https://leetcode.com/problems/graph-valid-tree/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/06.
7+
//
8+
9+
final class Solution {
10+
func validTree(_ n: Int, _ edges: [[Int]]) -> Bool {
11+
guard edges.count == n - 1
12+
else {
13+
return false
14+
}
15+
16+
var dictionary: [Int: [Int]] = [:]
17+
var visited: Set<Int> = []
18+
19+
for edge in edges {
20+
dictionary[edge[0], default: []].append(edge[1])
21+
dictionary[edge[1], default: []].append(edge[0])
22+
}
23+
24+
func dfs(parent: Int, node: Int) {
25+
if visited.contains(node) {
26+
return
27+
}
28+
29+
visited.insert(node)
30+
31+
if let childNodes = dictionary[node] {
32+
for childNode in childNodes where childNode != parent {
33+
dfs(parent: node, node: childNode)
34+
}
35+
}
36+
}
37+
38+
dfs(parent: -1, node: 0)
39+
40+
return visited.count == n
41+
}
42+
}

house-robber-ii/WhiteHyun.swift

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// 213. House Robber II
3+
// https://leetcode.com/problems/house-robber-ii/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/06.
7+
//
8+
9+
class Solution {
10+
func rob(_ nums: [Int]) -> Int {
11+
if nums.count < 3 { return nums.max()! }
12+
var current = 0
13+
var previous = 0
14+
15+
// 첫 번째 집을 턴 경우
16+
for element in nums.dropLast() {
17+
(previous, current) = (current, max(element + previous, current))
18+
}
19+
20+
var current2 = 0
21+
var previous2 = 0
22+
23+
// 첫 번째 집을 털지 않은 경우
24+
for element in nums.dropFirst() {
25+
(previous2, current2) = (current2, max(element + previous2, current2))
26+
}
27+
28+
return max(current, previous, current2, previous2)
29+
}
30+
}

house-robber/WhiteHyun.swift

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// 198. House Robber
3+
// https://leetcode.com/problems/house-robber/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/06.
7+
//
8+
9+
class Solution {
10+
func rob(_ nums: [Int]) -> Int {
11+
var previous = 0
12+
var current = 0
13+
14+
for element in nums {
15+
(previous, current) = (current, max(element + previous, current))
16+
}
17+
return max(previous, current)
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// 5. Longest Palindromic Substring
3+
// https://leetcode.com/problems/longest-palindromic-substring/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/06.
7+
//
8+
9+
class Solution {
10+
func longestPalindrome(_ s: String) -> String {
11+
let array = Array(s)
12+
if array.count == 1 { return s }
13+
14+
var start = 0
15+
var maxLength = 1
16+
17+
func expandAroundCenter(_ left: Int, _ right: Int) -> Int {
18+
var l = left
19+
var r = right
20+
while l >= 0, r < array.count, array[l] == array[r] {
21+
l -= 1
22+
r += 1
23+
}
24+
return r - l - 1
25+
}
26+
27+
for index in array.indices {
28+
let length1 = expandAroundCenter(index, index) // 홀수 길이
29+
let length2 = expandAroundCenter(index, index + 1) // 짝수 길이
30+
let length = max(length1, length2)
31+
32+
if length > maxLength {
33+
start = index - (length - 1) / 2
34+
maxLength = length
35+
}
36+
}
37+
return String(array[start ..< start + maxLength])
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// 323. Number of Connected Components in an Undirected Graph
3+
// https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/06.
7+
//
8+
9+
final class Solution {
10+
func countComponents(_ n: Int, _ edges: [[Int]]) -> Int {
11+
var visited: Set<Int> = []
12+
var count = 0
13+
14+
var graph: [Int: [Int]] = [:]
15+
16+
for edge in edges {
17+
graph[edge[0], default: []].append(edge[1])
18+
graph[edge[1], default: []].append(edge[0])
19+
}
20+
21+
func dfs(_ node: Int) {
22+
if visited.contains(node) {
23+
return
24+
}
25+
26+
visited.insert(node)
27+
28+
for nextNode in graph[node, default: []] {
29+
dfs(nextNode)
30+
}
31+
}
32+
33+
for node in 0 ..< n where !visited.contains(node) {
34+
count += 1
35+
dfs(node)
36+
}
37+
38+
return count
39+
}
40+
}

0 commit comments

Comments
 (0)