day 7: 70-climbing-stairs

This commit is contained in:
Kaushik Narayan R 2023-10-02 10:41:19 -07:00
parent 3f9b5b69e5
commit 81bd6f46c3
3 changed files with 42 additions and 0 deletions

View 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;
}

View 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;
}

View 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);
};