689. Maximum sum of 3 non-overlapping subarrays three non-coincident arrays maximum value

Source: Internet
Author: User

[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.