【问题标题】:return the number of distance's which is greater than threshold between each point in two arrays返回两个数组中每个点之间大于阈值的距离数
【发布时间】:2020-08-08 06:12:57
【问题描述】:

给定

distance  = a[i]*a[i]+b[j]*b[j]

我用:

int[] a = new int[] {3,-1,9};
int[] b = new int[] {100,5,-2};
int res=0;
int sum = 0;
for(int i=0;i<a.length;i++) {
    for(int j=0;j<b.length;j++) {
        sum = a[i]*a[i]+b[j]*b[j];
        if(lower<=sum&& sum<=upper)
            res++;
    }
}
return res;

上面是我的代码,其复杂度为m*n。是否有任何可能的方法来优化代码,使复杂度可以低于n^2。提前致谢。

【问题讨论】:

  • 这可以在O(n log n) 中使用 FFT 来完成,其中 n 是两个数组中较长的数组的长度

标签: arrays optimization time-complexity


【解决方案1】:

这个时间复杂度是O(m*n)

您将不得不检查所有可能性,因此您无法进一步优化它。除非您遗漏了一些有关要求的信息。

【讨论】:

  • "因此无法进一步优化" 您可以使用 FFT 将其设为 n log n
猜你喜欢
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-18
  • 2015-02-25
  • 1970-01-01
相关资源
最近更新 更多