Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions 70_climbing_stairs/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 70. Climbing Stairs

https://leetcode.com/problems/climbing-stairs/

## Comments

### step1

* dp や再帰で解けるだろう
* いくつか試すと、直近 2 つだけ持っていればよい

### step2

* フィボナッチっぽく実装するならこのあたりとか
* for のループ回数とか境界条件をちゃんと確認する必要はある

### step3

* 簡単な問題なので省略。ただし LeetCode に色々別解はある。
Copy link
Copy Markdown

@nodchip nodchip Oct 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ぱっと思いつくのは、

  • 深さ優先探索
  • キャッシュ付き深さ優先探索
  • 動的計画法
  • 行列の n 乗
  • フィボナッチ数列の一般項

あたりでした。自分が面接官なら、動的計画法までを期待値とすると思います。すらすら解けたら、行列の n 乗の方法を説明したうえで、 m * m 正方行列の乗算のコードを書けるかどうか試すと思います。

20 changes: 20 additions & 0 deletions 70_climbing_stairs/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
int climbStairs(int n) {
if (n <= 1) {
return n;
}
int ways;
// Starting from ways(0) = 0
int two_steps_before = 0;
// ways(1) = 1
int one_step_before = 1;
// Run n times.
for (int i = 0; i < n; ++i) {
ways = two_steps_before + one_step_before;
two_steps_before = one_step_before;
one_step_before = ways;
}
return ways;
}
};
16 changes: 16 additions & 0 deletions 70_climbing_stairs/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int climbStairs(int n) {
if (n <= 1) {
return n;
}
int first = 1;
int second = 1;
for (int i = 2; i <= n; ++i) {
int third = first + second;
first = second;
second = third;
}
return second;
}
};
Empty file added 70_climbing_stairs/step3.cpp
Empty file.