简单的PHP程序需要较少的执行时间

时间:2013-07-25 05:11:38

标签: php algorithm std

我最近申请了一份工作,要求是完成一项测试,然后进行面试 为测试提供了2个问题,这非常简单,我成功了但是我被告知我测试失败,因为脚本花了超过18秒才完成执行。这是程序,我不明白我还能做些什么来使它快速。虽然我没有通过测试,但仍然想知道我能做什么? 程序语言是PHP,我必须使用命令行输入

这是一个问题:

 K Difference
Given N numbers , [N<=10^5] we need to count the total pairs of numbers that have a difference of K. [K>0 and K<1e9]

Input Format:
1st line contains N & K (integers).
2nd line contains N numbers of the set. All the N numbers are assured to be distinct.
Output Format:
One integer saying the no of pairs of numbers that have a diff K.

Sample Input #00:
5 2
1 5 3 4 2

Sample Output #00:3
Sample Input #01:
10 1
363374326 364147530 61825163 1073065718 1281246024 1399469912 428047635 491595254 879792181 1069262793 
Sample Output #01:
0
Note: Java/C# code should be in a class named "Solution"
Read input from STDIN and write output to STDOUT.

这是解决方案

$fr = fopen("php://stdin", "r");
$fw = fopen("php://stdout", "w");

fscanf($fr, "%d", $total_nums);
fscanf($fr, "%d", $diff);

$ary_nums = array();
for ($i = 0; $i < $total_nums; $i++) {
    fscanf($fr, "%d", $ary_nums[$i]);
}

$count = 0;
sort($ary_nums);
for ($i = $total_nums - 1; $i > 0; $i--) {
    for ($j = $i - 1; $j >= 0; $j--) {
        if ($ary_nums[$i] - $ary_nums[$j] == $diff) {
            $count++;
            $j = 0;
        }
    }
}
fprintf($fw, "%d", $count);

2 个答案:

答案 0 :(得分:3)

你的算法的运行时间是O(N ^ 2),大约是10 ^ 5 * 10 ^ 5 = 10 ^ 10。通过一些基本的观察,它可以减少到O(NlgN),大约只有10 ^ 5 * 16 = 1.6 * 10 ^ 6。

算法:

  1. 对数组ary_nums进行排序。
  2. 对于数组的每个第i个整数,进行二进制搜索以查找ary_nums [i] -K是否存在于数组中。如果存在增加结果,否则跳过i整数。
  3. 排序($ ary_nums);

    for ($i = $total_nums - 1; $i > 0; $i--) {
    
            $hi  = $i-1;
            $low = 0;
            while($hi>=$low){
                $mid = ($hi+$low)/2;
                if($ary_nums[$mid]==$ary_nums[$i]-$diff){
                    $count++;
                    break;
                }
                if($ary_nums[$mid]<$ary_nums[$i]-$diff){
                     $low = $mid+1;
                }
                else{
                     $hi  = $mid-1;
                }
            }
        }
    }
    

答案 1 :(得分:0)

我的技术面试时遇到了同样的问题。我想知道我们是否正在为同一家公司面试。 :)

无论如何,这是我提出的答案(采访后):

// Insert code to get the input here

$count = 0;
sort ($arr);

for ($i = 0, $max = $N - 1; $i < $max; $i++)
{
   $lower_limit = $i + 1;
   $upper_limit = $max;

   while ($lower_limit <= $upper_limit)
   {
      $midpoint = ceil (($lower_limit + $upper_limit) / 2);
      $diff = $arr[$midpoint] - $arr[$i];

      if ($diff == $K)
      {
         // Found it. Increment the count and break the inner loop.
         $count++;
         $lower_limit = $upper_limit + 1;
      }
      elseif ($diff < $K)
      {
         // Search to the right of midpoint
         $lower_limit = $midpoint + 1;
      }
      else
      {
         // Search to the left of midpoint
         $upper_limit = $midpoint - 1;
      }
   }
}

@Fallen:您的代码因以下输入而失败:

Enter the numbers N and K: 10 3
Enter numbers for the set: 1 2 3 4 5 6 7 8 9 10
Result: 6 

我认为这与您对$mid的计算有关(不考虑奇数)