[Copy question]:
In a given array nums
of positive integers, find three non-overlapping subarrays with maximum sum.
Each subarray would be k
is of size, and we want to maximize the sum of all 3*k
entries.
Return The result as a list of indices representing the starting position of each interval (0-indexed). If There is multiple answers, return the lexicographically smallest one.
Example:
Input: [1,2,1,2,6,7,5,1], 2Output: [0, 3, 5]explanation:subarrays [1, 2], [2, 6], [7, 5] correspond to the starting Indic Es [0, 3, 5]. We could has also taken [2, 1], but a answer of [1, 3, 5] would be lexicographically larger.
[Brute force solution]:
Time Analysis:
Spatial Analysis:
[After optimization]:
Time Analysis:
Spatial Analysis:
[Wonderful output CONDITIONS]:
[Wonderful corner case]:
The "First I" is initialized to "item I", it is convenient to do the difference directly
for (int i = 1; I <= n; i++) { = sums[i-1] + nums[i-1]; }
[Thinking questions]:
Do not know why to use DP: each time to save the previous set of state, and then a forward update and parity.
Available when a set of arrays that are fixed to K-length is obtained.
// sum = Current Group and + previous Group and = Last sum of this group-DP summation before first and + of the group (starting with J-k) int cursum = sums[j]-sums[j-k] + dp[i-1][j-k];
[English data structure or algorithm, why not other data structures or algorithms]:
The results are stored in the DP array and can be extracted by continuously entering index:
int index = n; for (int i = 2; I >= 0; i--) { = pos[i + 1][index]; SYSTEM.OUT.PRINTLN ("index =" +index); System.out.println ("res[i] = pos[i + 1][index] =" +res[i]); = Res[i]; SYSTEM.OUT.PRINTLN ("index =" +index); System.out.println ("----------------"); }
[a sentence of thought]:
[input]: null: Normal: Large: Extra Small: Special cases handled in the program: abnormal conditions (unreasonable input):
[Drawing]:
[One brush]:
[Two brushes]:
[Three brushes]:
[Four brushes]:
[Five brushes]:
[Results of five-minute visual debug]:
[Summary]:
[Complexity]:time Complexity:o () Space complexity:o ()
[Algorithmic thinking: Iterative/recursive/split/greedy]:
[Key templating code]:
[Other solutions]:
[Follow up]:
[The problem given by the LC becomes variable]:
[Code Style]:
[Whether to write this type of driver Funcion code for the first time]:
689. Maximum sum of 3 non-overlapping subarrays three non-coincident arrays maximum value