代码之家  ›  专栏  ›  技术社区  ›  joebert

查找连续数组键的最长跨度

php
  •  0
  • joebert  · 技术社区  · 14 年前

    我有一个数组,它使用UNIX时间戳作为数组键。数组通常会保存15分钟到1小时的数据,但是只有几秒钟的条目有数据。

    大多数数据都会分散开来,但偶尔会有连续几秒钟的数据跨度。我想做的是,检索数组中最长连续秒跨度的第一秒和最后一秒。

    Array
    (
        [1276033307] => 119.0
        [1276033331] => 281.8
        [1276033425] => 28.2
        [1276033431] => 88.2
        [1276033432] => 196.2
        [1276034207] => 205.5
        [1276034226] => 73.8
        [1276034227] => 75.8
        [1276034228] => 77.8
        [1276034230] => 79.8
    )
    

    我需要密钥1276034226和1276034228,或者返回以下数组。

    Array
    (
        [1276034226] => 73.8
        [1276034227] => 75.8
        [1276034228] => 77.8
    )
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   Lukman    14 年前

    此代码在单个循环中执行(即是贪婪算法):

    $array = array(
        1276033307 => 119.0,
        1276033331 => 281.8,
        1276033425 => 28.2,
        1276033431 => 88.2,
        1276033432 => 196.2,
        1276034207 => 205.5,
        1276034226 => 73.8,
        1276034227 => 75.8,
        1276034228 => 77.8,
        1276034230 => 79.8,
    );
    
    $long_arr = array();
    $curr_arr = array();
    $last_key = -1;
    
    foreach($array as $k => $v) {
        if ($k != $last_key + 1) {
            $curr_arr = array();
        }
        $curr_arr[$k] = $v;
        if (count($curr_arr) > count($long_arr)) {
            $long_arr = $curr_arr;
        }
        $last_key = $k;
    }
    
    print_r($long_arr);
    
        2
  •  2
  •   acm    14 年前

    编辑:

    $array = array(
        1276033307 => 119.0,
        1276033331 => 281.8,
        1276033425 => 28.2,
        1276033431 => 88.2,
        1276033432 => 196.2,
        1276034207 => 205.5,
        1276034226 => 73.8,
        1276034227 => 75.8,
        1276034228 => 77.8,
        1276034230 => 79.8,
    );
    
    $finalArray = array();
    foreach($array as $k => $v){
        $tempArrays = array();
        $index = 0;
        while(isset($array[$k + $index])){
            $tempArrays[$k+$index] = $array[$k+$index++];
        }
    
        if(count($tempArrays) > count($finalArray))
            $finalArray = $tempArrays;
    }
    
    print_r($finalArray);
    

    原件:

    注:只记录第一次出现的最长跨度。

    $array = array(
        1276033307 => 119.0,
        1276033331 => 281.8,
        1276033425 => 28.2,
        1276033431 => 88.2,
        1276033432 => 196.2,
        1276034207 => 205.5,
        1276034226 => 73.8,
        1276034227 => 75.8,
        1276034228 => 77.8,
        1276034230 => 79.8,
    );
    
    $longspan = 0;
    foreach($array as $k => $v){
        $index   = 1;
        while(isset($array[$k+$index])){
            $index++;
        }
    
        $curspan = --$index;
    
        if($curspan > $longspan){
            $longspan = $curspan;
            $start    = $k;
        }
    }
    
    for($i=0; $i <= $longspan; $i++)
        $results[$start + $i] = $array[$start + $i];
    
    print_r($results);
    

    输出:

    Array ( 
      [1276034226] => 73.8 
      [1276034227] => 75.8 
      [1276034228] => 77.8
    )