-
MYYA
我不知道你的编程语言是啥,我用PHP写一下:
$data = [
0 => [
'price1' => 223.50,
'price2' => 149.00
],
1 => [
'price1' => 234.50,
'price2' => 144.00
],
2 => [
'price1' => 250.50,
'price2' => 140.00
],
3 => [
'price1' => 250.50,
'price2' => 0.00
]
];
$minList = [];
$maxList = [];
foreach($data as $key => $item) {
$price1 = $item['price1'];
$price2 = $item['price2'];
$minNum = 0;
if(!empty($price1) && !empty($price2)) {
$minNum = min($price1,$price2);
} else {
if(!empty($price1)) {
$minNum = $price1;
}
if(!empty($price2)) {
$minNum = $price2;
}
}
if(!empty($minNum)) {
$minList[$key] = $minNum;
}
}
$minNumber = min($minList); // 最小数
$minKey = array_search($minNumber);// 最小值下标
根据你修改问题做了修改,数据我就采用我原本写的吧,懒得改了。至于利用原生函数处理之后的速度与自己进行写代码遍历的两个操作的效率哪个高,没对比过,你可以自己测试一下。
-
largeQ
function buy(&$list) {
$price = 9e10; // 选择一个不可能出现的大价格, 主要为了后面少一个判断
$key = false;
foreach ($list as $k => &$item /* 传参, 减少可能的值复制 */) {
$pr1 = $item['price1'];
$pr2 = $item['price2'];
if ($pr1 > 0) {
$min = ($pr2 < $pr1 && $pr2) ? $pr2 : $pr1;
}
elseif ($pr2 > 0) {
$min = $pr2;
}
else {
continue;
}
if ($min < $price) {
$price = $min;
$key = $k;
}
}
return ($key === false) ? false : [$key, $price];
}
总的来说, 一次循环, 平均三次比较. 循环时使用传参不传值, 避免不必要的变量复制.
-
繁星coding
直接判定最小就是,没必要非要用数组全纪录下来再取最小值:
$data = [
0 => [
'price1' => 223.50,
'price2' => 149.00
],
1 => [
'price1' => 234.50,
'price2' => 144.00
],
2 => [
'price1' => 250.50,
'price2' => 140.00
],
3 => [
'price1' => 250.50,
'price2' => 0.00
]
];
$minKey=-1;
$minPrice=0;
foreach ($data as $key=>$value) {
$price1 = $value['price1'];
$price2 = $value['price2'];
if ($minPrice==0) { //先获取初始值
if ($price1 >= $price2 && $price2 !=0) {
$minPrice=$price2;
$minKey=$key;
}
if ($price2 > $price1 && $price1 !=0) {
$minPrice=$price1;
$minKey=$key;
}
} else {//继续判定
if ($minPrice >$price1 && $price1 !=0) {
$minPrice=$price1;
$minKey=$key;
}
if ($minPrice >$price2 && $price2 !=0) {
$minPrice=$price2;
$minKey=$key;
}
}
}
echo $minKey,PHP_EOL;
echo $minPrice;
-
FFIVE
$data = [
0 => [
'price1' => 223.50,
'price2' => 149.00
],
1 => [
'price1' => 234.50,
'price2' => 144.00
],
2 => [
'price1' => 250.50,
'price2' => 140.00
],
3 => [
'price1' => 250.50,
'price2' => 13414
]
];
$data = new \RecursiveArrayIterator($data);
$iterator = new \RecursiveIteratorIterator($data,\RecursiveIteratorIterator::SELF_FIRST);
$iteratorArr = [];
foreach ($iterator as $key=>$element) {
$d = $iterator->getDepth();
if($d%2 !=0 && is_numeric($a)){
$arrTmp[] = $element;
}
}
echo min($arrTmp);
-
三国纷争
$min = 0;
$k = 0;
foreach($data as $key => $val){
$temp = min($val['price1'], $val['price2']);
if($temp){
if($key == 0 || $min > $temp){
$min = $temp;
$k = $key;
}
}
}