I tried shortening my question, but this is the best I could do. Since I guess it's pretty hard to understand what I need, I'll provide an example:
我试图缩短我的问题,但这是我能做的最好的。因为我觉得很难理解我需要什么,我举个例子:
Say I have the following array:
假设我有以下数组:
$var = array(
array(1, 2, 3),
array(1, 3),
array(1, 2, 4, 3),
array(1, 3, 4)
);
What I want is to remove all arrays from $var
that have the first and last element the same as another array from $var
but have more elements than the latter.
我想要的是从$var中删除所有具有第一个和最后一个元素的数组,这些元素与$var中的另一个数组相同,但是元素比后者多。
So, the following arrays should be removed: (1, 2, 3)
and (1, 2, 4, 3)
because they both start with 1
and end with 3
and have more elements than (1, 3)
, which also starts and ends with 1
and 3
. (1, 3, 4)
should remain, because there is no other array which starts with 1
and ends with 4
and has fewer elements than it.
因此,应该删除下面的数组:(1、2、3)和(1、2、4、3),因为它们都从1开始,以3结束,而且元素比(1,3)更多,它们也开始并以1和3结束。(1,3,4)应该保留,因为没有其他数组以1开头,以4结尾,元素比它少。
I am looking for the most efficient way of doing this, both in terms of memory and time. $var
may have up to 100 arrays, and each individual array may have up to 10 elements in it. I thought of using some kind of comparison between all two elements (for(i=0;...) for(j=i+1;...) complexCompareFunction();
), but I believe this isn't very efficient.
我正在寻找一种最有效的方法,无论是在记忆方面还是在时间方面。$var可能有多达100个数组,每个单独的数组可能有多达10个元素。我想在(j= I +1;) complexCompareFunction()两个元素之间使用某种比较,但是我认为这不是很有效。
2 个解决方案
#1
1
In general, yes, you are too worried about efficiency (as you wondered in another comment). Though PHP is not the most blisteringly-fast language, I would suggest building the most straightforward solution, and only worry about optimizing it or streamlining it if there is a noticeable issue with the end result.
总的来说,是的,你太担心效率了(正如你在另一条评论中想的那样)。虽然PHP不是最快的语言,但是我建议构建最直接的解决方案,如果最终结果有明显的问题,我只需要考虑优化它或对它进行优化。
Here is what I would do, off the top of my head. It is based off of ajreal's answer but hopefully will be easier to follow, and catch some edge cases which that answer missed:
这就是我要做的,在我的头顶上。它是基于ajreal的答案,但希望会更容易理解,并抓住一些可能会遗漏的边缘案例:
// Assume $var is the array specified in your question
function removeRedundantRoutes( $var ){
// This line sorts $var by the length of each route
usort( $var, function( $x, $y ){ return count( $x ) - count( $y ); } );
// Create an empty array to store the result in
$results = array();
// Check each member of $var
foreach( $var as $route ){
$first = $route[0];
$last = $route[ count( $route ) - 1 ];
if( !array_key_exists( "$first-$last", $results ) ){
// If we have not seen a route with this pair of endpoints already,
// it must be the shortest such route, so place it in the results array
$results[ "$first-$last" ] = $route;
}
}
// Strictly speaking this call to array_values is unnecessary, but
// it would eliminate the unusual indexes from the result array
return array_values( $results );
}
#2
2
使用电流和结束
$all = array();
foreach ($var as $idx=>$arr):
$first = current($arr);
$last = end($arr);
$size = count($arr);
$key = $first.'.'.$last;
if (isset($all[$key])):
if ($size > $all[$key]):
unset($var[$idx]);
else:
$all[$key] = $size;
endif;
else:
$all[$key] = $size;
endif;
endforeach;
ops ... you can iterate (again) at the end to ensure the already reduced sized array can be further removed
行动……您可以在末尾(再次)迭代,以确保已经缩小的数组可以被进一步删除
#1
1
In general, yes, you are too worried about efficiency (as you wondered in another comment). Though PHP is not the most blisteringly-fast language, I would suggest building the most straightforward solution, and only worry about optimizing it or streamlining it if there is a noticeable issue with the end result.
总的来说,是的,你太担心效率了(正如你在另一条评论中想的那样)。虽然PHP不是最快的语言,但是我建议构建最直接的解决方案,如果最终结果有明显的问题,我只需要考虑优化它或对它进行优化。
Here is what I would do, off the top of my head. It is based off of ajreal's answer but hopefully will be easier to follow, and catch some edge cases which that answer missed:
这就是我要做的,在我的头顶上。它是基于ajreal的答案,但希望会更容易理解,并抓住一些可能会遗漏的边缘案例:
// Assume $var is the array specified in your question
function removeRedundantRoutes( $var ){
// This line sorts $var by the length of each route
usort( $var, function( $x, $y ){ return count( $x ) - count( $y ); } );
// Create an empty array to store the result in
$results = array();
// Check each member of $var
foreach( $var as $route ){
$first = $route[0];
$last = $route[ count( $route ) - 1 ];
if( !array_key_exists( "$first-$last", $results ) ){
// If we have not seen a route with this pair of endpoints already,
// it must be the shortest such route, so place it in the results array
$results[ "$first-$last" ] = $route;
}
}
// Strictly speaking this call to array_values is unnecessary, but
// it would eliminate the unusual indexes from the result array
return array_values( $results );
}
#2
2
使用电流和结束
$all = array();
foreach ($var as $idx=>$arr):
$first = current($arr);
$last = end($arr);
$size = count($arr);
$key = $first.'.'.$last;
if (isset($all[$key])):
if ($size > $all[$key]):
unset($var[$idx]);
else:
$all[$key] = $size;
endif;
else:
$all[$key] = $size;
endif;
endforeach;
ops ... you can iterate (again) at the end to ensure the already reduced sized array can be further removed
行动……您可以在末尾(再次)迭代,以确保已经缩小的数组可以被进一步删除