Efficient algorithm to determine if two sets of numbers are disjoint

后端 未结 4 1855
生来不讨喜
生来不讨喜 2020-12-21 11:07

Practicing for software developer interviews and got stuck on an algorithm question.

Given two sets of unsorted integers with array of length m and other of          


        
相关标签:
4条回答
  • 2020-12-21 11:18

    Fairly obvious approach - sort the array of length m - O(m log m). For every element in the array of length n, use binary search to check if it exists in the array of length m - O(log m) per element = O(n log m). Since m<n, this adds up to O(n log m).

    0 讨论(0)
  • 2020-12-21 11:22

    Looks like Cheruvian beat me to it, but you can use a hash table to get O(n+m) in average case:
    *Insert all elements of m into the table, taking (probably) constant time for each, assuming there aren't a lot with the same hash. This step is O(m)
    *For each element of n, check to see if it is in the table. If it is, return false. Otherwise, move on to the next. This takes O(n).
    *If none are in the table, return true.

    As I said before, this works because a hash table gives constant lookup time in average case. In the rare event that many unique elements in m have the same hash, it will take slightly longer. However, most people don't need to care about hypothetical worst cases. For example, quick sort is used more than merge sort because it gives better average performance, despite the O(n^2) upper bound.

    0 讨论(0)
  • 2020-12-21 11:24

    Using a datastructure that has O(1) lookup/insertion you can easily insert all elements of first set.

    Then foreach element in second set, if it exists not disjoint, otherwise it is disjoint

    Pseudocode

    function isDisjoint(list1, list2)
        HashMap = new HashMap();
        foreach( x in list1)
            HashMap.put(x, true);
    
        foreach(y in list2)
            if(HashMap.hasKey(y))
                 return false;
        return true;
    

    This will give you an O(n + m) solution

    0 讨论(0)
  • 2020-12-21 11:35

    Here's a link to a post that I think answers your question.

    3) Sort smaller O((m + n)logm)

    1. Say, m < n, sort A
    2. Binary search for each element of B into A

    Disadvantage: Modifies the input

    0 讨论(0)
提交回复
热议问题