Algorithm to calculate number of intersecting discs

前端 未结 30 1488
鱼传尺愫
鱼传尺愫 2020-12-12 10:57

Given an array A of N integers we draw N discs in a 2D plane, such that i-th disc has center in (0,i) and a radius

30条回答
  •  自闭症患者
    2020-12-12 11:37

    Here is the PHP code that scored 100 on codility:

    $sum=0;
    
    //One way of cloning the A:
    $start = array();
    $end = array();
    
    foreach ($A as $key=>$value)
    {
    $start[]=0;
    $end[]=0;   
    }  
    
    for ($i=0; $i= count($A))   
      $end[count($A)-1]++;
      else
      $end[$i+$A[$i]]++;
    }
    $active=0;
    for ($i=0; $i10000000) return -1;
      $active += $start[$i]-$end[$i];
    }
    return $sum;
    

    However I dont understand the logic. This is just transformed C++ code from above. Folks, can you elaborate on what you were doing here, please?

提交回复
热议问题