Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f8405e7

Browse files
committedMar 10, 2025
Add solution #689
1 parent a8379d1 commit f8405e7

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@
520520
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
521521
687|[Longest Univalue Path](./0687-longest-univalue-path.js)|Medium|
522522
688|[Knight Probability in Chessboard](./0688-knight-probability-in-chessboard.js)|Medium|
523+
689|[Maximum Sum of 3 Non-Overlapping Subarrays](./0689-maximum-sum-of-3-non-overlapping-subarrays.js)|Hard|
523524
693|[Binary Number with Alternating Bits](./0693-binary-number-with-alternating-bits.js)|Easy|
524525
695|[Max Area of Island](./0695-max-area-of-island.js)|Medium|
525526
696|[Count Binary Substrings](./0696-count-binary-substrings.js)|Easy|
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* 689. Maximum Sum of 3 Non-Overlapping Subarrays
3+
* https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/
4+
* Difficulty: Hard
5+
*
6+
* Given an integer array nums and an integer k, find three non-overlapping subarrays of
7+
* length k with maximum sum and return them.
8+
*
9+
* Return the result as a list of indices representing the starting position of each
10+
* interval (0-indexed). If there are multiple answers, return the lexicographically
11+
* smallest one.
12+
*/
13+
14+
/**
15+
* @param {number[]} nums
16+
* @param {number} k
17+
* @return {number[]}
18+
*/
19+
var maxSumOfThreeSubarrays = function(nums, k) {
20+
const n = nums.length;
21+
let [sumFirst, sumSecond, sumThird] = [0, 0, 0];
22+
let [maxFirst, maxFirstSecond, maxTotal] = [0, 0, 0];
23+
let [startFirst, startFirstBest, startSecondBest] = [0, 0, k];
24+
let result = [0, k, 2 * k];
25+
26+
for (let i = 0; i < k; i++) {
27+
sumFirst += nums[i];
28+
sumSecond += nums[i + k];
29+
sumThird += nums[i + 2 * k];
30+
}
31+
[maxFirst, maxFirstSecond, maxTotal] = [
32+
sumFirst, sumFirst + sumSecond,
33+
sumFirst + sumSecond + sumThird
34+
];
35+
36+
for (let i = 1; i <= n - 3 * k; i++) {
37+
sumFirst += nums[i + k - 1] - nums[i - 1];
38+
sumSecond += nums[i + 2 * k - 1] - nums[i + k - 1];
39+
sumThird += nums[i + 3 * k - 1] - nums[i + 2 * k - 1];
40+
41+
if (sumFirst > maxFirst) [maxFirst, startFirst] = [sumFirst, i];
42+
if (maxFirst + sumSecond > maxFirstSecond) {
43+
[maxFirstSecond, startFirstBest, startSecondBest] = [maxFirst + sumSecond, startFirst, i + k];
44+
}
45+
if (maxFirstSecond + sumThird > maxTotal) {
46+
[maxTotal, ...result] = [
47+
maxFirstSecond + sumThird,
48+
startFirstBest,
49+
startSecondBest, i + 2 * k
50+
];
51+
}
52+
}
53+
54+
return result;
55+
};

0 commit comments

Comments
 (0)
Please sign in to comment.