PHP 中的排列和大数组 - 性能问题

发布于 2025-01-12 10:49:45 字数 1540 浏览 0 评论 0原文

我有一个数字数组(int 或 float),我需要通过组合数组值来找到一个值。一旦找到最小可能的组合,该函数就会返回数组值。因此,我从样本大小=1开始并不断增加它。

这是给定数据的简化示例:

$values = [10, 20, 30, 40, 50];
$lookingFor = 80;

有效结果:

[30, 50] // return this
[10, 20, 50], [10, 30, 40] // just to demonstrate the possible combinations

排列解决了这个问题,我尝试了许多不同的实现(例如:排列 - 所有可能的数字组, 获取 PHP 数组的所有排列?, https://github.com/drupol/phpermutations)。我最喜欢的是这个,它带有使用生成器模式的排列大小参数:https://stackoverflow.com/a/43307800

我的问题是什么?性能!我的数组有 5 - 150 个数字,有时需要 30 个数组数字的总和才能找到搜索的值。有时找不到该值,这意味着我需要尝试所有可能的组合。基本上与排列大小> 5 任务变得太耗时。

另一种但不精确的方法是对数组进行排序,获取前 X 和后 X 数字并与搜索到的值进行比较。像这样:

sort($values, SORT_NUMERIC);
$countValues = count($values);

if ($sampleSize > $countValues)
{
    $sampleSize = $countValues;
}

$minValues = array_slice($values, 0, $sampleSize);
$maxValues = array_slice($values, $countValues - $sampleSize, $sampleSize);

$possibleMin = array_sum($minValues);
$possibleMax = array_sum($maxValues);

if ($possibleMin === $lookingFor)
{
    return $minValues;
}

if ($possibleMax === $lookingFor)
{
    return $maxValues;
}

return [];

希望有人处理过类似的问题并能引导我走向正确的方向。谢谢你!

I have an array of numbers (int or float) and I need to find a value by combining array values. Once the smallest possible combination is found the function returns the array values. Therefore I start with sample-size=1 and keep incrementing it.

Here's a simplified example of the given data:

$values = [10, 20, 30, 40, 50];
$lookingFor = 80;

Valid outcomes:

[30, 50] // return this
[10, 20, 50], [10, 30, 40] // just to demonstrate the possible combinations

Permutations solve this problem and I've tried many different implementations (for example: Permutations - all possible sets of numbers, Get all permutations of a PHP array?, https://github.com/drupol/phpermutations). My favourite is this one with a parameter for permutation-size using the Generator pattern: https://stackoverflow.com/a/43307800

What's my problem? Performance! My arrays have 5 - 150 numbers and sometimes the sum of 30 array numbers is needed to find the searched value. Sometimes the value can't be found, which means I needed to try all possible combinations. Basically with permutation-size > 5 the task becomes too time consuming.

An alternative, yet not precise way is to sort the array, take the first X and last X numbers and compare with the searched value. Like this:

sort($values, SORT_NUMERIC);
$countValues = count($values);

if ($sampleSize > $countValues)
{
    $sampleSize = $countValues;
}

$minValues = array_slice($values, 0, $sampleSize);
$maxValues = array_slice($values, $countValues - $sampleSize, $sampleSize);

$possibleMin = array_sum($minValues);
$possibleMax = array_sum($maxValues);

if ($possibleMin === $lookingFor)
{
    return $minValues;
}

if ($possibleMax === $lookingFor)
{
    return $maxValues;
}

return [];

Hopefully somebody has dealt with a similar problem and can guide me in the right direction. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哑剧 2025-01-19 10:49:45
  • 您必须使用组合而不是排列{例如:P(15) = 130767436800 vs C(15) = 32768}

  • if array_sum

    target_number 则不存在解决方案

  • 如果 in_array(target_number,numbers) 找到包含 1 个元素的解

  • 从最低到最高排序

  • 从 C(n,2) 开始,其中 2 代表第 1 个、第 2 个、然后是第 1 个、第 3 个等(静态的是第一个元素)

  • 如果上面的循环没有找到解决方案,则继续第 2 个第 3 个,然后是第 2 个第 4 个,等等)

  • 如果 C(n,2) 没有解决方案,则跳转到 C(n,3)s,但这次是 2 个静态数字和 1 个动态数字< /p>

  • 如果循环结束且无解,则不存在解

,我会调整这个问题并在堆栈交换的统计分支(交叉验证)中询问,因为数字总和的平均值、中值和累积分布可能暗示减少迭代次数意义重大,这是他们的职业。

  • you must use combination instead of permutations {ex: P(15) = 130767436800 vs C(15) = 32768}

  • if array_sum < target_number then no solution exists

  • if in_array(target_number, numbers) solution found with 1 element

  • sort lowest to highest

  • start with C(n,2) where 2 represents 1st 2nd then 1st 3rd etc (static one is 1st element)

  • if above loop found no solution continue with 2nd 3rd then 2nd 4th, etc)

  • if C(n,2) had no solution then jump to C(n,3)s but this time 2 static numbers and 1 dynamic one

  • if loop ended with no solution then there exists no solution

lastly, I would adjust this question and ask in statistics branch of stack exchange (crossvalidated) since mean, median and cumulative distribution of the sums of the numbers may hint to decrease the number of iterations significantly and this is their profession.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文