forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DP] Add solutions to Minimum Window Subsequence and Maximum Sum of 3…
… Non-Overlapping Subarrays
- Loading branch information
Showing
3 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters