Skip to content

Commit 95b0ccc

Browse files
committed
[LeetCode Sync] Runtime - 26 ms (67.12%), Memory - 20.5 MB (50.41%)
1 parent 11211e8 commit 95b0ccc

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, find three non-overlapping subarrays of length <code>k</code> with maximum sum and return them.</p>
2+
3+
<p>Return the result as a list of indices representing the starting position of each interval (<strong>0-indexed</strong>). If there are multiple answers, return the lexicographically smallest one.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> nums = [1,2,1,2,6,7,5,1], k = 2
10+
<strong>Output:</strong> [0,3,5]
11+
<strong>Explanation:</strong> Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
12+
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.
13+
</pre>
14+
15+
<p><strong class="example">Example 2:</strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> nums = [1,2,1,2,1,2,1,2,1], k = 2
19+
<strong>Output:</strong> [0,2,4]
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Constraints:</strong></p>
24+
25+
<ul>
26+
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
27+
<li><code>1 &lt;= nums[i] &lt;&nbsp;2<sup>16</sup></code></li>
28+
<li><code>1 &lt;= k &lt;= floor(nums.length / 3)</code></li>
29+
</ul>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Solution:
2+
def maxSumOfThreeSubarrays(self, nums: List[int], k: int) -> List[int]:
3+
n = len(nums)
4+
max_sum = 0
5+
6+
# Create prefix sum array using accumulate
7+
prefix_sum = [0] + list(accumulate(nums))
8+
9+
# Initialize arrays for best indices
10+
left_max_idx = [0] * n
11+
right_max_idx = [0] * n
12+
result = [0] * 3
13+
14+
# Calculate best left subarray positions
15+
curr_max_sum = prefix_sum[k] - prefix_sum[0]
16+
for i in range(k, n):
17+
curr_sum = prefix_sum[i + 1] - prefix_sum[i + 1 - k]
18+
if curr_sum > curr_max_sum:
19+
left_max_idx[i] = i + 1 - k
20+
curr_max_sum = curr_sum
21+
else:
22+
left_max_idx[i] = left_max_idx[i - 1]
23+
24+
# Calculate best right subarray positions
25+
right_max_idx[n - k] = n - k
26+
curr_max_sum = prefix_sum[n] - prefix_sum[n - k]
27+
for i in range(n - k - 1, -1, -1):
28+
curr_sum = prefix_sum[i + k] - prefix_sum[i]
29+
if curr_sum >= curr_max_sum:
30+
right_max_idx[i] = i
31+
curr_max_sum = curr_sum
32+
else:
33+
right_max_idx[i] = right_max_idx[i + 1]
34+
35+
# Find optimal middle position
36+
for i in range(k, n - 2 * k + 1):
37+
left_idx = left_max_idx[i - 1]
38+
right_idx = right_max_idx[i + k]
39+
total_sum = (
40+
prefix_sum[i + k]
41+
- prefix_sum[i]
42+
+ prefix_sum[left_idx + k]
43+
- prefix_sum[left_idx]
44+
+ prefix_sum[right_idx + k]
45+
- prefix_sum[right_idx]
46+
)
47+
48+
if total_sum > max_sum:
49+
max_sum = total_sum
50+
result = [left_idx, i, right_idx]
51+
52+
return result

0 commit comments

Comments
 (0)