SubSet sum与N array Solution,Dynamic Solution recquired

时间:2017-11-13 04:08:31

标签: algorithm dynamic-programming subset-sum

虽然你可以有任意数量的# Unit: seconds # expr min lq mean median uq max neval # surv_preds(cox_model, lung) 1.531631 1.561518 1.59431 1.574664 1.591117 2.157002 100 # (purrrlyr) 1.841713 1.887438 1.921371 1.90474 1.92649 2.170205 100 s但是,假设你有两个array s {1,2,3,4,5,6}和{1,2 ,3,4,5,6} 您必须在array元素的参与下找出它们总和为4。即

array

在Nutshell中:想要实现SubSet Sum算法,其中有两个数组,并且从两个数组中选择数组元素以构成目标Sum

这是我用于1 from array1, 3 from array2 2 from array1, 2 from array2 3 from array1, 1 from array2 子集和算法

array

2 个答案:

答案 0 :(得分:1)

我们可以使用3维dp实现这一点。但为了简单和可读性,我用两种方法编写了它。

注意:当我们从每个array中选择至少一个元素时,我的解决方案就有效。如果存在必须从每个array中选择相同数量的元素的条件,则它不起作用。

// This is a helper method

//  prevPosAr[] is the denotes what values could be made with participation from ALL
// arrays BEFORE the current array

// This method returns an array which denotes what values could be made with the
// with participation from ALL arrays UP TO current array


boolean[] getPossibleAr( boolean prevPossibleAr[], int ar[] )
{
    boolean dp[][] = new boolean[ ar.length + 1 ][ prevPossibleAr.length  ];
    // dp[i][j] denotes   if we can make value j using AT LEAST
    // ONE element from current ar[0...i-1]

    for (int i = 1; i <= ar.length; i++)
    {
        for (int j = 0; j < dp[i].length; j++)
        {
            if ( dp[i-1][j] == true )
            {
                // we can make value j using AT LEAST one element from ar[0...i-2]
                // it implies that we can also make value j using AT LEAST
                // one element from ar[0...i-1]
                dp[i][j] = true;
                continue;
            }

            int prev = i-ar[i-1];
            // now we look behind


            if ( prev < 0 )
            {
                // it implies that ar[i-1] > i
                continue;
            }

            if ( prevPossibleAr[prev] || dp[i-1][prev] )
            {
                // It is the main catch
                // Be careful


                // if ( prevPossibleAr[prev] == true )
                // it means that we could make the value prev
                // using the previous arrays (without using any element
                // of the current array)
                // so now we can add ar[i-1] with prev and eventually make i



                // if ( dp[i-1][prev] == true )
                // it means that we could make prev using one or more
                // elements from the current array....
                // now we can add ar[i-1] with this and eventually make i

                dp[i][j] = true;
            }
        }
    }

    // What is dp[ar.length] ?
    // It is an array of booleans
    // It denotes whether we can make value j using ALL the arrays 
    // (using means taking AT LEAST ONE ELEMENT)
    // before the current array and using at least ONE element 
    // from the current array ar[0...ar.lengh-1] (That is the full current array)

    return dp[ar.length];
}



// This is the method which will give us the output
boolean subsetSum(int  ar[][], int sum )
{
    boolean prevPossible[] = new boolean[sum+1];
    prevPossible[0] = true;


    for ( int i = 0; i < ar.length; i++ )
    {
        boolean newPossible[] = getPossibleAr(prevPossible, ar[i]); 
        // calling that helper function
        // newPossible denotes what values can be made with
        // participation from ALL arrays UP TO i th array
        // (0 based index here)



        prevPossible = newPossible;
    }

    return prevPossible[sum];
}

答案 1 :(得分:0)

步骤,

find (Array1, Array2, N)
  sort Array1
  sort Array2

  for (i -> 0 && i < Array1.length) and (j -> Array2.length-1 && j >= 0)
    if(Array1[i] + Array2[j] == N)
      return yes;
    if(Array1[i] + Array2[j] > N)
      j--;
    else
      i++;
  return NO;