diff --git a/70-climbing-stairs/driver.cpp b/70-climbing-stairs/driver.cpp new file mode 100644 index 0000000..9c47358 --- /dev/null +++ b/70-climbing-stairs/driver.cpp @@ -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; +} \ No newline at end of file diff --git a/70-climbing-stairs/soln.cpp b/70-climbing-stairs/soln.cpp new file mode 100644 index 0000000..8833a2f --- /dev/null +++ b/70-climbing-stairs/soln.cpp @@ -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; +} \ No newline at end of file diff --git a/70-climbing-stairs/soln.hpp b/70-climbing-stairs/soln.hpp new file mode 100644 index 0000000..bc92a5a --- /dev/null +++ b/70-climbing-stairs/soln.hpp @@ -0,0 +1,7 @@ +#include +using namespace std; +class Solution { + public: + int climbStairs(int n); + bool test(int n, long answer); +}; \ No newline at end of file