Skip to content

Commit

Permalink
feat: update lc-1365
Browse files Browse the repository at this point in the history
Signed-off-by: Certseeds <[email protected]>
  • Loading branch information
Certseeds committed Aug 29, 2023
1 parent 964f968 commit 4183cb5
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
2 changes: 1 addition & 1 deletion algorithm/array/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
8 changes: 4 additions & 4 deletions algorithm/array/leetcode_1360.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ CS203_DSAA_template
Copyright (C) 2022-2023 nanoseeds
*/
#include <numeric>
#include "leetcode_1360_test.hpp"
#include <numeric>

namespace leetcode_1360 {
namespace leetcode_1360 {
Expand All @@ -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)};
Expand Down
2 changes: 1 addition & 1 deletion algorithm/array/leetcode_1360_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ using std::string;

namespace leetcode_1360 {
int32_t daysBetweenDates(const string &date1, const string &date2);
}

using Catch::Matchers::Equals;

Expand Down Expand Up @@ -55,6 +56,5 @@ TEST_CASE("test case 1-4 {test_1360}", "{test_1360}") {



}
}
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1360_TEST_HPP
37 changes: 37 additions & 0 deletions algorithm/array/leetcode_1365.cpp
Original file line number Diff line number Diff line change
@@ -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<int32_t> leetcode_1365::smallerNumbersThanCurrent(const vector<int32_t> &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<int32_t, max_num + 1> 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<int32_t> 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;
}

}
51 changes: 51 additions & 0 deletions algorithm/array/leetcode_1365_test.hpp
Original file line number Diff line number Diff line change
@@ -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 <catch_main.hpp>
#include <cstdint>
#include <cstddef>
#include <vector>
#include <string>

namespace leetcode_1365 {
using std::vector;

namespace leetcode_1365 {
vector<int32_t> smallerNumbersThanCurrent(const vector<int32_t> &nums);
}

using Catch::Matchers::Equals;

TEST_CASE("test case 1-1 {test_1365}", "{test_1365}") {
const vector<int32_t> nums{8, 1, 2, 2, 3};
const vector<int32_t> 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<int32_t> nums{6, 5, 4, 8};
const vector<int32_t> 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<int32_t> nums{7, 7, 7, 7};
const vector<int32_t> 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<int32_t> nums{0, 0, 0, 0};
const vector<int32_t> result{0, 0, 0, 0};
CHECK_THAT(result, Equals(leetcode_1365::smallerNumbersThanCurrent(nums)));
}
}
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1365_TEST_HPP

0 comments on commit 4183cb5

Please sign in to comment.