Skip to content

Commit 3255ed3

Browse files
committed
[DP] Add solutions to Minimum Window Subsequence and Maximum Sum of 3 Non-Overlapping Subarrays
1 parent e96f752 commit 3255ed3

3 files changed

+113
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/
3+
* Primary idea: Dynamic Programming, find point where sum of [0, i - 1], [i, i + k - 1], [i + k, count - 1] is largest, where k <= i <= count - 2k
4+
* Time Complexity: O(n), Space Complexity: O(n)
5+
*
6+
*/
7+
8+
class MaximumSumThreeNonOverlappingSubarrays {
9+
func maxSumOfThreeSubarrays(_ nums: [Int], _ k: Int) -> [Int] {
10+
let sums = createSums(nums), n = nums.count
11+
let leftIndices = createLeftIndices(sums, n, k), rightIndices = createRightIndices(sums, n, k)
12+
var total = 0, res = [-1, -1, -1]
13+
14+
for i in k...n - 2 * k {
15+
let l = leftIndices[i - 1], r = rightIndices[i + k]
16+
let currentTotal = (sums[i+k] - sums[i]) + (sums[l + k] - sums[l]) + (sums[r + k] - sums[r])
17+
18+
if currentTotal > total {
19+
total = currentTotal
20+
res = [l, i, r]
21+
}
22+
}
23+
24+
return res
25+
}
26+
27+
fileprivate func createSums(_ nums: [Int]) -> [Int] {
28+
var sums = [0], currentSum = 0
29+
30+
for num in nums {
31+
currentSum += num
32+
33+
sums.append(currentSum)
34+
}
35+
36+
return sums
37+
}
38+
39+
// DP for starting index of left
40+
fileprivate func createLeftIndices(_ sums: [Int], _ n: Int, _ k: Int) -> [Int] {
41+
var leftIndices = Array(repeating: 0, count: n), maxSum = sums[k] - sums[0]
42+
43+
for i in k..<n {
44+
if sums[i + 1] - sums[i + 1 - k] > maxSum {
45+
leftIndices[i] = i + 1 - k
46+
maxSum = sums[i + 1] - sums[i + 1 - k]
47+
} else {
48+
leftIndices[i] = leftIndices[i - 1]
49+
}
50+
}
51+
52+
return leftIndices
53+
}
54+
55+
// DP for starting index of right
56+
fileprivate func createRightIndices(_ sums: [Int], _ n: Int, _ k: Int) -> [Int] {
57+
var rightIndices = Array(repeating: 0, count: n), maxSum = sums[n] - sums[n - k]
58+
rightIndices[n - k] = n - k
59+
60+
for i in (0...n - k - 1).reversed() {
61+
if sums[i + k] - sums[i] >= maxSum {
62+
rightIndices[i] = i
63+
maxSum = sums[i + k] - sums[i]
64+
} else {
65+
rightIndices[i] = rightIndices[i + 1]
66+
}
67+
}
68+
69+
return rightIndices
70+
}
71+
}

DP/MinimumWindowSubsequence.swift

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/minimum-window-subsequence/
3+
* Primary idea: Dynamic Programming, dp[i][j] = dp[i][j - 1] || dp[i - 1][j - 1]
4+
* Time Complexity: O(mn), Space Complexity: O(mn)
5+
*
6+
*/
7+
8+
class MinimumWindowSubsequence {
9+
func minWindow(_ S: String, _ T: String) -> String {
10+
let m = T.count, n = S.count, sChars = Array(S), tChars = Array(T)
11+
var dp = Array(repeating: Array(repeating: 0, count: n + 1), count: m + 1)
12+
var start = 0, len = n + 1
13+
14+
for i in 0...n {
15+
dp[0][i] = i + 1
16+
}
17+
18+
for i in 1...m {
19+
for j in 1...n {
20+
if tChars[i - 1] == sChars[j - 1] {
21+
dp[i][j] = dp[i - 1][j - 1]
22+
} else {
23+
dp[i][j] = dp[i][j - 1]
24+
}
25+
}
26+
}
27+
28+
for i in 1...n {
29+
if dp[m][i] != 0 {
30+
if i - dp[m][i] + 1 < len {
31+
len = i - dp[m][i] + 1
32+
start = dp[m][i] - 1
33+
}
34+
}
35+
}
36+
37+
return len == n + 1 ? "" : String(sChars[start..<start + len])
38+
}
39+
}

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Microsoft](#microsoft)
2929

3030
## Progress
31-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 238 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
31+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 240 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3232

3333

3434
## Array
@@ -183,6 +183,7 @@
183183
[Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| [Swift](./DP/BestTimeBuySellStockIII.swift)| Hard| O(n)| O(n)|
184184
[Best Time to Buy and Sell Stock IV](https://leetcode.com/problems/https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)| [Swift](./DP/BestTimeBuySellStockIV.swift)| Hard| O(n^2)| O(n)|
185185
[Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)| [Swift](./DP/BestTimeBuySellStockCooldown.swift)| Medium| O(n^2)| O(n)|
186+
[Maximum Sum of 3 Non-Overlapping Subarrays](https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/)| [Swift](./DP/MaximumSumThreeNonOverlappingSubarrays.swift| O(n)| O(n)|
186187
[Coin Change](https://leetcode.com/problems/coin-change/)| [Swift](./DP/CoinChange.swift)| Medium| O(n^2)| O(n)|
187188
[Coin Change II](https://leetcode.com/problems/coin-change-ii/)| [Swift](./DP/CoinChangeII.swift)| Medium| O(n^2)| O(n)|
188189
[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/)| [Swift](./DP/LongestIncreasingSubsequence.swift)| Medium| O(n^2)| O(n)|
@@ -199,6 +200,7 @@
199200
[Triangle](https://leetcode.com/problems/triangle/)| [Swift](./DP/Triangle.swift)| Medium| O(2^n - 1)| O(m)|
200201
[Wildcard Matching](https://leetcode.com/problems/wildcard-matching/)| [Swift](./DP/WildcardMatching.swift)| Hard| O(mn)| O(mn)|
201202
[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)| [Swift](./DP/RegularExpressionMatching.swift)| Hard| O(mn)| O(mn)|
203+
[Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence/)| [Swift](./DP/MinimumWindowSubsequence.swift)| Hard| O(mn)| O(mn)|
202204
[Guess Number Higher or Lower II](https://leetcode.com/problems/guess-number-higher-or-lower-ii/)| [Swift](./DP/GuessNumberHigherOrLowerII.swift)| Medium| O(nlogn)| O(n^2)|
203205
[Burst Ballons](https://leetcode.com/problems/burst-balloons/)| [Swift](./DP/BurstBalloons.swift)| Hard| O(n^3)| O(n)|
204206
[Frog Jump](https://leetcode.com/problems/frog-jump/)| [Swift](./DP/FrogJump.swift)| Hard| O(n^2)| O(n)|

0 commit comments

Comments
 (0)