【发布时间】:2014-05-07 18:54:36
【问题描述】:
刚刚遇到这个问题:
Sub-set sum problem : Finding the count of two pairs of numbers in a given array whose sum is equal to a given number
例如:给定总和为 9,数组为 { 0, 1, 2, 7, 13 } => O/P 为 1 对(2 和 7)
似乎这可以在O(n)中实现(构建一个哈希表或字典,迭代给定数组的每个元素并从给定的总和中减去,检查结果数是否在数组)
显然,遍历数组的每个元素需要O(n) 时间。
My question is what is the time complexity and the space complexity for building the hash-table or the dictionary ?
注 1:我的猜测是构建哈希表或字典的 O(n),同样我们必须遍历数组中的每个元素。 Is this correct ?
注 2:所以,复杂度是 O(n) + O(n) = 2 * O(n) 对吧? (但答案似乎只是 O(n))
【问题讨论】:
标签: algorithm dictionary hashtable subset-sum