Skip to content

Commit a21891c

Browse files
authored
Merge pull request soapyigu#347 from soapyigu/DP
Dp
2 parents 77afc6c + 607819e commit a21891c

File tree

3 files changed

+79
-28
lines changed

3 files changed

+79
-28
lines changed

DP/MaximumNumberPointsCost.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/maximum-number-of-points-with-cost/
3+
* Primary idea: DP. dp[i][j] is the maximum number of points we can have if points[i][j] is the most recent cell we picked.
4+
* Optimization: Keeps track the current row is enough. Update current row max by traversing from left and right.
5+
* Time Complexity: O(mn), Space Complexity: O(1)
6+
*
7+
*/
8+
9+
class MaximumNumberPointsCost {
10+
func maxPoints(_ points: [[Int]]) -> Int {
11+
12+
let m = points.count, n = points[0].count
13+
var rowMaxes = points[0]
14+
15+
for i in 1..<m {
16+
17+
var temp = rowMaxes
18+
var leftMax = 0, rightMax = 0
19+
20+
for j in 0..<n {
21+
leftMax = max(leftMax - 1, temp[j])
22+
temp[j] = points[i][j] + leftMax
23+
}
24+
25+
for j in (0..<n).reversed() {
26+
rightMax = max(rightMax - 1, rowMaxes[j])
27+
rowMaxes[j] = max(points[i][j] + rightMax, temp[j])
28+
}
29+
}
30+
31+
return rowMaxes.max() ?? 0
32+
}
33+
}

DP/UniquePathsII.swift

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,29 @@
55
*/
66

77
class UniquePathsII {
8-
func uniquePathsWithObstacles(_ obstacleGrid: [[Int]]) -> Int {
9-
let m = obstacleGrid.count
10-
guard m > 0 else {
11-
return 0
12-
}
8+
func uniquePathsWithObstacles(_ obstacleGrid: [[Int]]) -> Int {
9+
let m = obstacleGrid.count, n = obstacleGrid[0].count
1310

14-
let n = obstacleGrid[0].count
15-
guard n > 0 else {
16-
return 0
17-
}
18-
19-
var dp = Array(repeating: Array(repeating: -1, count: n), count: m)
11+
var dp = Array(repeating: Array(repeating: 0, count: n), count: m)
2012

21-
return help(m - 1, n - 1, &dp, obstacleGrid)
22-
}
23-
24-
fileprivate func help(_ m: Int, _ n: Int, _ dp: inout [[Int]], _ obstacleGrid: [[Int]]) -> Int {
25-
if m < 0 || n < 0 {
26-
return 0
27-
}
28-
if obstacleGrid[m][n] == 1 {
29-
return 0
30-
}
31-
if m == 0 && n == 0 {
32-
return 1
33-
}
34-
if dp[m][n] != -1 {
35-
return dp[m][n]
13+
for i in 0..<m {
14+
for j in 0..<n {
15+
if obstacleGrid[i][j] == 1 {
16+
dp[i][j] = 0
17+
} else {
18+
if i == 0 && j == 0 {
19+
dp[i][j] = 1
20+
} else if i == 0 {
21+
dp[i][j] = dp[i][j - 1]
22+
} else if j == 0 {
23+
dp[i][j] = dp[i - 1][j]
24+
} else {
25+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
26+
}
27+
}
28+
}
3629
}
3730

38-
dp[m][n] = help(m - 1, n, &dp, obstacleGrid) + help(m, n - 1, &dp, obstacleGrid)
39-
return dp[m][n]
31+
return dp[m - 1][n - 1]
4032
}
4133
}

DP/WordBreak.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/word-break/
3+
* Primary idea: DP. dp[i] = dp[j] && dict.contains(s[j..<i]). Break the operation if current index is already true.
4+
* Time Complexity: O(n^3), Space Complexity: O(n)
5+
*/
6+
7+
class WordBreak {
8+
func wordBreak(_ s: String, _ wordDict: [String]) -> Bool {
9+
let dict = Set(wordDict), s = Array(s)
10+
var dp = Array(repeating: false, count: s.count + 1)
11+
dp[0] = true
12+
13+
for i in 1...s.count {
14+
for j in 0..<i {
15+
let currentStr = String(s[j..<i])
16+
17+
if dp[j] && wordDict.contains(currentStr) {
18+
dp[i] = true
19+
break
20+
}
21+
}
22+
}
23+
24+
return dp[s.count]
25+
}
26+
}

0 commit comments

Comments
 (0)