lytlang/test/fixtures/tiny.exs
2019-05-11 13:01:39 -05:00

19 lines
482 B
Elixir

defmodule Leetcode do
@doc """
LeetCode #1: two_sum
TODO: store a map of complements (differences) as iterating for O(n)
"""
@spec two_sum(list, int, int) :: any
def two_sum(_, _, i \\ 0)
def two_sum([], _, _), do: nil
def two_sum([cur | rest], target, current_index) do
case Enum.find_index(rest, fn x -> cur + x == target end) do
nil -> two_sum(rest, target, current_index + 1)
i -> [current_index, 1 + current_index + i]
end
end
end