Leetcode 746. Min Cost Climbing Stairs

 

思路:动态规划。

 1 class Solution {
 2     //不能对cost数组进行写操作,因为JAVA中参数是引用
 3     public int minCostClimbingStairs(int[] cost) {
 4         int cost_0 = cost[0], cost_1 = cost[1];
 5         for(int i = 2; i < cost.length; i++) {
 6             int cost_2 = Math.min(cost_0, cost_1) + cost[i];
 7             cost_0 = cost_1;
 8             cost_1 = cost_2;
 9         }
10         return Math.min(cost_0, cost_1);
11     }
12 }

Next challenges: Paint Fence Coin Change Maximum Sum of 3 Non-Overlapping Subarrays

posted @ 2018-01-22 18:30  Deribs4  阅读(289)  评论(0编辑  收藏  举报