Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单的PHP程序需要更少的时间来执行_Php_Algorithm_Std - Fatal编程技术网

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

简单的PHP程序需要更少的时间来执行,php,algorithm,std,Php,Algorithm,Std,我最近申请了一份工作,要求完成一项测试,然后面试 测试中给出了两个问题,非常简单,我成功地完成了测试,但仍然有人告诉我测试失败,因为脚本花了超过18秒完成执行。这是一个程序,我不明白我还能做些什么来加快进度。虽然我考试不及格,但还是想知道我还能做些什么? 程序语言是PHP,我必须使用命令行输入 问题是: K Difference Given N numbers , [N<=10^5] we need to count the total pairs of numbers that hav

我最近申请了一份工作,要求完成一项测试,然后面试 测试中给出了两个问题,非常简单,我成功地完成了测试,但仍然有人告诉我测试失败,因为脚本花了超过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.
K差
给定N个数字,[N0和k0;$i--){
对于($j=$i-1;$j>=0;$j--){
如果($ari_nums[$i]-$ari_nums[$j]==$diff){
$count++;
$j=0;
}
}
}
fprintf($fw,“%d,$count);

算法的运行时间为O(N^2),约为10^5*10^5=10^10。通过一些基本观察,可以将其简化为O(NlgN),约为10^5*16=1.6*10^6

算法:

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

    for($i=$total\u nums-1;$i>0;$i--){
    $hi=$i-1;
    $low=0;
    而($hi>=$low){
    $mid=($hi+$low)/2;
    如果($ary\u nums[$mid]==$ary\u nums[$i]-$diff){
    $count++;
    打破
    }
    
    如果($ari_nums[$mid]我在技术面试中得到了同样的问题。我想知道我们是否在为同一家公司面试。:)

    无论如何,以下是我(面试后)得出的答案:

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

    $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);
    
    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;
                }
            }
        }
    }
    
    // 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;
          }
       }
    }
    
    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