Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ AllowShortBlocksOnASingleLine: Always
AlwaysBreakTemplateDeclarations: 'Yes'
BasedOnStyle: LLVM
ColumnLimit: 120
IfMacros: [ONLY, ONCE, TWICE, EVERY]
IndentWidth: 4
InsertNewlineAtEOF: true
SpaceBeforeParens: ControlStatementsExceptControlMacros
1 change: 1 addition & 0 deletions leetcode/0001/solution.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <cstdlib>
#include <unordered_map>
#include <vector>
Expand Down
2 changes: 1 addition & 1 deletion leetcode/0001/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "test.hpp"
#include "solution.hpp"
#include "test_utility.hpp"

using namespace testing;
namespace {
Expand Down
1 change: 1 addition & 0 deletions leetcode/0392/solution.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <string>

// 方法1:双指针
Expand Down
2 changes: 1 addition & 1 deletion leetcode/0392/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "test.hpp"
#include "solution.hpp"
#include "test_utility.hpp"

using namespace testing;
namespace {
Expand Down
1 change: 1 addition & 0 deletions leetcode/2011/solution.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <algorithm>
#include <string>
#include <vector>
Expand Down
2 changes: 1 addition & 1 deletion leetcode/2011/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "test.hpp"
#include "solution.hpp"
#include "test_utility.hpp"

using namespace testing;
namespace {
Expand Down
1 change: 1 addition & 0 deletions leetcode/3346/solution.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <algorithm>
#include <ranges>
#include <vector>
Expand Down
2 changes: 1 addition & 1 deletion leetcode/3346/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "test.hpp"
#include "solution.hpp"
#include "test_utility.hpp"

using namespace testing;
namespace {
Expand Down
1 change: 1 addition & 0 deletions leetcode/3347/solution.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <algorithm>
#include <ranges>
#include <vector>
Expand Down
2 changes: 1 addition & 1 deletion leetcode/3347/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "test.hpp"
#include "solution.hpp"
#include "test_utility.hpp"

using namespace testing;
namespace {
Expand Down

This file was deleted.

20 changes: 20 additions & 0 deletions leetcode/3397/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include <algorithm>
#include <ranges>
#include <vector>

// 方法1:贪心
struct Solution {
static int maxDistinctElements(std::vector<int> 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;
}
};
39 changes: 39 additions & 0 deletions leetcode/3397/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "solution.hpp"
#include "test_utility.hpp"

using namespace testing;
namespace {
using SolutionList = TypeList<Solution>;
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<int> nums;
int k;
} input;
int expected;
};

template <typename Solution>
void RunTest(const std::vector<int> &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 <typename Solution>
void RunTest(const TestCase &tc) {
RunTest<Solution>(tc.input.nums, tc.input.k, tc.expected);
}

TEST_Y(LeetCode3397, Example1);
TEST_Y(LeetCode3397, Example2);
} // namespace
11 changes: 11 additions & 0 deletions leetcode/3397/test_suite.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions leetcode/3397/title.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
maximum-number-of-distinct-elements-after-operations
1 change: 1 addition & 0 deletions leetcode/3461/solution.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <string>

struct Solution {
Expand Down
2 changes: 1 addition & 1 deletion leetcode/3461/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "test.hpp"
#include "solution.hpp"
#include "test_utility.hpp"

using namespace testing;
namespace {
Expand Down
2 changes: 1 addition & 1 deletion leetcode/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}\")
Expand Down
2 changes: 1 addition & 1 deletion leetcode/test/test.cpp → leetcode/test/test_utility.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test.hpp"
#include "test_utility.hpp"

#include <cctype>
#include <filesystem>
Expand Down
1 change: 1 addition & 0 deletions leetcode/test/test.hpp → leetcode/test/test_utility.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include "yaml-cpp/yaml.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
Expand Down