题解 | #最小花费爬楼梯#
最小花费爬楼梯
https://www.nowcoder.com/practice/6fe0302a058a4e4a834ee44af88435c7
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param cost int整型一维数组
* @param costLen int cost数组长度
* @return int整型
*/
int Min(int a,int b)
{
if(a>b) return b;
else return a;
}
int minCostClimbingStairs(int* cost, int costLen ) {
if(costLen==1) return cost[0];
//累计花费的数组
int arr[costLen+1];
//开始选择下标1或0;所以在0台阶与1台阶不需要花费
arr[0]=0;
arr[1]=0;
for(int i=2;i<costLen+1;i++)
{
//计算需要迈一级台阶还是二级台阶需要的花费cost,再加上原来累计的花费arr,取较小值arr+cost
arr[i]=Min(arr[i-1]+cost[i-1],arr[i-2]+cost[i-2]);
}
return arr[costLen];
}
