Dynamic programming: find the longest subsequence that is zig zag using only one dp array

Is it possible to solve this problem using only one dp array? This is a zigzag problem from topcoder ( http://community.topcoder.com/stat?c=problem_statement&pm=1259&rd=4493 ) A sequence of numbers is called a zigzag sequence if the differences between consecutive numbers strictly alternate between positive and negative. The first difference (if it exists) can be both positive and negative. A sequence with less than two elements is trivially a zigzag sequence.

For example, 1,7,4,9,2,5 is a zigzag sequence, since the differences (6, -3.5, -7.3) are alternately positive and negative. In contrast, 1,4,7,2,5 and 1,7,4,5,5 are not zigzag sequences, the first because its first two differences are positive, and secondly because its last difference is zero.

Given a sequence of integers, a sequence, we return the length of the longest sequence of the sequence, which is a zigzag sequence. A subsequence is obtained by removing a certain number of elements (possibly zero) from the original sequence, leaving the remaining elements in the original order.

+4
source share
5

: DP A [1..n], A [i] - , i, B [1..n], B [i] - , zag i. 1 n DP A B [i] B A [i]. B , , A. , .

( , , , .)

0

, , . (1,4,7,2,5) 5 4, 4,7,2,5.

, .

 public class LongestZigZag
    {
        private readonly int[] _input;

        public LongestZigZag(int[] input)
        {
            _input = input;
        }

        public Tuple<int,int> Sequence()
        {
            var indices = new Tuple<int, int>(int.MinValue, int.MinValue);

            if (_input.Length <= 2) return indices;

            for (int i = 2; i < _input.Length; i++)
            {
                var firstDiff = _input[i - 1] - _input[i - 2];
                var secondDiff = _input[i] - _input[i - 1];

                if ((firstDiff > 0 && secondDiff < 0) || (firstDiff < 0 && secondDiff > 0))
                {
                    var index1 = indices.Item1;
                    if (index1 == int.MinValue)
                    {
                        index1 = i - 2;
                    }

                    indices = new Tuple<int, int>(index1, i);
                }
                else
                {
                    indices = new Tuple<int, int>(int.MinValue, int.MinValue); 
                }
            }

            return indices;
        }
    }
0

O (n 2) . , O (n). . , , .

C:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,j;
int n;
int count=0;
int flag=0;
scanf(" %d",&n);
int *a;
a = (int*)malloc(n*sizeof(a));

for(i=0;i<n;i++)
{
    scanf(" %d",&a[i]);  //1,7,5,10,13,15,10,5,16,8
}   
i=0; 
if(a[0] < a[1])
{
    count++;
    while(a[i] <= a[i+1] && i<n-1)
    i++;

    if(i==n-1 && a[i-1]<a[i])
    {
        count++;
        i++;
    }    
}  
  while(i<n-1)
  {   count++;
      while(a[i] >= a[i+1] && i<n-1) 
      {
        i++;  
      }
      if(i==n-1 && a[i-1]>a[i])
      {
          count++; 
          break;
      }      
      if(i<n-1)
      count++;
      while(a[i] <= a[i+1] && i<n-1)
      {
          i++;
      } 
      if(i==n-1 && a[i-1]<a[i])
      {
          count++;
          break;
      }         
  }      

printf("%d",count);
return 0;
}     
0

( , ) , , " " ( , , ) DAG ( ).

, , DAG:

  • Edge e(u, v) , valueOf(u) < valueOf(v) ( valueOf(x) - , node x)

- , . , , โ€‹โ€‹ DP.

. , - ( <, >).

DAG, DP - , DAG (, , ).

, , DP. . , , .


( , ):

S - given sequence (array of integers)
P(i), Q(i) - length of the longest zigzag subsequence on elements S[0 -> i] inclusive (the longest sequence that is correct, where S[i] is the last element)

P(i) = {if i == 0 then 1
       {max(Q(j) + 1 if A[i] < A[j] for every 0 <= j < i)


Q(i) = {if i == 0 then 0  #yields 0 because we are pedantic about "is zig the first relation, or is it zag?". If we aren't, then this can be a 1.
       {max(P(j) + 1 if A[i] > A[j] for every 0 <= j < i)

O (n) memoization ( DP). - , " " , , .

0
public class ZigZag
{

  public void maxZigZag(int[] zigzag)
  {
    int sign[]=new int[zigzag.length];
    int p=0;
    int max=1;
        for(int j=0;j<zigzag.length-1;j++)
        {
            if(zigzag[j+1]-zigzag[j] > 0)
                sign[p++]=1;
            else if(zigzag[j+1]-zigzag[j] < 0)
                sign[p++]=-1;
        }
        /*for(int s:sign)
        {
            System.out.print(s+",");
        }*/
        System.out.println();
        for(int k=0;k<p;k++)
        {
            if(sign[k]==sign[k+1])
            {
                sign[k]=0;

            }
        }
        /*for(int s:sign)
        {
            System.out.print(s+",");
        }*/
        System.out.println();
        for(int s=0;s<p;s++)
        {
            if(sign[s]!=0)
            {
                max++;
                /*System.out.print(sign[s]+",");*/
            }

        }

        System.out.println("max length zigzag:"+max);
  }

  public static void main(String args[])
  {
    ZigZag z=new ZigZag();
    int zigzag[]={374, 40, 854, 203, 203, 156, 362, 279, 812, 955, 
            600, 947, 978, 46, 100, 953, 670, 862, 568, 188, 
            67, 669, 810, 704, 52, 861, 49, 640, 370, 908, 
            477, 245, 413, 109, 659, 401, 483, 308, 609, 120, 
            249, 22, 176, 279, 23, 22, 617, 462, 459, 244};
    z.maxZigZag(zigzag);
  }
}
-2

Source: https://habr.com/ru/post/1528131/


All Articles