diff options
author | Jack Sangdahl <[email protected]> | 2024-12-27 19:04:58 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-12-27 19:04:58 -0700 |
commit | be2dd2d507ad02c2ebfb3e371cfda1d4612de271 (patch) | |
tree | 6ec2021ad3c81294a4e99416cbf0eafc5f3140ea | |
parent | a52ccc2d81430bae1862eebf40055815e1ff1c73 (diff) | |
download | leetcode-be2dd2d507ad02c2ebfb3e371cfda1d4612de271.tar.gz leetcode-be2dd2d507ad02c2ebfb3e371cfda1d4612de271.zip |
689. maximum sum of 3 non-overlapping subarrays
-rw-r--r-- | max_sum_3_noverlap_subarr.cpp | 57 | ||||
-rw-r--r-- | max_sum_3_noverlap_subarr.py | 53 |
2 files changed, 110 insertions, 0 deletions
diff --git a/max_sum_3_noverlap_subarr.cpp b/max_sum_3_noverlap_subarr.cpp new file mode 100644 index 0000000..c326ad3 --- /dev/null +++ b/max_sum_3_noverlap_subarr.cpp @@ -0,0 +1,57 @@ +// 689. Maximum Sum of 3 Non-Overlapping Subarrays +#include "leetcode.h" + +/* + * given an integer array 'nums' and an integer 'k', find three non-overlapping + * subarrays of length 'k' with maximum sum and return them. return the result + * as a list of indices representing the starting position of each interval + * (0-indexed). if there are multiple answers, return the lexicographcially + * smallest one + */ + +class Solution { +public: + vector<int> maxSumOfThreeSubarrays(vector<int> &nums, int k) { + int n = nums.size(), max_sum = 0; + vector<int> sum = {0}, left(n, 0), right(n, n - k), ans(3, 0); + for (int i : nums) + sum.push_back(sum.back() + i); + for (int i = k, tot = sum[k] - sum[0]; i < n; i++) { + if (sum[i + 1] - sum[i + 1 - k] > tot) { + left[i] = i + 1 - k; + tot = sum[i + 1] - sum[i + 1 - k]; + } else + left[i] = left[i - 1]; + } + for (int i = n - k - 1, tot = sum[n] - sum[n - k]; i >= 0; i--) { + if (sum[i + k] - sum[i] >= tot) { + right[i] = i; + tot = sum[i + k] - sum[i]; + } else + right[i] = right[i + 1]; + } + for (int i = k; i <= n - 2 * k; i++) { + int l = left[i - 1], r = right[i + k]; + int tot = + (sum[i + k] - sum[i]) + (sum[l + k] - sum[l]) + (sum[r + k] - sum[r]); + if (tot > max_sum) { + max_sum = tot; + ans = {l, i, r}; + } + } + return ans; + } +}; + +int main() { + Solution obj; + vector<int> n1 = {1, 2, 1, 2, 6, 7, 5, 1}, n2 = {1, 2, 1, 2, 1, 2, 1, 2, 1}; + vector<int> msots1 = obj.maxSumOfThreeSubarrays(n1, 2); + vector<int> msots2 = obj.maxSumOfThreeSubarrays(n2, 2); + for (auto i : msots1) + printf("%d ", i); // expect: 0 3 5 + printf("\n"); + for (auto i : msots2) + printf("%d ", i); // expect: 0 2 4 + printf("\n"); +} diff --git a/max_sum_3_noverlap_subarr.py b/max_sum_3_noverlap_subarr.py new file mode 100644 index 0000000..8240c46 --- /dev/null +++ b/max_sum_3_noverlap_subarr.py @@ -0,0 +1,53 @@ +# 689. Maximum Sum of 3 Non-Overlapping Subarrays + +""" +given an integer array 'nums' and an integer 'k', find three non-overlapping +subarrays of length 'k' with maximum sum and return them. return the result +as a list of indices representing the starting position of each interval +(0-indexed). if there are multiple answers, return the lexicographcially +smallest one +""" + + +class Solution(object): + def max_sumOfThreeSubarrays(self, nums, k): + """ + :type nums: List[int] + :type k: int + :rtype: List[int] + """ + n = len(nums) + max_sum = 0 + sum = [0] + left = [0] * n + right = [n - k] * n + ans = [0, 0, 0] + for i in nums: + sum.append(sum[-1] + i) + tot = sum[k] - sum[0] + for i in range(k, n): + if sum[i + 1] - sum[i + 1 - k] > tot: + left[i] = i + 1 - k + tot = sum[i + 1] - sum[i + 1 - k] + else: + left[i] = left[i - 1] + tot = sum[n] - sum[n - k] + for i in range(n - k - 1, -1, -1): + if sum[i + k] - sum[i] > tot: + right[i] = i + tot = sum[i + k] - sum[i] + else: + right[i] = right[i + 1] + for i in range(k, n - 2 * k + 1): + l, r = left[i - 1], right[i + k] + tot = (sum[i + k] - sum[i]) + (sum[l + k] - sum[l]) + (sum[r + k] - sum[r]) + if tot > max_sum: + max_sum = tot + ans = [l, i, r] + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.max_sumOfThreeSubarrays(nums=[1, 2, 1, 2, 6, 7, 5, 1], k=2)) + print(obj.max_sumOfThreeSubarrays(nums=[1, 2, 1, 2, 1, 2, 1, 2, 1], k=2)) |