From 4785c622fa0196f944b7abc6806959b25968043a Mon Sep 17 00:00:00 2001 From: chaimode Date: Wed, 21 Jan 2026 02:27:34 +0000 Subject: [PATCH] feat: Solve LeetCode Daily Challenge #3315 --- .../construct-the-minimum-bitwise-array-ii.h | 18 ++++ ...construct-the-minimum-bitwise-array-ii.cpp | 94 +++++++++++++++++++ ...construct-the-minimum-bitwise-array-ii.cpp | 92 ++++++++++++++++++ 3 files changed, 204 insertions(+) create mode 100644 include/leetcode/problems/construct-the-minimum-bitwise-array-ii.h create mode 100644 src/leetcode/problems/construct-the-minimum-bitwise-array-ii.cpp create mode 100644 test/leetcode/problems/construct-the-minimum-bitwise-array-ii.cpp diff --git a/include/leetcode/problems/construct-the-minimum-bitwise-array-ii.h b/include/leetcode/problems/construct-the-minimum-bitwise-array-ii.h new file mode 100644 index 0000000..c6bab19 --- /dev/null +++ b/include/leetcode/problems/construct-the-minimum-bitwise-array-ii.h @@ -0,0 +1,18 @@ +#include "leetcode/core.h" + +namespace leetcode { +namespace problem_3315 { + +using Func = std::function(vector&)>; + +class ConstructTheMinimumBitwiseArrayIiSolution : public SolutionBase { + public: + //! 3315. Construct the Minimum Bitwise Array II + //! https://leetcode.com/problems/construct-the-minimum-bitwise-array-ii/ + vector minBitwiseArray(vector& nums); + + ConstructTheMinimumBitwiseArrayIiSolution(); +}; + +} // namespace problem_3315 +} // namespace leetcode \ No newline at end of file diff --git a/src/leetcode/problems/construct-the-minimum-bitwise-array-ii.cpp b/src/leetcode/problems/construct-the-minimum-bitwise-array-ii.cpp new file mode 100644 index 0000000..d4f477e --- /dev/null +++ b/src/leetcode/problems/construct-the-minimum-bitwise-array-ii.cpp @@ -0,0 +1,94 @@ +#include "leetcode/problems/construct-the-minimum-bitwise-array-ii.h" + +namespace leetcode { +namespace problem_3315 { + +// 策略1:直接计算后缀连续1的个数,然后减去相应位 +// 时间复杂度: O(n log MAX),空间复杂度: O(1) +static vector solution1(vector& nums) { + vector ans; + ans.reserve(nums.size()); + for (int num : nums) { + // 计算从最低位开始的连续1的个数 + int suffix_ones = 0; + int temp = num; + while ((temp & 1) == 1) { + ++suffix_ones; + temp >>= 1; + } + // 如果后缀连续1的个数为0,则无解 + if (suffix_ones == 0) { + ans.push_back(-1); + } else { + // 最小x = num - (1 << (suffix_ones - 1)) + ans.push_back(num - (1 << (suffix_ones - 1))); + } + } + return ans; +} + +// 策略2:使用位运算技巧,直接构造x +// 时间复杂度: O(n log MAX),空间复杂度: O(1) +static vector solution2(vector& nums) { + vector ans; + ans.reserve(nums.size()); + for (int num : nums) { + // 找到最低的0位的位置(从0开始),实际上就是后缀连续1的个数 + int suffix_ones = 0; + int temp = num; + while ((temp & 1) == 1) { + ++suffix_ones; + temp >>= 1; + } + if (suffix_ones == 0) { + ans.push_back(-1); + } else { + // 构造mask,将第(suffix_ones-1)位清零 + int mask = 1 << (suffix_ones - 1); + ans.push_back(num & ~mask); + } + } + return ans; +} + +// 策略3:使用内置函数__builtin_ctz计算后缀0的个数,从而得到连续1的个数 +// 注意:__builtin_ctz(0)是未定义的,但这里temp = num ^ ((1 << suffix_ones) - 1)可能为0 +// 更安全的方法:直接计算连续1的个数 +// 时间复杂度: O(n),空间复杂度: O(1) +static vector solution3(vector& nums) { + vector ans; + ans.reserve(nums.size()); + for (int num : nums) { + // 计算后缀连续1的个数:找到最低的0位 + // 方法:计算 (num ^ (num + 1)) >> 1 的尾部0的个数?需要推导 + // 更简单:直接循环 + int suffix_ones = 0; + int temp = num; + while ((temp & 1) == 1) { + ++suffix_ones; + temp >>= 1; + } + if (suffix_ones == 0) { + ans.push_back(-1); + } else { + ans.push_back(num - (1 << (suffix_ones - 1))); + } + } + return ans; +} + +ConstructTheMinimumBitwiseArrayIiSolution::ConstructTheMinimumBitwiseArrayIiSolution() { + setMetaInfo({.id = 3315, + .title = "Construct the Minimum Bitwise Array II", + .url = "https://leetcode.com/problems/construct-the-minimum-bitwise-array-ii/"}); + registerStrategy("Count Suffix Ones", solution1); + registerStrategy("Bit Mask", solution2); + registerStrategy("Alternative", solution3); +} + +vector ConstructTheMinimumBitwiseArrayIiSolution::minBitwiseArray(vector& nums) { + return getSolution()(nums); +} + +} // namespace problem_3315 +} // namespace leetcode \ No newline at end of file diff --git a/test/leetcode/problems/construct-the-minimum-bitwise-array-ii.cpp b/test/leetcode/problems/construct-the-minimum-bitwise-array-ii.cpp new file mode 100644 index 0000000..62e8281 --- /dev/null +++ b/test/leetcode/problems/construct-the-minimum-bitwise-array-ii.cpp @@ -0,0 +1,92 @@ +#include "leetcode/problems/construct-the-minimum-bitwise-array-ii.h" + +#include "gtest/gtest.h" + +namespace leetcode { +namespace problem_3315 { + +class ConstructTheMinimumBitwiseArrayIiTest : public ::testing::TestWithParam { + protected: + void SetUp() override { solution.setStrategy(GetParam()); } + + ConstructTheMinimumBitwiseArrayIiSolution solution; +}; + +TEST_P(ConstructTheMinimumBitwiseArrayIiTest, Example1) { + vector nums = {2, 3, 5, 7}; + vector expected = {-1, 1, 4, 3}; + vector result = solution.minBitwiseArray(nums); + EXPECT_EQ(expected, result); +} + +TEST_P(ConstructTheMinimumBitwiseArrayIiTest, Example2) { + vector nums = {11, 13, 31}; + vector expected = {9, 12, 15}; + vector result = solution.minBitwiseArray(nums); + EXPECT_EQ(expected, result); +} + +TEST_P(ConstructTheMinimumBitwiseArrayIiTest, AdditionalTest1) { + vector nums = {2}; + vector expected = {-1}; + vector result = solution.minBitwiseArray(nums); + EXPECT_EQ(expected, result); +} + +TEST_P(ConstructTheMinimumBitwiseArrayIiTest, AdditionalTest2) { + vector nums = {3}; + vector expected = {1}; + vector result = solution.minBitwiseArray(nums); + EXPECT_EQ(expected, result); +} + +TEST_P(ConstructTheMinimumBitwiseArrayIiTest, AdditionalTest3) { + vector nums = {5}; + vector expected = {4}; + vector result = solution.minBitwiseArray(nums); + EXPECT_EQ(expected, result); +} + +TEST_P(ConstructTheMinimumBitwiseArrayIiTest, AdditionalTest4) { + vector nums = {7}; + vector expected = {3}; + vector result = solution.minBitwiseArray(nums); + EXPECT_EQ(expected, result); +} + +TEST_P(ConstructTheMinimumBitwiseArrayIiTest, AdditionalTest5) { + vector nums = {131}; // 10000011 binary + // 后缀连续1的个数:最低位1,次低位1,第三位0 -> suffix_ones = 2 + // ans = 131 - (1 << 1) = 131 - 2 = 129 + // 验证:129 (10000001) OR 130 (10000010) = 131 (10000011) + vector expected = {129}; + vector result = solution.minBitwiseArray(nums); + EXPECT_EQ(expected, result); +} + +TEST_P(ConstructTheMinimumBitwiseArrayIiTest, AdditionalTest6) { + vector nums = {257}; // 100000001 binary + // 后缀连续1的个数:最低位1,次低位0 -> suffix_ones = 1 + // ans = 257 - 1 = 256 + // 验证:256 (100000000) OR 257 (100000001) = 257 + vector expected = {256}; + vector result = solution.minBitwiseArray(nums); + EXPECT_EQ(expected, result); +} + +TEST_P(ConstructTheMinimumBitwiseArrayIiTest, AdditionalTest7) { + vector nums = {1023}; // 1111111111 (10 bits) + // 后缀连续1的个数 = 10 + // ans = 1023 - (1 << 9) = 1023 - 512 = 511 + // 验证:511 (0111111111) OR 512 (1000000000) = 1023 (1111111111) + vector expected = {511}; + vector result = solution.minBitwiseArray(nums); + EXPECT_EQ(expected, result); +} + +INSTANTIATE_TEST_SUITE_P( + LeetCode, ConstructTheMinimumBitwiseArrayIiTest, + ::testing::ValuesIn(ConstructTheMinimumBitwiseArrayIiSolution().getStrategyNames())); + +} // namespace problem_3315 +} // namespace leetcode \ No newline at end of file