mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 05:24:07 +00:00
day 7: 70-climbing-stairs
This commit is contained in:
parent
3f9b5b69e5
commit
81bd6f46c3
10
70-climbing-stairs/driver.cpp
Normal file
10
70-climbing-stairs/driver.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
int n = 9;
|
||||
int answer = 55;
|
||||
Solution soln;
|
||||
cout << "Found steps correctly? " << soln.test(n, answer) << endl;
|
||||
return 0;
|
||||
}
|
||||
25
70-climbing-stairs/soln.cpp
Normal file
25
70-climbing-stairs/soln.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int Solution::climbStairs(int n)
|
||||
{
|
||||
if (n < 4)
|
||||
return n;
|
||||
|
||||
long minus_one = 3;
|
||||
long minus_two = 2;
|
||||
long steps = 0;
|
||||
|
||||
for (int i = 3; i < n; i++)
|
||||
{
|
||||
// climb one
|
||||
steps = minus_one + minus_two;
|
||||
minus_two = minus_one;
|
||||
minus_one = steps;
|
||||
}
|
||||
|
||||
return steps;
|
||||
}
|
||||
|
||||
bool Solution::test(int n, long answer) {
|
||||
return climbStairs(n) == answer;
|
||||
}
|
||||
7
70-climbing-stairs/soln.hpp
Normal file
7
70-climbing-stairs/soln.hpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
class Solution {
|
||||
public:
|
||||
int climbStairs(int n);
|
||||
bool test(int n, long answer);
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user