Compare commits

...

3 Commits

6 changed files with 118 additions and 43 deletions

View File

@@ -5,6 +5,7 @@ set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(GTEST_VERSION 1.17.0) set(GTEST_VERSION 1.17.0)
enable_testing()
include_directories(include) include_directories(include)

14
include/solution/746.h Normal file
View File

@@ -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

80
mkfile.py Normal file → Executable file
View File

@@ -1,41 +1,41 @@
#!/usr/bin/python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sys import sys
import datetime import datetime
if len(sys.argv) != 2: if len(sys.argv) != 2:
print("Usage: mkfile.py <filename>") print("Usage: mkfile.py <filename>")
sys.exit(1) sys.exit(1)
filename = sys.argv[1] filename = sys.argv[1]
# create header file # create header file
with open(f"./include/solution/{filename.lower()}.h", "w") as f: with open(f"./include/solution/{filename.lower()}.h", "w") as f:
f.write("// This file is generated by mkfile.py\n") f.write("// This file is generated by mkfile.py\n")
f.write(f"// Date: {datetime.datetime.now().strftime('%Y-%m-%d')}\n\n") f.write(f"// Date: {datetime.datetime.now().strftime('%Y-%m-%d')}\n\n")
f.write(f"#ifndef INC_{filename.upper()}_H\n") f.write(f"#ifndef INC_{filename.upper()}_H\n")
f.write(f"#define INC_{filename.upper()}_H\n") f.write(f"#define INC_{filename.upper()}_H\n")
f.write("#ifdef __cplusplus\n") f.write("#ifdef __cplusplus\n")
f.write("extern \"C\"\n") f.write("extern \"C\"\n")
f.write("{\n") f.write("{\n")
f.write("#endif\n\n") f.write("#endif\n\n")
f.write("#ifdef __cplusplus\n") f.write("#ifdef __cplusplus\n")
f.write("}\n") f.write("}\n")
f.write("#endif\n") f.write("#endif\n")
f.write(f"#endif // INC_{filename.upper()}_H\n") f.write(f"#endif // INC_{filename.upper()}_H\n")
f.close() f.close()
# create source file # create source file
with open(f"./src/{filename.lower()}.c", "w") as f: with open(f"./src/{filename.lower()}.c", "w") as f:
f.write("// This file is generated by mkfile.py\n") f.write("// This file is generated by mkfile.py\n")
f.write(f"// Date: {datetime.datetime.now().strftime('%Y-%m-%d')}\n\n") f.write(f"// Date: {datetime.datetime.now().strftime('%Y-%m-%d')}\n\n")
f.write(f'#include <solution/{filename.lower()}.h>\n') f.write(f'#include <solution/{filename.lower()}.h>\n')
f.close() f.close()
# create test file # create test file
with open(f"./tests/test_{filename.lower()}.cpp", "w") as f: with open(f"./tests/test_{filename.lower()}.cpp", "w") as f:
f.write("// This file is generated by mkfile.py\n") f.write("// This file is generated by mkfile.py\n")
f.write(f"// Date: {datetime.datetime.now().strftime('%Y-%m-%d')}\n\n") f.write(f"// Date: {datetime.datetime.now().strftime('%Y-%m-%d')}\n\n")
f.write(f'#include <solution/{filename.lower()}.h>\n') f.write(f'#include <solution/{filename.lower()}.h>\n')
f.write("#include <gtest/gtest.h>\n") f.write("#include <gtest/gtest.h>\n")
f.close() f.close()

15
src/746.c Normal file
View File

@@ -0,0 +1,15 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <solution/746.h>
#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];
}

View File

@@ -9,6 +9,4 @@ foreach (TEST_SRC ${TEST_SOURCES})
target_link_libraries(${TEST_NAME} PUBLIC ${TEST_LIBS}) target_link_libraries(${TEST_NAME} PUBLIC ${TEST_LIBS})
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME}) add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
endforeach () endforeach ()
enable_testing()

47
tests/test_746.cpp Normal file
View File

@@ -0,0 +1,47 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <gtest/gtest.h>
#include <solution/746.h>
#include <algorithm>
#include <vector>
// Tests for minCostClimbingStairs (LeetCode 746)
class MinCostClimbingStairsTest : public ::testing::Test
{
protected:
void AssertMinCost(const std::vector<int> &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);
}