Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds a solution to two sum problem in Elixir #9

Merged
merged 1 commit into from Aug 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions leetcode/1 - twosum/two_sum.ex
@@ -0,0 +1,29 @@
# Time complexity: O(n^2) - quadrático, N*N, sendo N o tamanho do vetor nums

# Elixir não foi criado para resolver problemas de CPU-bound
# ou seja, caso você precise resolver problemas onde exija muito da CPU, elixir definitivamente
# não é a linguagem correta =(. Pode até existir um modo melhor de resolver esse problema utilizando elixir
# porém comparado com outras linguagens como (C++, Rust, Golang ou até Python) a complexidade de implementação
# fica extremamente alta.

# Acredito que possa haver um modo de resolver o problema de outra maneira
# Porém o meu conhecimento de elixir se limita apenas a essa solução XD

defmodule Solution do
def two_sum(nums, target) do
nums
|> Enum.with_index
|> get_positions(target)
end

defp get_positions([{number, current_index} | tail], target) do
case exists_sum?(tail, number, target) do
nil -> get_positions(tail, target)
new_index -> [current_index, new_index]
end
end

defp exists_sum?([{new_value, index} | _tail], current, target) when current + new_value == target, do: index
defp exists_sum?([_ | tail], current, target), do: exists_sum?(tail, current, target)
defp exists_sum?(_, _, _), do: nil
end