diff --git a/leetcode/0011-container-with-most-water/elixir/.formatter.exs b/leetcode/0011-container-with-most-water/elixir/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/leetcode/0011-container-with-most-water/elixir/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/leetcode/0011-container-with-most-water/elixir/.gitignore b/leetcode/0011-container-with-most-water/elixir/.gitignore new file mode 100644 index 0000000..dfe4f64 --- /dev/null +++ b/leetcode/0011-container-with-most-water/elixir/.gitignore @@ -0,0 +1,26 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +solution-*.tar + +# Temporary files, for example, from tests. +/tmp/ diff --git a/leetcode/0011-container-with-most-water/elixir/lib/solution.ex b/leetcode/0011-container-with-most-water/elixir/lib/solution.ex new file mode 100644 index 0000000..0d20f02 --- /dev/null +++ b/leetcode/0011-container-with-most-water/elixir/lib/solution.ex @@ -0,0 +1,27 @@ +defmodule Solution do + @spec max_area(height :: [integer]) :: integer + def max_area(height) do + iter(0, height, 0, 0) + end + + defp iter(prev, [head | tail], head_idx, max_vol) do + if prev >= head do + iter(prev, tail, head_idx + 1, max_vol) + else + max_vol = iter_inner(head, head_idx, tail, head_idx + 1, max_vol) + iter(head, tail, head_idx + 1, max_vol) + end + end + + defp iter(_, [], _, max_vol), do: max_vol + + defp iter_inner(a, a_idx, [head | tail], head_idx, max_vol) do + distance = head_idx - a_idx + min_height = min(a, head) + volume = min_height * distance + max_vol = max(max_vol, volume) + iter_inner(a, a_idx, tail, head_idx + 1, max_vol) + end + + defp iter_inner(_, _, [], _, max_vol), do: max_vol +end diff --git a/leetcode/0011-container-with-most-water/elixir/mix.exs b/leetcode/0011-container-with-most-water/elixir/mix.exs new file mode 100644 index 0000000..dc6667f --- /dev/null +++ b/leetcode/0011-container-with-most-water/elixir/mix.exs @@ -0,0 +1,28 @@ +defmodule Solution.MixProject do + use Mix.Project + + def project do + [ + app: :solution, + version: "0.1.0", + elixir: "~> 1.16", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/leetcode/0011-container-with-most-water/elixir/test/solution_test.exs b/leetcode/0011-container-with-most-water/elixir/test/solution_test.exs new file mode 100644 index 0000000..1db3a47 --- /dev/null +++ b/leetcode/0011-container-with-most-water/elixir/test/solution_test.exs @@ -0,0 +1,18 @@ +defmodule SolutionTest do + use ExUnit.Case + doctest Solution + + test "example 1" do + assert Solution.max_area([1, 8, 6, 2, 5, 4, 8, 3, 7]) == 49 + end + + test "example 2" do + assert Solution.max_area([1, 1]) == 1 + end + + test "large input" do + length = Integer.pow(10, 7) + input = Enum.map(1..length, fn _ -> :rand.uniform(10000) end) + Solution.max_area(input) + end +end diff --git a/leetcode/0011-container-with-most-water/elixir/test/test_helper.exs b/leetcode/0011-container-with-most-water/elixir/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/leetcode/0011-container-with-most-water/elixir/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start() diff --git a/leetcode/0011-container-with-most-water/readme.md b/leetcode/0011-container-with-most-water/readme.md new file mode 100644 index 0000000..fab75ec --- /dev/null +++ b/leetcode/0011-container-with-most-water/readme.md @@ -0,0 +1,11 @@ +# 11. Container With Most Water + +[Leetcode](https://leetcode.com/problems/container-with-most-water/) + +You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). + +Find two lines that together with the x-axis form a container, such that the container contains the most water. + +Return the maximum amount of water a container can store. + +Notice that you may not slant the container.