diff options
| author | mo khan <mo.khan@gmail.com> | 2020-08-25 13:06:20 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-08-25 13:06:20 -0600 |
| commit | 272b640bf9689981c2e1bef3646be51690acfe94 (patch) | |
| tree | 4f60a0df8323654ed2dfc73987b5d9acf79fe6fc /2020 | |
| parent | b4e0004f9fd9c08dd0039f8bcbda5c7713751fea (diff) | |
Solve using cubic time
Diffstat (limited to '2020')
| -rw-r--r-- | 2020/08/24/main.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/2020/08/24/main.rb b/2020/08/24/main.rb new file mode 100644 index 0000000..102cf91 --- /dev/null +++ b/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!" |
