diff --git a/.clang-format b/.clang-format index fa6e7ac..40ce86b 100644 --- a/.clang-format +++ b/.clang-format @@ -7,7 +7,6 @@ AllowShortBlocksOnASingleLine: Always AlwaysBreakTemplateDeclarations: 'Yes' BasedOnStyle: LLVM ColumnLimit: 120 -IfMacros: [ONLY, ONCE, TWICE, EVERY] IndentWidth: 4 InsertNewlineAtEOF: true SpaceBeforeParens: ControlStatementsExceptControlMacros diff --git a/leetcode/0001/solution.hpp b/leetcode/0001/solution.hpp index dbea879..345bc7a 100644 --- a/leetcode/0001/solution.hpp +++ b/leetcode/0001/solution.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/leetcode/0001/test.cpp b/leetcode/0001/test.cpp index e30cf4a..b224515 100644 --- a/leetcode/0001/test.cpp +++ b/leetcode/0001/test.cpp @@ -1,5 +1,5 @@ -#include "test.hpp" #include "solution.hpp" +#include "test_utility.hpp" using namespace testing; namespace { diff --git a/leetcode/0392/solution.hpp b/leetcode/0392/solution.hpp index 8341eeb..2d15c2f 100644 --- a/leetcode/0392/solution.hpp +++ b/leetcode/0392/solution.hpp @@ -1,3 +1,4 @@ +#pragma once #include // 方法1:双指针 diff --git a/leetcode/0392/test.cpp b/leetcode/0392/test.cpp index b57c67d..2e4ed6c 100644 --- a/leetcode/0392/test.cpp +++ b/leetcode/0392/test.cpp @@ -1,5 +1,5 @@ -#include "test.hpp" #include "solution.hpp" +#include "test_utility.hpp" using namespace testing; namespace { diff --git a/leetcode/2011/solution.hpp b/leetcode/2011/solution.hpp index 88906e6..f239b2e 100644 --- a/leetcode/2011/solution.hpp +++ b/leetcode/2011/solution.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/leetcode/2011/test.cpp b/leetcode/2011/test.cpp index 67b4bc1..5537561 100644 --- a/leetcode/2011/test.cpp +++ b/leetcode/2011/test.cpp @@ -1,5 +1,5 @@ -#include "test.hpp" #include "solution.hpp" +#include "test_utility.hpp" using namespace testing; namespace { diff --git a/leetcode/3346/solution.hpp b/leetcode/3346/solution.hpp index 931c13f..f7d22db 100644 --- a/leetcode/3346/solution.hpp +++ b/leetcode/3346/solution.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/leetcode/3346/test.cpp b/leetcode/3346/test.cpp index 71117b5..5c1293e 100644 --- a/leetcode/3346/test.cpp +++ b/leetcode/3346/test.cpp @@ -1,5 +1,5 @@ -#include "test.hpp" #include "solution.hpp" +#include "test_utility.hpp" using namespace testing; namespace { diff --git a/leetcode/3347/solution.hpp b/leetcode/3347/solution.hpp index 5690cfc..49b81be 100644 --- a/leetcode/3347/solution.hpp +++ b/leetcode/3347/solution.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/leetcode/3347/test.cpp b/leetcode/3347/test.cpp index 1eeb6fa..51b7637 100644 --- a/leetcode/3347/test.cpp +++ b/leetcode/3347/test.cpp @@ -1,5 +1,5 @@ -#include "test.hpp" #include "solution.hpp" +#include "test_utility.hpp" using namespace testing; namespace { diff --git a/leetcode/3397-maximum-number-of-distinct-elements-after-operations/solution.hpp b/leetcode/3397-maximum-number-of-distinct-elements-after-operations/solution.hpp deleted file mode 100644 index 6503298..0000000 --- a/leetcode/3397-maximum-number-of-distinct-elements-after-operations/solution.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include - -class Solution { -public: - int maxDistinctElements(std::vector &nums, int k) { - std::sort(nums.begin(), nums.end()); - int count = 1; - int min_v = nums[0] - k + 1; - for (int i{1}; i < nums.size(); ++i) { - if (nums[i] + k >= min_v) { - min_v = std::max(min_v, nums[i] - k) + 1; - count++; - } - } - return count; - } -}; diff --git a/leetcode/3397/solution.hpp b/leetcode/3397/solution.hpp new file mode 100644 index 0000000..d5859e4 --- /dev/null +++ b/leetcode/3397/solution.hpp @@ -0,0 +1,20 @@ +#pragma once +#include +#include +#include + +// 方法1:贪心 +struct Solution { + static int maxDistinctElements(std::vector nums, int k) { + std::ranges::sort(nums); + int res{1}, prev{nums[0] - k}; + for (std::size_t i{1}; i < nums.size(); ++i) { + if (nums[i] + k > prev) { + // 每个数都取可能的最小值 + prev = std::max(prev + 1, nums[i] - k); + ++res; + } + } + return res; + } +}; diff --git a/leetcode/3397/test.cpp b/leetcode/3397/test.cpp new file mode 100644 index 0000000..d08a9ad --- /dev/null +++ b/leetcode/3397/test.cpp @@ -0,0 +1,39 @@ +#include "solution.hpp" +#include "test_utility.hpp" + +using namespace testing; +namespace { +using SolutionList = TypeList; +struct TestCase { + TestCase(const YAML::Node &node) { + Decode(node["input"]["nums"], input.nums); + Decode(node["input"]["k"], input.k); + Decode(node["expected"], expected); + } + struct { + std::vector nums; + int k; + } input; + int expected; +}; + +template +void RunTest(const std::vector &nums, const int k, const int expected) { + // 检查约束条件 + ASSERT_THAT(nums.size(), AllOf(Ge(1), Le(E<5>))); // 1 <= nums.length <= 10^5 + ASSERT_THAT(nums, Each(AllOf(Ge(1), Le(E<9>)))); // 1 <= nums[i] <= 10^9 + ASSERT_THAT(k, AllOf(Ge(1), Le(E<9>))); // 0 <= k <= 10^9 + + // 测试目标函数 + Solution solution; + EXPECT_EQ(solution.maxDistinctElements(nums, k), expected); +} + +template +void RunTest(const TestCase &tc) { + RunTest(tc.input.nums, tc.input.k, tc.expected); +} + +TEST_Y(LeetCode3397, Example1); +TEST_Y(LeetCode3397, Example2); +} // namespace diff --git a/leetcode/3397/test_suite.yaml b/leetcode/3397/test_suite.yaml new file mode 100644 index 0000000..b4c9c2c --- /dev/null +++ b/leetcode/3397/test_suite.yaml @@ -0,0 +1,11 @@ +example1: + input: + nums: [1,2,2,3,3,4] + k: 2 + expected: 6 + +example2: + input: + nums: [4,4,4,4] + k: 1 + expected: 3 diff --git a/leetcode/3397/title.txt b/leetcode/3397/title.txt new file mode 100644 index 0000000..48788ee --- /dev/null +++ b/leetcode/3397/title.txt @@ -0,0 +1 @@ +maximum-number-of-distinct-elements-after-operations diff --git a/leetcode/3461/solution.hpp b/leetcode/3461/solution.hpp index e52db2a..0e61d39 100644 --- a/leetcode/3461/solution.hpp +++ b/leetcode/3461/solution.hpp @@ -1,3 +1,4 @@ +#pragma once #include struct Solution { diff --git a/leetcode/3461/test.cpp b/leetcode/3461/test.cpp index 02ed0cd..7214a9d 100644 --- a/leetcode/3461/test.cpp +++ b/leetcode/3461/test.cpp @@ -1,5 +1,5 @@ -#include "test.hpp" #include "solution.hpp" +#include "test_utility.hpp" using namespace testing; namespace { diff --git a/leetcode/CMakeLists.txt b/leetcode/CMakeLists.txt index 2e8b71f..6d9e7f4 100644 --- a/leetcode/CMakeLists.txt +++ b/leetcode/CMakeLists.txt @@ -1,5 +1,5 @@ # 构建测试对象库 -file(GLOB_RECURSE TEST_SRC "*/test.cpp") +file(GLOB_RECURSE TEST_SRC "*.cpp") add_library(test_leetcode_o OBJECT ${TEST_SRC}) target_include_directories(test_leetcode_o PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/test) target_compile_definitions(test_leetcode_o PRIVATE PROBLEM_PATH=\"${CMAKE_CURRENT_SOURCE_DIR}\") diff --git a/leetcode/test/test.cpp b/leetcode/test/test_utility.cpp similarity index 96% rename from leetcode/test/test.cpp rename to leetcode/test/test_utility.cpp index dbb9948..0e38d74 100644 --- a/leetcode/test/test.cpp +++ b/leetcode/test/test_utility.cpp @@ -1,4 +1,4 @@ -#include "test.hpp" +#include "test_utility.hpp" #include #include diff --git a/leetcode/test/test.hpp b/leetcode/test/test_utility.hpp similarity index 99% rename from leetcode/test/test.hpp rename to leetcode/test/test_utility.hpp index 157d3b5..4534698 100644 --- a/leetcode/test/test.hpp +++ b/leetcode/test/test_utility.hpp @@ -1,3 +1,4 @@ +#pragma once #include "yaml-cpp/yaml.h" #include "gmock/gmock.h" #include "gtest/gtest.h"