Compare commits

..

16 Commits

Author SHA1 Message Date
6e2206cabe Update Ubuntu mirror in test.yaml for dependency installation
Some checks failed
Gitea CTest Workflow / test (push) Failing after 1m34s
2026-02-05 00:07:55 +08:00
89a41445ef Update minimum CMake version to 3.25 in CMakeLists.txt
Some checks failed
Gitea CTest Workflow / test (push) Has been cancelled
2026-02-05 00:05:54 +08:00
cdb5602c47 Add step to install dependencies in test.yaml
Some checks failed
Gitea CTest Workflow / test (push) Failing after 6m10s
2026-02-04 23:58:46 +08:00
3559f83475 Replace checkout action with direct git clone command in test.yaml
Some checks failed
Gitea CTest Workflow / test (push) Failing after 4s
2026-02-04 23:57:04 +08:00
6f30721f1b Update checkout action URL in test.yaml to use version 6
Some checks failed
Gitea CTest Workflow / test (push) Failing after 42s
2026-02-04 23:55:46 +08:00
9a8260a3b6 Remove token from test.yaml
Some checks failed
Gitea CTest Workflow / test (push) Failing after 32s
2026-02-04 23:53:07 +08:00
fc2fd50bac Update test.yaml to specify server URL and repository for Git operations
Some checks failed
Gitea CTest Workflow / test (push) Failing after 4s
2026-02-04 23:52:24 +08:00
aa3f652a8f Update repository URL in test.yaml
Some checks failed
Gitea CTest Workflow / test (push) Failing after 4s
2026-02-04 23:47:52 +08:00
b6a1850f48 Update checkout action URL in test.yaml to use the mirror repository
Some checks failed
Gitea CTest Workflow / test (push) Failing after 44s
2026-02-04 23:06:18 +08:00
737c8e17dd Add Gitea CTest workflow configuration
Some checks failed
Gitea CTest Workflow / test (push) Has been cancelled
2026-02-04 22:59:05 +08:00
2cf5db11bd Remove debug compile options from CMakeLists.txt 2025-12-06 00:05:53 +08:00
7ae0aeb744 Implement dynamic programming solution for tribonacci and fix index calculation in sequence matching 2025-12-06 00:03:28 +08:00
cf633352b5 1137 2025-12-05 23:10:18 +08:00
45162bb934 修复CMakeLists.txt中的MSVC支持检查,并调整代码格式 2025-12-05 23:10:08 +08:00
dc222231e6 3397 2025-12-05 22:41:20 +08:00
8fadb725e1 1039 2025-12-05 22:41:12 +08:00
12 changed files with 294 additions and 8 deletions

View File

@@ -0,0 +1,36 @@
name: Gitea CTest Workflow
on:
push:
branches: [ "main", "master" ]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
run: |
git clone https://cantyonion.site/git/cantyonion/leetcode.git .
- name: Install Dependencies
run: |
sudo sed -i 's@//.*archive.ubuntu.com@//mirrors.ustc.edu.cn@g' /etc/apt/sources.list.d/ubuntu.sources
sudo apt-get update
sudo apt-get install -y build-essential cmake
- name: Configure CMake
# -B build 创建构建目录,-S . 指定源代码在当前目录
run: cmake -B build -S . -DCMAKE_BUILD_TYPE=Release
- name: Build with CMake
# 使用 --parallel 充分利用 Runner 核心数
run: cmake --build build --parallel $(nproc)
- name: Run CTest via CMake
# 在 build 目录下运行 ctest
# --output-on-failure 可以在测试失败时直接看到 log
run: |
cd build
ctest --output-on-failure --parallel $(nproc)

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.30)
cmake_minimum_required(VERSION 3.25)
project(leetcode C CXX)
set(CMAKE_C_STANDARD 11)
@@ -7,6 +7,10 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(GTEST_VERSION 1.17.0)
enable_testing()
if (MSVC)
message(FATAL_ERROR "MSVC is not supported. Please use GCC or Clang.")
endif ()
include_directories(include)
include(FetchContent)
@@ -14,9 +18,6 @@ include(FetchContent)
add_compile_options(
-Wall
-Wextra
-O0
-g3
-fno-omit-frame-pointer
)
if (UNIX)

14
include/solution/1039.h Normal file
View 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

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

@@ -0,0 +1,14 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#ifndef INC_1137_H
#define INC_1137_H
#ifdef __cplusplus
extern "C"
{
#endif
int tribonacci(int n);
#ifdef __cplusplus
}
#endif
#endif // INC_1137_H

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

