Skip to content

Commit

Permalink
[DP] Add solutions to Minimum Window Subsequence and Maximum Sum of 3…
Browse files Browse the repository at this point in the history
… Non-Overlapping Subarrays
  • Loading branch information
soapyigu committed Apr 22, 2018
1 parent e96f752 commit 3255ed3
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
71 changes: 71 additions & 0 deletions DP/MaximumSumThreeNonOverlappingSubarrays.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Question Link: https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/
* 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
* Time Complexity: O(n), Space Complexity: O(n)
*
*/

class MaximumSumThreeNonOverlappingSubarrays {
func maxSumOfThreeSubarrays(_ nums: [Int], _ k: Int) -> [Int] {
let sums = createSums(nums), n = nums.count
let leftIndices = createLeftIndices(sums, n, k), rightIndices = createRightIndices(sums, n, k)
var total = 0, res = [-1, -1, -1]

for i in k...n - 2 * k {
let l = leftIndices[i - 1], r = rightIndices[i + k]
let currentTotal = (sums[i+k] - sums[i]) + (sums[l + k] - sums[l]) + (sums[r + k] - sums[r])

if currentTotal > total {
total = currentTotal
res = [l, i, r]
}
}

return res
}

fileprivate func createSums(_ nums: [Int]) -> [Int] {
var sums = [0], currentSum = 0

for num in nums {
currentSum += num

sums.append(currentSum)
}

return sums
}

// DP for starting index of left
fileprivate func createLeftIndices(_ sums: [Int], _ n: Int, _ k: Int) -> [Int] {
var leftIndices = Array(repeating: 0, count: n), maxSum = sums[k] - sums[0]

for i in k..<n {
if sums[i + 1] - sums[i + 1 - k] > maxSum {
leftIndices[i] = i + 1 - k
maxSum = sums[i + 1] - sums[i + 1 - k]
} else {
leftIndices[i] = leftIndices[i - 1]
}
}

return leftIndices
}

// DP for starting index of right
fileprivate func createRightIndices(_ sums: [Int], _ n: Int, _ k: Int) -> [Int] {
var rightIndices = Array(repeating: 0, count: n), maxSum = sums[n] - sums[n - k]
rightIndices[n - k] = n - k

for i in (0...n - k - 1).reversed() {
if sums[i + k] - sums[i] >= maxSum {
rightIndices[i] = i
maxSum = sums[i + k] - sums[i]
} else {
rightIndices[i] = rightIndices[i + 1]
}
}

return rightIndices
}
}
39 changes: 39 additions & 0 deletions DP/MinimumWindowSubsequence.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Question Link: https://leetcode.com/problems/minimum-window-subsequence/
* Primary idea: Dynamic Programming, dp[i][j] = dp[i][j - 1] || dp[i - 1][j - 1]
* Time Complexity: O(mn), Space Complexity: O(mn)
*
*/

class MinimumWindowSubsequence {
func minWindow(_ S: String, _ T: String) -> String {
let m = T.count, n = S.count, sChars = Array(S), tChars = Array(T)
var dp = Array(repeating: Array(repeating: 0, count: n + 1), count: m + 1)
var start = 0, len = n + 1

for i in 0...n {
dp[0][i] = i + 1
}

for i in 1...m {
for j in 1...n {
if tChars[i - 1] == sChars[j - 1] {
dp[i][j] = dp[i - 1][j - 1]
} else {
dp[i][j] = dp[i][j - 1]
}
}
}

for i in 1...n {
if dp[m][i] != 0 {
if i - dp[m][i] + 1 < len {
len = i - dp[m][i] + 1
start = dp[m][i] - 1
}
}
}

return len == n + 1 ? "" : String(sChars[start..<start + len])
}
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* [Microsoft](#microsoft)

## Progress
[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).
[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).


## Array
Expand Down Expand Up @@ -183,6 +183,7 @@
[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)|
[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)|
[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)|
[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)|
[Coin Change](https://leetcode.com/problems/coin-change/)| [Swift](./DP/CoinChange.swift)| Medium| O(n^2)| O(n)|
[Coin Change II](https://leetcode.com/problems/coin-change-ii/)| [Swift](./DP/CoinChangeII.swift)| Medium| O(n^2)| O(n)|
[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/)| [Swift](./DP/LongestIncreasingSubsequence.swift)| Medium| O(n^2)| O(n)|
Expand All @@ -199,6 +200,7 @@
[Triangle](https://leetcode.com/problems/triangle/)| [Swift](./DP/Triangle.swift)| Medium| O(2^n - 1)| O(m)|
[Wildcard Matching](https://leetcode.com/problems/wildcard-matching/)| [Swift](./DP/WildcardMatching.swift)| Hard| O(mn)| O(mn)|
[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)| [Swift](./DP/RegularExpressionMatching.swift)| Hard| O(mn)| O(mn)|
[Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence/)| [Swift](./DP/MinimumWindowSubsequence.swift)| Hard| O(mn)| O(mn)|
[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)|
[Burst Ballons](https://leetcode.com/problems/burst-balloons/)| [Swift](./DP/BurstBalloons.swift)| Hard| O(n^3)| O(n)|
[Frog Jump](https://leetcode.com/problems/frog-jump/)| [Swift](./DP/FrogJump.swift)| Hard| O(n^2)| O(n)|
Expand Down

0 comments on commit 3255ed3

Please sign in to comment.