码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 70 Climbing Stairs

时间:2018-04-22 21:47:15      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:复杂度   AC   dex   minimum   cli   The   from   target   rom   

dp[i] means the minimum cost to reach i-th (index start from 0) stair.

DP formula:

dp[i] = min(dp[i-1], dp[i-2])+cost[i]     i>=2

base cases: dp[0]=cost[0]; dp[1]=cost[1];

target: min(dp[n-1], dp[n-2])

 

优化: 由于 dp[i] 只和前两个状态有关,因此只需两个变量来保存即可,空间复杂度从 O(n) 降为 O(1)。

 

Leetcode 70 Climbing Stairs

标签:复杂度   AC   dex   minimum   cli   The   from   target   rom   

原文地址:https://www.cnblogs.com/hankunyan/p/8908996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!