Commit 06845a0
Changed files (1)
spec
spec/sum_of_two_spec.rb
@@ -38,6 +38,7 @@ true if there are two elements from a and b which add up to v, and false otherwi
DOC
describe "sum_of_two" do
+ # time: nlogn
def sum_of_two(a, b, v)
outer, inner = a.size > b.size ? [b.sort, a.sort] : [a.sort, b.sort]
outer.each do |i|
@@ -47,12 +48,17 @@ describe "sum_of_two" do
false
end
+ # time: n
def sum_of_two(a, b, v)
hash = {}
a.each { |x| hash[x] = true }
b.any? { |x| hash[v - x] }
end
+ def sum_of_two(a, b, v)
+ (a.map { |x| v - x } & b).size > 0
+ end
+
[
{ a: [1, 2, 3], b: [10, 20, 30, 40], v: 42, expected: true },
{ a: [1, 2, 3], b: [10, 20, 30, 40], v: 50, expected: false },