Skip to content

Commit b3b9556

Browse files
authored
Merge pull request #193 from WhiteHyun/whitehyun/week12
[Hyun] Week 12 Solution Explanation
2 parents aeb6a8e + 57b2a21 commit b3b9556

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

jump-game/WhiteHyun.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// 55. Jump Game
3+
// https://leetcode.com/problems/jump-game/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/20.
7+
//
8+
9+
class Solution {
10+
func canJump(_ numbers: [Int]) -> Bool {
11+
var goal = numbers.endIndex - 1
12+
13+
for index in numbers.indices.dropLast().reversed() where index + numbers[index] >= goal {
14+
goal = index
15+
}
16+
17+
return goal == 0
18+
}
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// 1143. Longest Common Subsequence
3+
// https://leetcode.com/problems/longest-common-subsequence/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/20.
7+
//
8+
9+
class Solution {
10+
func longestCommonSubsequence(_ text1: String, _ text2: String) -> Int {
11+
let m = text1.count
12+
let n = text2.count
13+
14+
var dp: [[Int]] = .init(repeating: .init(repeating: 0, count: n + 1), count: m + 1)
15+
16+
let text1Array = Array(text1)
17+
let text2Array = Array(text2)
18+
19+
for i in 1 ... m {
20+
for j in 1 ... n {
21+
if text1Array[i - 1] == text2Array[j - 1] {
22+
dp[i][j] = dp[i - 1][j - 1] + 1
23+
} else {
24+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
25+
}
26+
}
27+
}
28+
29+
return dp[m][n]
30+
}
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// 300. Longest Increasing Subsequence
3+
// https://leetcode.com/problems/longest-increasing-subsequence/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/20.
7+
//
8+
9+
class Solution {
10+
func lengthOfLIS(_ nums: [Int]) -> Int {
11+
var tails: [Int] = [nums[0]]
12+
13+
for index in 1 ..< nums.count {
14+
if nums[index] > tails.last! {
15+
tails.append(nums[index])
16+
} else {
17+
tails[binarySearch(tails, target: nums[index])] = nums[index]
18+
}
19+
}
20+
21+
return tails.count
22+
}
23+
24+
private func binarySearch(_ arr: [Int], target: Int) -> Int {
25+
var left = 0
26+
var right = arr.count - 1
27+
28+
while left <= right {
29+
let mid = (left + right) >> 1
30+
if arr[mid] < target {
31+
left = mid + 1
32+
} else {
33+
right = mid - 1
34+
}
35+
}
36+
return left
37+
}
38+
}

maximum-subarray/WhiteHyun.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// 53. Maximum Subarray
3+
// https://leetcode.com/problems/maximum-subarray/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/20.
7+
//
8+
9+
class Solution {
10+
func maxSubArray(_ numbers: [Int]) -> Int {
11+
var tracking = numbers[0]
12+
var answer = numbers[0]
13+
for value in numbers.dropFirst() {
14+
tracking = max(tracking + value, value)
15+
if tracking > answer {
16+
answer = tracking
17+
}
18+
}
19+
20+
return answer
21+
}
22+
}

unique-paths/WhiteHyun.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// 62. Unique Paths
3+
// https://leetcode.com/problems/unique-paths/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/20.
7+
//
8+
9+
class Solution {
10+
func uniquePaths(_ m: Int, _ n: Int) -> Int {
11+
if m == 1 || n == 1 { return 1 }
12+
var dp: [[Int]] = .init(repeating: .init(repeating: 1, count: n), count: m)
13+
14+
for i in 1 ..< m {
15+
for j in 1 ..< n {
16+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
17+
}
18+
}
19+
20+
return dp[m - 1][n - 1]
21+
}
22+
}

0 commit comments

Comments
 (0)