Commit 272b640

mo khan <mo.khan@gmail.com>
2020-08-25 19:06:20
Solve using cubic time
1 parent b4e0004
Changed files (1)
2020
08
2020/08/24/main.rb
@@ -0,0 +1,30 @@
+def assert(value)
+  raise value.inspect unless value
+end
+
+class Solution
+  # time: O(n^3)
+  # space: O(1)
+  def self.run(items)
+    for i in (0...items.size)
+      for p in (i+1...items.size)
+        for q in (p+1...items.size)
+          x = items[i] ** 2
+          y = items[p] ** 2
+          z = items[q] ** 2
+
+          return true if (x + y) == z
+        end
+      end
+    end
+
+    false
+  end
+end
+
+assert(Solution.run([3, 12, 5, 13]))
+assert(!Solution.run([1]))
+assert(!Solution.run([1, 2]))
+assert(!Solution.run([1, 2, 3]))
+assert(Solution.run([3, 4, 5]))
+puts "YAY!"