diff options
| author | mo khan <mo.khan@gmail.com> | 2020-08-18 16:45:20 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-08-18 16:45:20 -0600 |
| commit | c0e62e1a82f8ff29f938b63dd5e0c0ac7e726ffc (patch) | |
| tree | 84854cdebb14ade18b2014266c27519b35b5c079 | |
| parent | 0a3e55174308d0a82af75d7a3bd81648c784c4a2 (diff) | |
Find hacky solution with O(1) space with some assumptions about data
| -rw-r--r-- | 2020/08/18/main.rb | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/2020/08/18/main.rb b/2020/08/18/main.rb index efc2263..f5ffc9d 100644 --- a/2020/08/18/main.rb +++ b/2020/08/18/main.rb @@ -14,6 +14,20 @@ class Solution cache.values.find { |value| value > 0 } end + + # time: O(n) + # space: O(1) assumes non negative numbers + def self.run(items) + cache = Array.new(items.size, 2) + + for i in items + cache[i] -= 1 + end + + for i in (0...items.size) + return i if cache[i] % 2 != 0 + end + end end assert_equal 1, Solution.run([4, 3, 2, 4, 1, 3, 2]) |
