-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 689. Maximum Sum of 3 Non-Overlapping Subarrays (#674)
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
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,62 @@ | ||
class Solution { | ||
public: | ||
void subarraySum(vector<int>& nums, vector<int>& subarr, int k) { | ||
int n = nums.size(); | ||
int l = 0; | ||
int r = 0; | ||
int sum = 0; | ||
|
||
while (r < n) { | ||
while (r - l + 1 < k) { | ||
sum += nums[r]; | ||
r++; | ||
} | ||
sum += nums[r]; | ||
subarr.push_back(sum); | ||
sum -= nums[l]; | ||
l++; | ||
r++; | ||
} | ||
}; | ||
|
||
int helper(int ind, vector<int>& subarr, int k, int count, | ||
vector<vector<int>>& dp) { | ||
int n = subarr.size(); | ||
if (count == 0) | ||
return 0; | ||
if (ind >= n) | ||
return INT_MIN; | ||
if (dp[ind][count] != -1) | ||
return dp[ind][count]; | ||
int take = subarr[ind] + helper(ind + k, subarr, k, count - 1, dp); | ||
int nottake = helper(ind + 1, subarr, k, count, dp); | ||
return dp[ind][count] = max(take, nottake); | ||
} | ||
void solve(int ind, int count, vector<int>& ans, vector<int>& subarr, int k, | ||
vector<vector<int>>& dp) { | ||
int n = subarr.size(); | ||
if (count == 0) | ||
return; | ||
if (ind >= n) | ||
return; | ||
|
||
int take = subarr[ind] + helper(ind + k, subarr, k, count - 1, dp); | ||
int nottake = helper(ind + 1, subarr, k, count, dp); | ||
|
||
if (take >= nottake) { | ||
ans.push_back(ind); | ||
solve(ind + k, count - 1, ans, subarr, k, dp); | ||
} else | ||
solve(ind + 1, count, ans, subarr, k, dp); | ||
} | ||
|
||
vector<int> maxSumOfThreeSubarrays(vector<int>& nums, int k) { | ||
int n = nums.size(); | ||
vector<int> subarr; | ||
subarraySum(nums, subarr, k); | ||
vector<vector<int>> dp(n + 1, vector<int>(4, -1)); | ||
vector<int> ans; | ||
solve(0, 3, ans, subarr, k, dp); | ||
return ans; | ||
} | ||
}; |