leetcode: solve 'container with most water' problem
This commit is contained in:
parent
1febd49b82
commit
ede7315d4e
7 changed files with 115 additions and 0 deletions
|
@ -0,0 +1,4 @@
|
|||
# Used by "mix format"
|
||||
[
|
||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||
]
|
26
leetcode/0011-container-with-most-water/elixir/.gitignore
vendored
Normal file
26
leetcode/0011-container-with-most-water/elixir/.gitignore
vendored
Normal file
|
@ -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/
|
|
@ -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
|
28
leetcode/0011-container-with-most-water/elixir/mix.exs
Normal file
28
leetcode/0011-container-with-most-water/elixir/mix.exs
Normal file
|
@ -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
|
|
@ -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
|
|
@ -0,0 +1 @@
|
|||
ExUnit.start()
|
11
leetcode/0011-container-with-most-water/readme.md
Normal file
11
leetcode/0011-container-with-most-water/readme.md
Normal file
|
@ -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.
|
Loading…
Reference in a new issue