diff --git a/dynamic-programming/src/leetcode/editor/cn/MinCostClimbingStairs.java b/dynamic-programming/src/leetcode/editor/cn/MinCostClimbingStairs.java new file mode 100644 index 0000000..d61f28f --- /dev/null +++ b/dynamic-programming/src/leetcode/editor/cn/MinCostClimbingStairs.java @@ -0,0 +1,65 @@ +//

给你一个整数数组 cost ,其中 cost[i] 是从楼梯第 i 个台阶向上爬需要支付的费用。一旦你支付此费用,即可选择向上爬一个或者两个台阶。

+// +//

你可以选择从下标为 0 或下标为 1 的台阶开始爬楼梯。

+// +//

请你计算并返回达到楼梯顶部的最低花费。

+// +//

 

+// +//

示例 1:

+// +//
+//输入:cost = [10,15,20]
+//输出:15
+//解释:你将从下标为 1 的台阶开始。
+//- 支付 15 ,向上爬两个台阶,到达楼梯顶部。
+//总花费为 15 。
+//
+// +//

示例 2:

+// +//
+//输入:cost = [1,100,1,1,1,100,1,1,100,1]
+//输出:6
+//解释:你将从下标为 0 的台阶开始。
+//- 支付 1 ,向上爬两个台阶,到达下标为 2 的台阶。
+//- 支付 1 ,向上爬两个台阶,到达下标为 4 的台阶。
+//- 支付 1 ,向上爬两个台阶,到达下标为 6 的台阶。
+//- 支付 1 ,向上爬一个台阶,到达下标为 7 的台阶。
+//- 支付 1 ,向上爬两个台阶,到达下标为 9 的台阶。
+//- 支付 1 ,向上爬一个台阶,到达楼梯顶部。
+//总花费为 6 。
+//
+// +//

 

+// +//

提示:

+// +// +// +//
Related Topics
  • 数组
  • 动态规划

  • 👍 1330
  • 👎 0
  • +package leetcode.editor.cn; + +// 746:使用最小花费爬楼梯 +public class MinCostClimbingStairs { + public static void main(String[] args) { + Solution solution = new MinCostClimbingStairs().new Solution(); + // TO TEST + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int minCostClimbingStairs(int[] cost) { + int[] sum = new int[cost.length + 1]; + for (int i = 2; i < cost.length + 1; i++) { + sum[i] = Math.min(sum[i - 1] + cost[i - 1], sum[i - 2] + cost[i - 2]); + } + return sum[cost.length]; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/dynamic-programming/src/leetcode/editor/cn/doc/content/MinCostClimbingStairs.md b/dynamic-programming/src/leetcode/editor/cn/doc/content/MinCostClimbingStairs.md new file mode 100644 index 0000000..db0796f --- /dev/null +++ b/dynamic-programming/src/leetcode/editor/cn/doc/content/MinCostClimbingStairs.md @@ -0,0 +1,43 @@ +

    给你一个整数数组 cost ,其中 cost[i] 是从楼梯第 i 个台阶向上爬需要支付的费用。一旦你支付此费用,即可选择向上爬一个或者两个台阶。

    + +

    你可以选择从下标为 0 或下标为 1 的台阶开始爬楼梯。

    + +

    请你计算并返回达到楼梯顶部的最低花费。

    + +

     

    + +

    示例 1:

    + +
    +输入:cost = [10,15,20]
    +输出:15
    +解释:你将从下标为 1 的台阶开始。
    +- 支付 15 ,向上爬两个台阶,到达楼梯顶部。
    +总花费为 15 。
    +
    + +

    示例 2:

    + +
    +输入:cost = [1,100,1,1,1,100,1,1,100,1]
    +输出:6
    +解释:你将从下标为 0 的台阶开始。
    +- 支付 1 ,向上爬两个台阶,到达下标为 2 的台阶。
    +- 支付 1 ,向上爬两个台阶,到达下标为 4 的台阶。
    +- 支付 1 ,向上爬两个台阶,到达下标为 6 的台阶。
    +- 支付 1 ,向上爬一个台阶,到达下标为 7 的台阶。
    +- 支付 1 ,向上爬两个台阶,到达下标为 9 的台阶。
    +- 支付 1 ,向上爬一个台阶,到达楼梯顶部。
    +总花费为 6 。
    +
    + +

     

    + +

    提示:

    + + + +
    Related Topics
  • 数组
  • 动态规划

  • 👍 1330
  • 👎 0
  • \ No newline at end of file