[Learn dynamic programming] The maximum sum of circular sub-arrays (20)

Table of contents

How to learn dynamic programming?

1. Topic analysis

2. Algorithm principle

1. Status representation

2. State transition equation

3. Initialization

4. Filling order

5. Return value

3. Code writing

Write at the end:


How to learn dynamic programming?

There is no shortcut to learning an algorithm, let alone learning dynamic programming,

Brush dynamic programming algorithm questions with me, and learn dynamic programming together!

1. Topic analysis

Topic Link: 918. The Maximum Sum of Circular Subarrays - LeetCode

This question blah blah blah blah blah blah blah blah said a lot of things that seem very mysterious, we don't care about him,

The core of grasping the topic is to find the sub-array, so what are the rules for finding the sub-array?

Let's interpret it directly by looking at the example:

Through example 2, we can see what is called a circular array, its head and tail are connected,

So 5 and 5 can form a subarray.

So how should we do this question?

We can break it down into two sub-problems:

1. It is to find its largest sub-array normally:

2. Because of the circular array, we can transform the case of end-to-end connection into the case of finding the smallest sub-array sum:

2. Algorithm principle

1. Status representation

According to the two situations we analyzed above, it can actually be expressed in two states of atmosphere:

f [ i ] means the maximum sum of all subarrays ending with i

g [ i ] means the minimum sum of all subarrays ending with i 

2. State transition equation

Then each state indicates that there are two situations, one situation is oneself, and the other situation is oneself plus the maximum/minimum sum of the previous position

So their state transition equation is:

f [ i ] = max( nums[ i ],nums[ i ] + f [ i - 1 ] )

g [ i ] = min( nums[ i ],nums[ i ] + g[ i - 1] )

3. Initialization

In order to prevent cross-border during initialization, add one more grid and initialize to 0

4. Filling order

from left to right

5. Return value

max(Maximum value of f array, sum - minimum value of g array)

3. Code writing

class Solution {
public:
    int maxSubarraySumCircular(vector<int>& nums) {
        int n = nums.size();
        vector<int> f(n + 1);
        auto g = f;
        for(int i = 1; i <= n; i++) {
            f[i] = max(nums[i - 1], nums[i - 1] + f[i - 1]);
            g[i] = min(nums[i - 1], nums[i - 1] + g[i - 1]);
        }
        int fmax = INT_MIN;
        int gmin = INT_MAX;
        int sum = 0;
        for(int i = 1; i <= n; i++) fmax = max(fmax, f[i]);
        for(int i = 1; i <= n; i++) gmin = min(gmin, g[i]);
        for(auto s : nums) sum += s;
        return fmax < 0 ? fmax : max(fmax, sum - gmin);
    }
};

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/131845345