From 4183cb541f3fa8551ce4b5be15b77f0bdee2c44e Mon Sep 17 00:00:00 2001 From: Certseeds <51754303+Certseeds@users.noreply.github.com> Date: Tue, 29 Aug 2023 13:25:38 +0000 Subject: [PATCH] feat: update lc-1365 Signed-off-by: Certseeds <51754303+Certseeds@users.noreply.github.com> --- algorithm/array/CMakeLists.txt | 2 +- algorithm/array/leetcode_1360.cpp | 8 ++-- algorithm/array/leetcode_1360_test.hpp | 2 +- algorithm/array/leetcode_1365.cpp | 37 +++++++++++++++++++ algorithm/array/leetcode_1365_test.hpp | 51 ++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 algorithm/array/leetcode_1365.cpp create mode 100644 algorithm/array/leetcode_1365_test.hpp diff --git a/algorithm/array/CMakeLists.txt b/algorithm/array/CMakeLists.txt index 5f5a9e08..4acba2e5 100644 --- a/algorithm/array/CMakeLists.txt +++ b/algorithm/array/CMakeLists.txt @@ -23,7 +23,7 @@ list(APPEND leetcode_order 905 908 922 941 942) list(APPEND leetcode_order 944 977 985 986 989) list(APPEND leetcode_order 999 1010 1013 1030 1051) list(APPEND leetcode_order 1089 1108 1170 1184 1200) -list(APPEND leetcode_order 1217 1329 1360) +list(APPEND leetcode_order 1217 1329 1360 1365) LIST(TRANSFORM leetcode_order PREPEND leetcode_) set(dependencies ${dependencies} ${leetcode_order}) diff --git a/algorithm/array/leetcode_1360.cpp b/algorithm/array/leetcode_1360.cpp index dabae3cf..00f2668b 100644 --- a/algorithm/array/leetcode_1360.cpp +++ b/algorithm/array/leetcode_1360.cpp @@ -5,8 +5,8 @@ CS203_DSAA_template Copyright (C) 2022-2023 nanoseeds */ -#include #include "leetcode_1360_test.hpp" +#include namespace leetcode_1360 { namespace leetcode_1360 { @@ -26,9 +26,9 @@ int32_t daysBetweenDates(const string &date1, const string &date2) { if (value > 0) { return daysBetweenDates(date2, date1); } - const auto yy1{std::stoi(date1.substr(0, 4))},yy2{std::stoi(date2.substr(0, 4))}; - const auto mm1{std::stoi(date1.substr(5, 7))},mm2{std::stoi(date2.substr(5, 7))}; - const auto dd1{std::stoi(date1.substr(8, 10))},dd2{std::stoi(date2.substr(8, 10))}; + const auto yy1{std::stoi(date1.substr(0, 4))}, yy2{std::stoi(date2.substr(0, 4))}; + const auto mm1{std::stoi(date1.substr(5, 7))}, mm2{std::stoi(date2.substr(5, 7))}; + const auto dd1{std::stoi(date1.substr(8, 10))}, dd2{std::stoi(date2.substr(8, 10))}; const auto days_of_year1{year_days(yy1)}, days_of_year2{year_days(yy2)}; const auto days_of_begin1{dd1 + std::accumulate(days_of_year1.begin(), days_of_year1.begin() + mm1 - 1, 0)}; const auto days_of_begin2{dd2 + std::accumulate(days_of_year2.begin(), days_of_year2.begin() + mm2 - 1, 0)}; diff --git a/algorithm/array/leetcode_1360_test.hpp b/algorithm/array/leetcode_1360_test.hpp index b5269c29..ed3b9179 100644 --- a/algorithm/array/leetcode_1360_test.hpp +++ b/algorithm/array/leetcode_1360_test.hpp @@ -22,6 +22,7 @@ using std::string; namespace leetcode_1360 { int32_t daysBetweenDates(const string &date1, const string &date2); +} using Catch::Matchers::Equals; @@ -55,6 +56,5 @@ TEST_CASE("test case 1-4 {test_1360}", "{test_1360}") { -} } #endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1360_TEST_HPP diff --git a/algorithm/array/leetcode_1365.cpp b/algorithm/array/leetcode_1365.cpp new file mode 100644 index 00000000..dd1b4d40 --- /dev/null +++ b/algorithm/array/leetcode_1365.cpp @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +/* +CS203_DSAA_template + +Copyright (C) 2023 nanoseeds + +*/ +#include "leetcode_1365_test.hpp" + +namespace leetcode_1365 { + +vector leetcode_1365::smallerNumbersThanCurrent(const vector &nums) { + constexpr const auto max_num{100}; + const auto nums_size{nums.size()}; + for (const auto num: nums) { + assert(num >= 0); + assert(num <= max_num); + } + assert(nums_size >= 2); + assert(nums_size <= 500); + std::array arrs{0,}; + for (const auto num: nums) { + arrs[num] += 1; + } + for (int32_t i{1}; i < max_num + 1; i++) { + arrs[i] = arrs[i] + arrs[i - 1]; + } + vector will_return(nums_size, 0); + for (size_t i{0}; i < nums_size; i++) { + if (nums[i] != 0) { + will_return[i] = arrs[nums[i] - 1]; + } + } + return will_return; +} + +} diff --git a/algorithm/array/leetcode_1365_test.hpp b/algorithm/array/leetcode_1365_test.hpp new file mode 100644 index 00000000..3ff2ee68 --- /dev/null +++ b/algorithm/array/leetcode_1365_test.hpp @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +/* +CS203_DSAA_template + +Copyright (C) 2023 nanoseeds + +*/ +//@Tag array +//@Tag 数组 +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1365_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1365_TEST_HPP + +#include +#include +#include +#include +#include + +namespace leetcode_1365 { +using std::vector; + +namespace leetcode_1365 { +vector smallerNumbersThanCurrent(const vector &nums); +} + +using Catch::Matchers::Equals; + +TEST_CASE("test case 1-1 {test_1365}", "{test_1365}") { + const vector nums{8, 1, 2, 2, 3}; + const vector result{4, 0, 1, 1, 3}; + CHECK_THAT(result, Equals(leetcode_1365::smallerNumbersThanCurrent(nums))); +} + +TEST_CASE("test case 1-2 {test_1365}", "{test_1365}") { + const vector nums{6, 5, 4, 8}; + const vector result{2, 1, 0, 3}; + CHECK_THAT(result, Equals(leetcode_1365::smallerNumbersThanCurrent(nums))); +} + +TEST_CASE("test case 1-3 {test_1365}", "{test_1365}") { + const vector nums{7, 7, 7, 7}; + const vector result{0, 0, 0, 0}; + CHECK_THAT(result, Equals(leetcode_1365::smallerNumbersThanCurrent(nums))); +} +TEST_CASE("test case 1-4 {test_1365}", "{test_1365}") { + const vector nums{0, 0, 0, 0}; + const vector result{0, 0, 0, 0}; + CHECK_THAT(result, Equals(leetcode_1365::smallerNumbersThanCurrent(nums))); +} +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1365_TEST_HPP