【问题标题】:Finding 5 elements in array whose sum is closest or equal to a given number在数组中查找总和最接近或等于给定数字的 5 个元素
【发布时间】:2012-12-27 21:45:40
【问题描述】:

我需要一些提示来找到返回 5 元素的算法,其总和最接近或等于给定数字。

这些元素是大于0 的数字,在尝试获取给定数字时,每个元素只能“使用”一次。

假设我们有一个数组{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 和我们试图获得的数字21。它应该返回{2, 3, 4, 5, 7}

非常感谢任何帮助!

【问题讨论】:

  • 提示:你可以使用带有记忆的递归来解决这个问题。
  • 从 3-SUM (en.wikipedia.org/wiki/3SUM) 开始并从那里继续前进,同时对线性时间算法失去任何希望(如果有的话)。
  • 提供的“21”可能的最大值是多少。在问题中可能存在负值?
  • 我不确定我是否理解你的问题。我们正在搜索的数字没有最大值,它可以是 21 或更多(只要它是正值)。但数组不会有任何负值。

标签: c arrays algorithm


【解决方案1】:
ok n loops all begin with counts = 0 (in this case n = 5 )
all loops end at MainArraySize (in this case MainArraySize = 10)

int RequiredSum = ValueOfRequiredSum;
int CurrentSum = 0;
int CurrentClosestSum = 0;
int[] Finalindexesrequired = int[5]{0,0,0,0,0};

//U might want to add Duplicates 
duplicate_count ++;
int[5][] FinalindexesRequiredDuplicates= new int[5][];


for(int loopcount1 = 0, loopcount1++, loopcount < MainArraySize-1)
{
for(int loopcount2 = 0, loopcount2++, loopcount < MainArraySize-1)
{..
..
..
for(int loopcount5 = 0, loopcount5++, loopcount < MainArraySize-1)
{

-------------------------------------
Now this is all inside the 5th loop
//Process logic here
//Looping for first time
if(CurrentSum = 0)
{Currentsum = MainArray[loopcount1] + MainArray[loopcount2] + .... + MainArray[loopcount5]
CurrentClosestSum = CurrentSum
FinalindexesRequired[0] = loopcount1;
FinalindexesRequired[1] = loopcount2;
..
..
FinalindexesRequired[4] = loopcount2;
}

Currentsum = MainArray[loopcount1] + MainArray[loopcount2] + .... + MainArray[loopcount5]


if((RequiredSum - CurrentSum) < (RequiredSum - CurrentClosestSum))
{
//Am gonna change the indexes because the currentsum ITERATION is closer
FinalindexesRequired[0] = loopcount1;
FinalindexesRequired[1] = loopcount2;
..
..
FinalindexesRequired[4] = loopcount2;

//If u wanted the duplicates also, since u came to a fresher ITERATION
Reset the duplicatecount to 0 and remove all duplicates because they aint valid anymore
}

//What u Might want to Add is this
if((Requiredsum - CurrentSum) = (RequiredSum - CurrentCosestSum))
{
//Hey we got Duplicates
duplicate_count ++;
FinalindexesRequiredDuplicates[0][duplicate_count] = loopcount1;
FinalindexesRequiredDuplicates[1][duplicate_count] = loopcount1;
..
..
FinalindexesRequiredDuplicates[5][duplicate_count] = loopcount1;
}

}



-------------------------------- End of 5th loop
}}}}}



//FINALLY AFTER EXITING I THINK U HAVE UR ANSWER IN

MAINARRAY[FINALINDEXESREQUIRED[0]]
MAINARRAY[FINALINDEXESREQUIRED[1]]
MAINARRAY[FINALINDEXESREQUIRED[2]]
MAINARRAY[FINALINDEXESREQUIRED[3]]
MAINARRAY[FINALINDEXESREQUIRED[4]]


//IN CASE OF DUPLICATES U HAVE UR ANSWER IN
set 1:
MAINARRAY[FINALINDEXESREQUIRED[0]]
MAINARRAY[FINALINDEXESREQUIRED[1]]
MAINARRAY[FINALINDEXESREQUIRED[2]]
MAINARRAY[FINALINDEXESREQUIRED[3]]
MAINARRAY[FINALINDEXESREQUIRED[4]]

other sets:
MAINARRAY[FinalindexesRequiredDuplicates[0][0...n]]
MAINARRAY[FinalindexesRequiredDuplicates[1][0...n]]
MAINARRAY[FinalindexesRequiredDuplicates[2][0...n]]
MAINARRAY[FinalindexesRequiredDuplicates[3][0...n]]
MAINARRAY[FinalindexesRequiredDuplicates[4][0...n]]

【讨论】:

  • 嗯,我的意思是我只是想通过快速修复让你摆脱问题。这显然不是最好的修复,但这肯定会奏效……其他用户显然使用 Log 获得了更好的解决方案和更好的算法,但是嘿,理解 cud 需要更多的时间......请注意,这只是一个快速修复!
【解决方案2】:

如果目标是 21,为什么不应该返回 {2,3,4,5,7} 而不是 {2,3,4,5,6}? 很混乱……

如果我的理解是正确的,这个问题可以通过DP解决。它与背包问题非常相似。时间复杂度是 O(n*S) 其中 n 是数组的大小,S 是你的目标

【讨论】:

    猜你喜欢
    • 2011-01-05
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多