1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-16 16:36:56 +02:00
LeetCode/rb/min-cost-climbing-stairs.rb
Matej Focko 2351dfd0ee
chore: unwrap one layer
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-12-12 14:36:00 +01:00

28 lines
654 B
Ruby

# @param {Integer[]} cost
# @return {Integer}
def min_cost_climbing_stairs(cost)
def get(cost, k)
if k < 0 || k >= cost.size then
return 0
end
return cost[k]
end
i = cost.size - 1
while i >= 0 do
cost[i] += [get(cost, i + 1), get(cost, i + 2)].min
i -= 1
end
return cost.take(2).min
end
RSpec.describe "min_cost_climbing_stairs" do
it "of [10,15,20] is 15" do
expect(min_cost_climbing_stairs([10,15,20])).to eq(15)
end
it "of [1,100,1,1,1,100,1,1,100,1] is 6" do
expect(min_cost_climbing_stairs([1,100,1,1,1,100,1,1,100,1])).to eq(6)
end
end