Written interview questions: sum of two numbers & sum of three numbers

      Originally published in:

 

 

      The sum of two numbers (two sum) is a typical written interview question, and it is also the first question of LeetCode. For ease of description, I simplify the original question:

      Given an array a[N], determine whether the sum of two elements is k.

 

Algorithm 1: Two-layer loop

     For each a[i], we traverse the array a[N] again, and then determine whether the value of a[i]+a[j] is equal to k, where j> i. This is the most intuitive and violent solution. The time complexity is O(N*N), and the space complexity is O(1).

      If you answer this way, although it is correct, you will definitely fail the written interview.

     Analyze, for each a[i], in the process of finding a reasonable a[j], linear search is used. Obviously it is a hit. Why not use hash search?

 

Algorithm 2: Single-layer loop + hash table

      Let's improve Algorithm 1:

     For each a[i], you need to find the value of ka[i] in the array a[N], and the fastest lookup is a hash lookup (time complexity is O(1)). Therefore, the overall time complexity The degree is O(N). Since a hash table is used, the space complexity is also O(N).

     This practice of trading space for time is often used. This solution is to meet the requirements of the written interview.

     The code is implemented as follows (the result is the subscript of the value that meets the condition):

func twoSum(nums []int, target int) []int {
    var result []int
    m := make(map[int]int)
    lenN := len(nums)
    for i, v := range nums {
        m[v] = i
    }

    for i := 0; i <  lenN; i++ {
        if j, ok := m[target - nums[i]]; ok && i < j {
            result = append(result, i, j)
        }
    }

    return result
}

      The test results are as follows:

 

     The above code, although it is a single-layer loop, but the loop is used twice, in fact, it can continue to be optimized and unified into one loop. This optimization is not difficult, so I won't repeat it.

 

Algorithm 3: Fast sorting + double pointer

      What if the space complexity is required to be O(logN) and the time complexity is as low as possible?

      You can consider quick sort and double pointer method, as follows:

      Step 1: Quick sort

      Step 2: Double pointer clamping force, as follows:

      The loop condition is while(left <high):

      If a[left]+a[right] happens to be equal to k, then two numbers whose sum is k are found.

      If a[left]+a[right] is smaller than k, then left++, try with a larger number.

      If a[left]+a[right] is greater than k, then righ--, try with a smaller number. 

      

     This solution is also quite clever. The overall time complexity is O(N*logN), and the space complexity is O(logN), which can also meet the requirements of written interviews.

     

 

     The sum of two numbers was discussed above, so how to deal with the problem of the sum of three numbers? Obviously, there are also three methods:

     Algorithm 1: Three-layer loop (this violent solution cannot pass the written interview)

     The time complexity is O(N*N*N), and the space complexity is O(1).

 

     Algorithm 2: Double loop + hash table

     The time complexity is O(N*N), and the space complexity is O(N).

 

     Algorithm 3: Fast sorting + double pointer

     The time complexity is O(N*N), and the space complexity is O(logN).

 

 

     Interviewers from Alibaba, Tencent, and Toutiao really like Leetcode. I wish I got my favorite offer.

 

Guess you like

Origin blog.csdn.net/stpeace/article/details/108921742