|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * 689. Maximum Sum of 3 Non-Overlapping Subarrays |
5 |
| - * |
6 |
| - * In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. |
7 |
| - * Each subarray will be of size k, and we want to maximize the sum of all 3*k entries. |
8 |
| - * Return the result as a list of indices representing the starting position of each interval (0-indexed). |
9 |
| - * If there are multiple answers, return the lexicographically smallest one. |
10 |
| -
|
11 |
| - Example: |
12 |
| - Input: [1,2,1,2,6,7,5,1], 2 |
13 |
| - Output: [0, 3, 5] |
14 |
| - Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5]. |
15 |
| - We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger. |
16 |
| -
|
17 |
| - Note: |
18 |
| - nums.length will be between 1 and 20000. |
19 |
| - nums[i] will be between 1 and 65535. |
20 |
| - k will be between 1 and floor(nums.length / 3). |
21 |
| - */ |
22 | 3 | public class _689 {
|
23 | 4 | public static class Solution1 {
|
24 |
| - /**we basically need to find the interval (i, i+k-1) as the middle interval, where k <= i <= n-2k |
| 5 | + /** |
| 6 | + * we basically need to find the interval (i, i+k-1) as the middle interval, where k <= i <= n-2k |
25 | 7 | * then this interval (0, i-1) will be the left interval
|
26 | 8 | * the interval (i+k, n-1) will be the right interval.
|
27 |
| - * |
| 9 | + * <p> |
28 | 10 | * Please pay special attention to the variable name I use here: this `k` is not a random one, it's the `k`
|
29 | 11 | * from the passed in parameter.
|
30 |
| - * |
31 |
| - * Credit: https://discuss.leetcode.com/topic/105577/c-java-dp-with-explanation-o-n/*/ |
| 12 | + * <p> |
| 13 | + * Credit: https://discuss.leetcode.com/topic/105577/c-java-dp-with-explanation-o-n/ |
| 14 | + */ |
32 | 15 | public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
|
33 | 16 | if (nums == null || nums.length == 0) {
|
34 | 17 | return new int[]{};
|
|
0 commit comments