1039
This commit is contained in:
14
include/solution/1039.h
Normal file
14
include/solution/1039.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
// This file is generated by mkfile.py
|
||||||
|
// Date: 2025-09-29
|
||||||
|
|
||||||
|
#ifndef INC_1039_H
|
||||||
|
#define INC_1039_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int minScoreTriangulation(int *values, int valuesSize);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_1039_H
|
||||||
30
src/1039.c
Normal file
30
src/1039.c
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
// This file is generated by mkfile.py
|
||||||
|
// Date: 2025-09-29
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
#include <solution/1039.h>
|
||||||
|
|
||||||
|
#define min(x, y) (x) < (y) ? (x) : (y)
|
||||||
|
|
||||||
|
int minScoreTriangulation(int *values, int valuesSize)
|
||||||
|
{
|
||||||
|
int dp[valuesSize][valuesSize];
|
||||||
|
for (int i = 0; i < valuesSize; i++)
|
||||||
|
for (int j = 0; j < valuesSize; j++)
|
||||||
|
dp[i][j] = 0;
|
||||||
|
|
||||||
|
for (int len = 2; len < valuesSize; len++)
|
||||||
|
{
|
||||||
|
for (int i = 0; i + len < valuesSize; i++)
|
||||||
|
{
|
||||||
|
int j = i + len;
|
||||||
|
dp[i][j] = INT_MAX;
|
||||||
|
|
||||||
|
for (int k = i + 1; k < j; k++)
|
||||||
|
{
|
||||||
|
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + values[i] * values[j] * values[k]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[0][valuesSize - 1];
|
||||||
|
}
|
||||||
45
tests/test_1039.cpp
Normal file
45
tests/test_1039.cpp
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
// This file is generated by mkfile.py
|
||||||
|
// Date: 2025-09-29
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <solution/1039.h>
|
||||||
|
|
||||||
|
|
||||||
|
class MinScoreTriangulationTest : public ::testing::Test
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
void AssertMinScoreTriangulation(std::vector<int> input, int expected)
|
||||||
|
{
|
||||||
|
int *values = new int[input.size()];
|
||||||
|
std::copy(input.begin(), input.end(), values);
|
||||||
|
|
||||||
|
int result = minScoreTriangulation(values, input.size());
|
||||||
|
ASSERT_EQ(result, expected);
|
||||||
|
|
||||||
|
delete[] values;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 示例 1
|
||||||
|
TEST_F(MinScoreTriangulationTest, Example1)
|
||||||
|
{
|
||||||
|
// 输入: values = [1,2,3]
|
||||||
|
// 输出: 6
|
||||||
|
AssertMinScoreTriangulation({1, 2, 3}, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例 2
|
||||||
|
TEST_F(MinScoreTriangulationTest, Example2)
|
||||||
|
{
|
||||||
|
// 输入: values = [3,7,4,5]
|
||||||
|
// 输出: 144
|
||||||
|
AssertMinScoreTriangulation({3, 7, 4, 5}, 144);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例 3
|
||||||
|
TEST_F(MinScoreTriangulationTest, Example3)
|
||||||
|
{
|
||||||
|
// 输入: values = [1,3,1,4,1,5]
|
||||||
|
// 输出: 13
|
||||||
|
AssertMinScoreTriangulation({1, 3, 1, 4, 1, 5}, 13);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user