Skip to content

Commit adfb981

Browse files
authored
Merge pull request soapyigu#361 from soapyigu/String
String
2 parents f147478 + b734c5e commit adfb981

File tree

3 files changed

+33
-45
lines changed

3 files changed

+33
-45
lines changed

DP/LongestIncreasingSubsequence.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,34 @@
66

77
class LongestIncreasingSubsequence {
88
func lengthOfLIS(_ nums: [Int]) -> Int {
9-
guard let first = nums.first else {
10-
return 0
11-
}
12-
13-
var ends = [first]
9+
var res = [nums[0]]
1410

1511
for i in 1..<nums.count {
16-
17-
// find first greater ends number
18-
var left = 0, right = ends.count
19-
20-
while left < right {
21-
let mid = (right - left) / 2 + left
22-
23-
if ends[mid] < nums[i] {
24-
left = mid + 1
25-
} else {
26-
right = mid
27-
}
28-
}
29-
30-
if right >= ends.count {
31-
ends.append(nums[i])
12+
if res.last! < nums[i] {
13+
res.append(nums[i])
3214
} else {
33-
ends[right] = nums[i]
15+
res[binarySearch(res, nums[i])] = nums[i]
3416
}
3517
}
3618

37-
return ends.count
19+
return res.count
20+
}
21+
22+
private func binarySearch(_ num: Int, _ res: [Int]) -> Int {
23+
var l = 0, r = res.count - 1
24+
25+
while l < r {
26+
let mid = (r - l) / 2 + l
27+
28+
if res[mid] == num {
29+
return mid
30+
} else if res[mid] > num {
31+
r = mid
32+
} else {
33+
l = mid + 1
34+
}
35+
}
36+
37+
return l
3838
}
39-
}
39+
}

DP/MinimumPathSum.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,23 @@
66

77
class MinimumPathSum {
88
func minPathSum(_ grid: [[Int]]) -> Int {
9-
guard grid.count != 0 && grid[0].count != 0 else{
10-
return 0
11-
}
12-
139
let m = grid.count, n = grid[0].count
14-
var dp = Array(repeating: Array(repeating: 0, count: n), count: m)
15-
10+
var dp = grid
11+
1612
for i in 0..<m {
1713
for j in 0..<n {
18-
if i == 0 && j == 0{
14+
if i == 0 && j == 0 {
1915
dp[i][j] = grid[i][j]
2016
} else if i == 0 {
21-
dp[i][j] = dp[i][j - 1] + grid[i][j]
17+
dp[i][j] += dp[i][j - 1]
2218
} else if j == 0 {
23-
dp[i][j] = dp[i - 1][j] + grid[i][j]
19+
dp[i][j] += dp[i - 1][j]
2420
} else {
25-
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]
21+
dp[i][j] += min(dp[i - 1][j], dp[i][j - 1])
2622
}
2723
}
2824
}
29-
25+
3026
return dp[m - 1][n - 1]
3127
}
32-
}
28+
}

String/GroupAnagrams.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@
88

99
class GroupAnagrams {
1010
func groupAnagrams(_ strs: [String]) -> [[String]] {
11-
var sortedStrToStrs = [String: [String]]()
12-
13-
for str in strs {
14-
let sortedStr = String(str.sorted())
15-
16-
sortedStrToStrs[sortedStr, default: []].append(str)
17-
}
18-
19-
return Array(sortedStrToStrs.values)
11+
return Array(Dictionary(strs.map { (String($0.sorted()), [$0]) }, uniquingKeysWith: +).values)
2012
}
2113
}

0 commit comments

Comments
 (0)