Lanqiao Cup-Dynamic Programming-Subarray Problem

Table of contents

1. Maximum product array

2. The length of the longest subarray whose product is a positive number

3. Division of Arithmetic Sequences

4. The longest turbulence subarray


Thoughts:

The most important thing is state representation. We need to analyze different questions and situations according to the meaning of the question, and how many states are needed.

1. Maximum product array

product max array

1. Status representation

dp[i]: The maximum product subarray reaching position i.

2. State transition equation

dp[i]=Math.max(dp[i-1]*p[i],dp[i-1]);

Problem: You cannot fill in the table with a simple maximum value, because there are situations where negatives can lead to positives, but there are actually two cases of multiplication.

Positive*positive is positive maximum value

Positive*negative is negative minimum value

Negative*negative is positive maximum value

The status representation changes to

f[i]: reaches position i, the maximum product

g[i]: reaching position i, the smallest product

Therefore, the state transition equation also needs to be changed.

 f[i]=Math.max(nums[i],Math.max(f[i-1]*nums[i],g[i-1]*nums[i]));
 g[i]=Math.min(nums[i],Math.min(f[i-1]*nums[i],g[i-1]*nums[i]));

3.Initialization

f[0]=g[0]=nums[0]

4. Order of filling in the form

from left to right

5.Return value

class Solution {
    public int maxProduct(int[] nums) {
   int m=nums.length;
   //f为最大乘积和
   //g为最小乘积和
   int[]f=new int[m];
   int[]g=new int[m];
   f[0]=g[0]=nums[0];
   for(int i=1;i<m;i++){
       //f[i]状态表示
   f[i]=Math.max(nums[i],Math.max(f[i-1]*nums[i],g[i-1]*nums[i]));
   g[i]=Math.min(nums[i],Math.min(f[i-1]*nums[i],g[i-1]*nums[i]));
   }
int ret =-0x3f3f3f3f;
   for(int i=0;i<m;i++){
    ret=Math.max(ret,f[i]);
   }
   return  ret;
    }
}

2. The length of the longest subarray whose product is a positive number

1. Status representation

dp[i]=The length of the longest subarray until the product at position i is positive

If we want to ensure that the product is a positive number, we need the state representation of the array with the largest product above.

f[i]: With i element as the ending position, the product is the length of a positive number

g[i]: With i position as the ending position, the product is the length of a negative number

2. State transition equation

f[i] is divided into the case where the length is 1 and the case where the length is not 1

When the length is one, it is also necessary to distinguish whether it is a positive number.

If the length is not one, it depends on whether the current i is a positive or negative number.

When nums[i]<0, we have to think about one thing. If g[i-1] is exactly equal to 0, but at this time nums[i]<0, then there is no positive number, and the final result is Is it equal to 0? So we cannot write that when nums[i]<0, f[i]=g[i-1]+1

3.Initialization:

Just look at whether nums[0] is greater than 0 or less than 0. Greater than 0, then f[0] is 1. less than 0 then g[0] is 1

4. Order of filling in the form:

from left to right

5Return value: Maximum return value

class Solution {
      public static int getMaxLen(int[] nums) {
            int m=nums.length;
            int f[]=new int[m];
            int g[]=new int[m];

            if(nums[0]>0){
                f[0]=1;
            }else if(nums[0]<0){
                g[0]=1;
            }
            for(int i=1;i<m;i++){
                //f[i]状态表示
                if(nums[i]>0){
                    f[i]=f[i-1]+1;
                    if(g[i-1]==0){
                        g[i]=0;
                    }else{
                        g[i]=g[i-1]+1;
                    }
                }
                else if(nums[i]<0){
                    if(g[i-1]==0){
                        f[i]=0 ;}else{
                        f[i]=g[i-1]+1;}
                    g[i]=f[i-1]+1;

                }
            }
            int ret=0;
            for(int i=0;i<m;i++){
                ret=Math.max(ret,f[i]);

            }
            return ret;
        }

}

3. Division of Arithmetic Sequences

1. Status representation

dp[i] reaches the number of arithmetic sequences at position i

2. Status representation

If nums[i]-nums[i-1]==nums[i-1]-nums[i-2], then it forms an arithmetic array of three numbers,

If it was an arithmetic array of three numbers before, adding one number can form an arithmetic array of four numbers.

dp[i]=dp[i-1]+1 (how many arithmetic arrays) Then if it changes from 3 to 4, 4-3 means adding 1, and the remaining one is Among the three, in other words, there is the above judgment condition, which is to judge three for you, but if you say it is 4, it will not be counted, so if it is 4, add 1 or 5 more One needs to add one more 4, and one more five, so that the rule gradually becomes i-2 (I mean if it is five minus three)

Then check whether the three are an arithmetic array

3.Initialization:

Less than 3 is 0

4. Order of filling in the form:

from left to right

5.Return value

Just return the maximum value in the dp table

class Solution {
    public static int numberOfArithmeticSlices(int[] nums) {
        int m=nums.length;
        int[]dp=new int[m];
        if(m<3){
            return 0;
        }
        int max=0;
        for(int i=2;i<m;i++){

            if(nums[i]-nums[i-1]==nums[i-1]-nums[i-2]){
                dp[i]=dp[i-1]+1;
                if(i-2>0&&dp[i-1]!=0){
                    dp[i]=dp[i]+i-2;
                }
               max=Math.max(dp[i-1],max);
                if(i-2>0&&dp[i-1]==0){
                    dp[i]=max+1;
                }
                 max=Math.max(dp[i],max);
            }
        }
        int ret=0;
        for(int i=0;i<m;i++){
            ret=Math.max(dp[i],ret);
        }
        return ret;
    }
}

4. The longest turbulence subarray

The turbulence array represented by a graph is equivalent to

大概就是这种图像的含义。

    *        *
*       *
         

1. Status representation

dp[i]: length of the longest turbulence array reaching position i

2. Status representation

if(n%2==0){

If nums[i]>nums[i+1]}

else{

nums[i]<nums[i+1]}

dp[i]=dp[i-1]+1

Here we find one thing, an array, which can only represent the current situation at most.

But there are three states in this place, so it cannot be said that only one state can express the turbulence array.

So we decided to use f[i],g[i], to represent the first two cases, the last one is 0

f[i]: Indicates that at the i position, it shows a downward trend with the i-1 position, and the longest turbulence array is long

g[i]: indicates that at the i position, it shows an upward trend with the i-1 position, and the longest turbulence array length

那么if(nums[i-1]>nums[i]){

f[i]=g[i-1]+1;

g[i]=1;

}

if(nums[i]<nums[i+1]){

f[i]=1;

g[i]=f[i-1]+1;

}

3.Initialization

Because if there is only one number, then the length of the turbulence array is 1, so this defaults to 1.

from 1 to n

4. Order of filling in the form

from left to right

5.Return value

Return maximum value

class Solution {
    public int maxTurbulenceSize(int[] arr) {
        int m=arr.length;
        int[]f=new int[m];
        int[]g=new int[m];
        for(int i=0;i<m;i++){
          f[i]=g[i]=1;
        }
      for(int i=1;i<m;i++){
         if(arr[i-1]>arr[i]){
             f[i]=g[i-1]+1;
             g[i]=1;
           }
           if(arr[i-1]<arr[i]){
              f[i]=1;
              g[i]=f[i-1]+1;
            }
      }
      int ret=0;
      for(int i=0;i<m;i++){
          ret=Math.max(ret,Math.max(g[i],f[i]));
      }
      return ret;
    }
}

Guess you like

Origin blog.csdn.net/weixin_72953218/article/details/134151404