A few solutions comparison


  • 0
    Solution Add Find Space
    Add the item to an array; calculate all sum and save in a hashset. time O(n) time O(1) array O(n), hash O(n^2)
    Add the item to an array; save it in a hashset; in find, go through the array and check if any sum-element exists in hashset. Need to handle sum = 2 * element case (i.e., track item count in dictionary.) time O(1) time O(n) array O(n), hash O(n)
    Add the item to an array. in find, check every two elements time O(1) time O(n^2) array O(n), no hash.
    Add the item to a sorted array. in find, use two pointers start and end to scan. time O(logn) time O(n) array O(n), no hash.

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.