1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
def assert(x)
raise x.inspect unless x
end
class Solution
# time: O(n)
# space: O(n)
def self.run(items, k)
nums = {}
for i in (0...items.size)
nums[items[i]] = true
return true if nums[k - items[i]]
end
false
end
end
=begin
h
|4|7|1|-3|2|
=end
assert(Solution.run([4, 7, 1, -3, 2], 5))
assert(!Solution.run([], 5))
assert(!Solution.run([1], 5))
assert(Solution.run([1,-3,7,8], 5))
assert(Solution.run([3,1,3], 6))
puts "Yay!"
|