From fbcb84921020c1b3eae8a5262b0d2d2decbbf6eb Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Fri, 10 Nov 2023 12:06:49 -0700 Subject: [PATCH] day 37: 746-min-cost-climbing-stairs --- 746-min-cost-climbing-stairs/driver.cpp | 10 ++++++++++ 746-min-cost-climbing-stairs/soln.cpp | 16 ++++++++++++++++ 746-min-cost-climbing-stairs/soln.hpp | 8 ++++++++ 3 files changed, 34 insertions(+) create mode 100644 746-min-cost-climbing-stairs/driver.cpp create mode 100644 746-min-cost-climbing-stairs/soln.cpp create mode 100644 746-min-cost-climbing-stairs/soln.hpp diff --git a/746-min-cost-climbing-stairs/driver.cpp b/746-min-cost-climbing-stairs/driver.cpp new file mode 100644 index 0000000..d28d3f2 --- /dev/null +++ b/746-min-cost-climbing-stairs/driver.cpp @@ -0,0 +1,10 @@ +#include "soln.hpp" + +int main() +{ + vector costs{1, 100, 1, 1, 1, 100, 1, 1, 100, 1}; + int answer = 6; + Solution soln; + cout << "Found min cost correctly? " << soln.test(costs, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/746-min-cost-climbing-stairs/soln.cpp b/746-min-cost-climbing-stairs/soln.cpp new file mode 100644 index 0000000..6da0366 --- /dev/null +++ b/746-min-cost-climbing-stairs/soln.cpp @@ -0,0 +1,16 @@ +#include "soln.hpp" + +int Solution::minCostClimbingStairs(vector &cost) +{ + int n = cost.size(); + for (int i = 2; i < n; i++) + { + cost[i] += min(cost[i - 1], cost[i - 2]); + } + return min(cost[n - 1], cost[n - 2]); +} + +bool Solution::test(vector &cost, int answer) +{ + return minCostClimbingStairs(cost) == answer; +} diff --git a/746-min-cost-climbing-stairs/soln.hpp b/746-min-cost-climbing-stairs/soln.hpp new file mode 100644 index 0000000..c3b7759 --- /dev/null +++ b/746-min-cost-climbing-stairs/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + int minCostClimbingStairs(vector &cost); + bool test(vector &cost, int answer); +}; \ No newline at end of file