[LeetCode] Climbing Stairs

Question:

You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Note: Given n will be a positive integer.

Answer:

Basically it's a fibonacci.
The problem seems to be a dynamic programming one.
Here are the steps to get the solution incrementally.

Base cases:

  • if n <= 0, then the number of ways should be zero.
  • if n == 1, then there is only way to climb the stair.
  • if n == 2, then there are two ways to climb the stairs. One solution is one step by another; the other one is two steps at one time.

The key intuition to solve the problem is that given a number of stairs n, if we know the number ways to get to the points [n-1] and [n-2] respectively, denoted as n1 and n2 , then the total ways to get to the point [n] is n1 + n2. Because from the [n-1] point, we can take one single step to reach [n]. And from the [n-2] point, we could take two steps to get there. There is NO overlapping between these two solution sets, because we differ in the final step.

Now given the above intuition, one can construct an array where each node stores the solution for each number n. Or if we look at it closer, it is clear that this is basically a fibonacci number, with the starting numbers as 1 and 2, instead of 1 and 1.

The implementation in Java as follows:

public int climbStairs(int n) {
    // base cases
    if(n <= 0) return 0;
    if(n == 1) return 1;
    if(n == 2) return 2;
    
    int one_step_before = 2;
    int two_steps_before = 1;
    int all_ways = 0;
    
    for(int i=2; i<n; i++){
        all_ways = one_step_before + two_steps_before;
        two_steps_before = one_step_before;
        one_step_before = all_ways;
    }
    return all_ways;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子糊渊,更是在濱河造成了極大的恐慌,老刑警劉巖樟氢,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件豺瘤,死亡現(xiàn)場離奇詭異辆床,居然都是意外死亡愿卸,警方通過查閱死者的電腦和手機(jī)灵临,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來趴荸,“玉大人儒溉,你說我怎么就攤上這事》⒍郏” “怎么了顿涣?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵波闹,是天一觀的道長。 經(jīng)常有香客問我园骆,道長舔痪,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任锌唾,我火速辦了婚禮,結(jié)果婚禮上夺英,老公的妹妹穿的比我還像新娘晌涕。我一直安慰自己,他們只是感情好痛悯,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布余黎。 她就那樣靜靜地躺著,像睡著了一般载萌。 火紅的嫁衣襯著肌膚如雪惧财。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天扭仁,我揣著相機(jī)與錄音垮衷,去河邊找鬼。 笑死乖坠,一個胖子當(dāng)著我的面吹牛搀突,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播熊泵,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼仰迁,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了顽分?” 一聲冷哼從身側(cè)響起徐许,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎卒蘸,沒想到半個月后雌隅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡悬秉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年澄步,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片和泌。...
    茶點(diǎn)故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡村缸,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出武氓,到底是詐尸還是另有隱情梯皿,我是刑警寧澤仇箱,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站东羹,受9級特大地震影響剂桥,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜属提,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一权逗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧冤议,春花似錦斟薇、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蕊温,卻和暖如春袱箱,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背义矛。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工发笔, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人症革。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓筐咧,卻偏偏與公主長得像,于是被迫代替她去往敵國和親噪矛。 傳聞我的和親對象是個殘疾皇子量蕊,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內(nèi)容