简体   繁体   English

为什么具有 O(n) 时间复杂度的 leetcode 提交需要比 O(n log n) 时间复杂度更多的时间来运行?

[英]Why does leetcode submission with O(n) Time Complexity takes more time to run than O(n log n) Time Complexity?

I was solving below question on Leetcode -我在 Leetcode 上解决以下问题 -

Given two arrays, write a function to compute their intersection.

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]

I devised a solution with O(n) TC in java using a HashMap as below:我使用 HashMap 在 Java 中设计了一个 O(n) TC 的解决方案,如下所示:

Approach-1方法一

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
       int res[] = new int[nums1.length];
       Map<Integer,Integer> freqMap = new HashMap<Integer,Integer>();
        for(int i=0;i<nums1.length;i++){
          freqMap.put(nums1[i],freqMap.getOrDefault(nums1[i],0)+1);
        }
    int k = 0;
    for(int i=0;i<nums2.length;i++){
        if(freqMap.get(nums2[i]) != null && freqMap.get(nums2[i]) != 0){
           res[k] = nums2[i]; 
           freqMap.put(nums2[i],freqMap.get(nums2[i])-1);
           k++;
        }
      }
     return Arrays.copyOfRange(res,0,k);
    }
}

I saw another accepted solution with O(nlogn) TC using sorting approach as below:我看到了另一个被接受的 O(nlogn) TC 使用排序方法的解决方案,如下所示:

Approach-2方法二

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
    Arrays.sort(nums1);
    Arrays.sort(nums2);
    int i = 0, j = 0, k = 0;
    while (i < nums1.length && j < nums2.length) {
        if (nums1[i] < nums2[j]) {
            ++i;
        } else if (nums1[i] > nums2[j]) {
            ++j;
        } else {
            nums1[k++] = nums1[i++];
            ++j;
        }
    }
    return Arrays.copyOfRange(nums1, 0, k);
}
}

Now, theoretically speaking Approach-1 solution should run faster than Approach-2 but the Approach-2 solution ran in 1ms whereas Approach-1 solution ran in 2ms.现在,从理论上讲 Approach-1 解决方案应该比 Approach-2 运行得更快,但是 Approach-2 解决方案在 1ms 内运行,而 Approach-1 解决方案在 2ms 内运行。

Can anyone explain why this can happen?谁能解释为什么会发生这种情况?

PS - The runtimes were calculated by leetcode on submission PS - 运行时间由 leetcode 在提交时计算

Edit- With the new comments I'm pondering over some new questions now.编辑 - 有了新评论,我现在正在思考一些新问题。

Since this looks to be affected by constant factor in big O .因为这看起来受大 O 中的常数因素的影响。 I would like to know which are the factors which can cause time difference in this specific case?我想知道在这种特定情况下哪些因素会导致时差?

And is using Array sort over Hashmap always better for calculations with integer value of n?对于整数值为 n 的计算,在 Hashmap 上使用数组排序总是更好吗?

Threre are endless variables that can affect runtime, variance, connection, proseccor load, how things are placed in memory and so on.有无穷无尽的变量会影响运行时、方差、连接、proseccor 负载、事物在内存中的放置方式等。 This is the reason we use O(n) notation, as its the only reasonable way to compare two algorithms.这就是我们使用 O(n) 表示法的原因,因为它是比较两种算法的唯一合理方法。

Also some algoritms have a large fixed cost, which means they are slower when n is small because the scaling factor doesnt get enough data to scale.还有一些算法有很大的固定成本,这意味着当 n 小时它们会变慢,因为缩放因子没有获得足够的数据来缩放。

When we calculate O(n) we cut out the fixed costs as when n gets large the fixced costs mean less and less in terms of time used.当我们计算 O(n) 时,我们削减了固定成本,因为当 n 变大时,固定成本意味着使用的时间越来越少。 This way, our big O notation will not differentiate between 999n and n, even though the latter does less operations.这样,我们的大 O 符号就不会区分 999n 和 n,即使后者的运算较少。 Remember that 10n is slower(more operations) than n^2 when n < 10, even though O(n) is considered faster than O(n^2)请记住,当 n < 10 时,10n 比 n^2 慢(更多操作),即使 O(n) 被认为比 O(n^2) 快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM