From 14dbd04d8f9a185487a89ddcf398d8cd182e4804 Mon Sep 17 00:00:00 2001 From: rononz <42426951+rononz@users.noreply.github.com> Date: Tue, 28 Dec 2021 23:46:13 +0800 Subject: [PATCH] Create LeetCode_746_142.java --- Week_04/id_142/LeetCode_746_142.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Week_04/id_142/LeetCode_746_142.java diff --git a/Week_04/id_142/LeetCode_746_142.java b/Week_04/id_142/LeetCode_746_142.java new file mode 100644 index 00000000..e2920983 --- /dev/null +++ b/Week_04/id_142/LeetCode_746_142.java @@ -0,0 +1,18 @@ +import java.lang.Math; + +class Solution { + public int minCostClimbingStairs(int[] cost) { + // states[i] = cost[i] + min(states[i - 1], states[i - 2]) + int len = cost.length; + int[] states = new int[len]; + + states[0] = cost[0]; + states[1] = cost[1]; + + for (int i = 2; i < len; i++) { + states[i] = cost[i] + Math.min(states[i - 1], states[i - 2]); + } + + return Math.min(states[len - 1], states[len - 2]); + } +}