Commit 3ee369b

mo khan <mo.khan@gmail.com>
2020-08-17 16:34:40
Solve two sum with a hash table
1 parent 377586e
Changed files (2)
2020/08/17/main.rb
@@ -0,0 +1,25 @@
+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))
+puts "Yay!"
2020-08-17/README.md → 2020/08/17/README.md
File renamed without changes