aboutsummaryrefslogtreecommitdiff
path: root/other/max_sum_3_noverlap_subarr.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/max_sum_3_noverlap_subarr.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/max_sum_3_noverlap_subarr.cpp')
-rw-r--r--other/max_sum_3_noverlap_subarr.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/other/max_sum_3_noverlap_subarr.cpp b/other/max_sum_3_noverlap_subarr.cpp
new file mode 100644
index 0000000..c326ad3
--- /dev/null
+++ b/other/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");
+}