Cong_Go_De_Su
Senior Member
Daily hôm nay dễ thế nhỉ, bọn leetcode này rating đề vớ vẩn thật
Mã:
defmodule Solution do
def trap([]), do: 0
def trap([_]), do: 0
def trap([_, _]), do: 0
def trap([left, mid, right]), do: max(0, min(left, right) - mid)
def trap(heights) do
heights
|> Enum.reduce({[], 0}, fn mid, {columns, max_left} ->
{[[max_left, mid] | columns], max(mid, max_left)}
end)
|> elem(0)
|> Enum.reduce({0, 0}, fn [max_left, mid], {water, max_right} ->
{water + trap([max_left, mid, max_right]), max(mid, max_right)}
end)
|> elem(0)
end
end
Sửa lần cuối:
what the.....? Convention?
Làm thì làm ra được, mà tính không được