@@ -0,0 +1,14 @@
// This file is generated by mkfile.py
// Date: 2025-10-18
#ifndef INC_3397_H
#define INC_3397_H
#ifdef __cplusplus
extern "C"
{
#endif
int maxDistinctElements(int* nums, int numsSize, int k);
#ifdef __cplusplus
}
#endif
#endif // INC_3397_H

30
src/1039.c Normal file
View 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];
}

17
src/1137.c Normal file
View File

@@ -0,0 +1,17 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <solution/1137.h>
int tribonacci(int n)
{
int dp[n + 1];
dp[0] = 0;
if (n >= 1)
dp[1] = 1;
if (n >= 2)
dp[2] = 1;
for (int i = 3; i <= n; i++)
dp[i] = dp[i - 3] + dp[i - 2] + dp[i - 1];
return dp[n];
}

View File

@@ -14,19 +14,20 @@ int maxRepeating(char *sequence, char *word)
return 0;
int dp[n];
memset(dp, 0, sizeof(dp));
for (int i = m - 1; i < n; i++)
{
bool valid = true;
for (int j = 0; j < m; j++)
{
if (sequence[i - m + 1] != word[j])
if (sequence[i - m + 1 + j] != word[j])
{
valid = false;
break;
}
}
if (valid)
dp[i] = (i == m - 1 ? 0 : dp[i - m] + 1);
dp[i] = (i == m - 1 ? 0 : dp[i - m]) + 1;
}
int ret = dp[0];

41
src/3397.c Normal file
View File

@@ -0,0 +1,41 @@
// This file is generated by mkfile.py
// Date: 2025-10-18
#include <solution/3397.h>
#include <stdlib.h>
int cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int maxDistinctElements(int *nums, int numsSize, int k)
{
if (numsSize == 0)
return 0;
qsort(nums, numsSize, sizeof(int), cmp);
int result = 1;
int prev = nums[0] - k;
for (int i = 1; i < numsSize; i++)
{
int min = nums[i] - k;
int max = nums[i] + k;
if (prev >= min)
{
if (max <= prev)
{
continue;
}
prev += 1;
}
else
{
prev = min;
}
result++;
}
return result;
}

45
tests/test_1039.cpp Normal file
View 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);
}

41
tests/test_1137.cpp Normal file
View File

@@ -0,0 +1,41 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <gtest/gtest.h>
#include <solution/1137.h>
// Tests for tribonacci (LeetCode 1137)
class TribonacciTest : public ::testing::Test
{
protected:
void AssertTribo(int n, int expected)
{
ASSERT_EQ(tribonacci(n), expected);
}
};
TEST_F(TribonacciTest, BaseCases)
{
AssertTribo(0, 0);
AssertTribo(1, 1);
AssertTribo(2, 1);
}
TEST_F(TribonacciTest, SmallNumbers)
{
AssertTribo(3, 2);
AssertTribo(4, 4);
AssertTribo(5, 7);
}
TEST_F(TribonacciTest, MediumNumbers)
{
AssertTribo(10, 149);
AssertTribo(15, 3136);
}
TEST_F(TribonacciTest, UpperBound)
{
AssertTribo(25, 1389537);
AssertTribo(37, 2082876103);
}

32
tests/test_3397.cpp Normal file
View File

@@ -0,0 +1,32 @@
// This file is generated by mkfile.py
// Date: 2025-10-18
#include <solution/3397.h>
#include <gtest/gtest.h>
class MaxDistinctElementsTest : public ::testing::Test {
protected:
void AssertMaxDistinctElements(std::vector<int> nums, int k, int expected) {
int* arr = new int[nums.size()];
std::copy(nums.begin(), nums.end(), arr);
int result = maxDistinctElements(arr, nums.size(), k);
ASSERT_EQ(result, expected);
delete[] arr;
}
};
// 示例 1
TEST_F(MaxDistinctElementsTest, Example1) {
// 输入nums = [1,2,2,3,3,4], k = 2
// 输出6
AssertMaxDistinctElements({1, 2, 2, 3, 3, 4}, 2, 6);
}
// 示例 2
TEST_F(MaxDistinctElementsTest, Example2) {
// 输入nums = [4,4,4,4], k = 1
// 输出3
AssertMaxDistinctElements({4, 4, 4, 4}, 1, 3);
}