一条链子断3条最低成本计算

$arr = [5,2,4,6,3,7];
for($i=1;$i<count($arr)-2;$i++)
{
	$result = eee($arr,$i);
	$temp = (isset($temp)==false || $temp[0]>$result[0])?$result:$temp;
}
echo "成本:",$temp[0]," index:",$temp[1],",",$temp[2]," values:",$arr[$temp[1]],",",$arr[$temp[2]];


function eee($a,$p)
{
	$len = count($a);
	$temp = -1;
	for($i=1;$i<$len;$i++)
	{
		if($i<$p-1 || $i>$p+1)
		{
			if($temp==-1 || $a[$i]<$a[$temp])
			{
				$temp = $i;
			}
		}
	}
	return [$a[$p]+$a[$temp],$p,$temp];
}

  

猜你喜欢

转载自www.cnblogs.com/samxiong/p/11094652.html