From 36c5339b1f91b43d3c57f9d9d926a7adbcdc32d5 Mon Sep 17 00:00:00 2001 From: Jeffrey Hsu Date: Fri, 5 Dec 2025 21:05:14 +0800 Subject: [PATCH] 746 --- include/solution/746.h | 14 +++++++++++++ src/746.c | 15 ++++++++++++++ tests/test_746.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 include/solution/746.h create mode 100644 src/746.c create mode 100644 tests/test_746.cpp diff --git a/include/solution/746.h b/include/solution/746.h new file mode 100644 index 0000000..c3924a2 --- /dev/null +++ b/include/solution/746.h @@ -0,0 +1,14 @@ +// This file is generated by mkfile.py +// Date: 2025-12-05 + +#ifndef INC_746_H +#define INC_746_H +#ifdef __cplusplus +extern "C" +{ +#endif + int minCostClimbingStairs(int *cost, int costSize); +#ifdef __cplusplus +} +#endif +#endif // INC_746_H diff --git a/src/746.c b/src/746.c new file mode 100644 index 0000000..eb63719 --- /dev/null +++ b/src/746.c @@ -0,0 +1,15 @@ +// This file is generated by mkfile.py +// Date: 2025-12-05 + +#include + +#define min(x, y) ((x) < (y) ? (x) : (y)) +int minCostClimbingStairs(int *cost, int costSize) +{ + int dp[costSize + 1]; + dp[0] = dp[1] = 0; + for (int i = 2; i <= costSize; i++) + dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); + + return dp[costSize]; +} \ No newline at end of file diff --git a/tests/test_746.cpp b/tests/test_746.cpp new file mode 100644 index 0000000..68b1672 --- /dev/null +++ b/tests/test_746.cpp @@ -0,0 +1,47 @@ +// This file is generated by mkfile.py +// Date: 2025-12-05 + +#include +#include + +#include +#include + +// Tests for minCostClimbingStairs (LeetCode 746) +class MinCostClimbingStairsTest : public ::testing::Test +{ + protected: + void AssertMinCost(const std::vector &input, int expected) + { + int *cost = new int[input.size()]; + std::copy(input.begin(), input.end(), cost); + + int result = minCostClimbingStairs(cost, (int)input.size()); + ASSERT_EQ(result, expected); + + delete[] cost; + } +}; + +TEST_F(MinCostClimbingStairsTest, Example1) +{ + // cost = [10,15,20] -> expected 15 + AssertMinCost({10, 15, 20}, 15); +} + +TEST_F(MinCostClimbingStairsTest, Example2) +{ + // cost = [1,100,1,1,1,100,1,1,100,1] -> expected 6 + AssertMinCost({1, 100, 1, 1, 1, 100, 1, 1, 100, 1}, 6); +} + +TEST_F(MinCostClimbingStairsTest, TwoSteps) +{ + // Two steps: choose cheaper one + AssertMinCost({5, 3}, 3); +} + +TEST_F(MinCostClimbingStairsTest, ZeroCost) +{ + AssertMinCost({0, 0, 0}, 0); +}