freewebwithme
Two sum from leetcode
I see two solution from others
- using
Enummodule
@spec two_sum(nums:: [integer], target:: integer) :: [integer]
def two_sum(nums, target) do
Enum.reduce_while(nums, {%{}, 0}, fn n, {map, i} ->
complement = Map.get(map, target - n)
if complement do
{:halt, [i, complement]}
else
{:cont, {Map.put(map, n, i), i + 1}}
end
end)
end
- Using recursion
@spec two_sum(nums :: [integer], target :: integer) :: [integer]
def two_sum(nums, target) do
helper(Enum.with_index(nums), %{}, target)
end
defp helper([{value, index} | _t], map, _target) when is_map_key(map, value), do: [map[value], index]
defp helper([{value, index} | t], map, target), do: helper(t, Map.put(map, target - value, index), target)
What is difference in terms of efficiency?(time and space complexity)?
First solution is easy to understand for me but second solution using recursion is hard to understand.
Most Liked Responses
Eiji
In a second example this part:
is adding you one extra loop over whole nums list. You can pass i as a fourth argument to helper/3 function and work with it similarly to how you do that in an Enum.reduce_while/3-based example.
1
Popular in Challenges
Note: This topic is to talk about Day 12 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can joi...
New
Note by the Moderators: This topic is to talk about Day 5 of the Advent of Code.
For general discussion about the Advent of Code 2018 an...
New
Note: This topic is to talk about Day 7 of the Advent of Code 2019 .
There is a private leaderboard for elixirforum members. You can joi...
New
Here is my solution:
https://github.com/bjorng/advent-of-code-2021/blob/77bdef7a51b428b58ffb4ab76f540901e636f841/day12/lib/day12.ex
New
I have 2 arrays: a1 can be any combination of value or nil like that
a1 = [1,nil,3]
and array 2 the same
a2 = [4,2, nil]
How do I com...
New
This was way too easy after the last few days. Simple map, filter and count.
https://github.com/shritesh/advent/blob/main/2023/06.livemd
New
New
I spent 3 hours struggling in part 2, until I noticed a very basic mistake :joy:
Here’s my code:
https://github.com/Aetherus/advent-of-...
New
Hi, there :wave:
Today, I felt it was way more challenging! I went through part2 thanks to Agent based memoization (without memoization ...
New
Don’t know why the regex ~r/[\W && [^\.]]/x does not work in Elixir. It works pretty well in Ruby.
Anyway, here is my solution: ...
New
Other popular topics
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help.
Where are they similar?
Where do they differ the m...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition)
It’s been a while since we first asked this, I...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #podcasts
- #code-sync
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








