Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull request for v0.11.1 #35

Merged
merged 23 commits into from
Sep 16, 2023
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
6 changes: 4 additions & 2 deletions .github/workflows/daily.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-20.04, ubuntu-latest ]
os: [ ubuntu-20.04, ubuntu-22.04 ]
env:
CC: gcc-10
CXX: g++-10
Expand All @@ -30,7 +30,9 @@ jobs:
env:
cache-name: cache-cmake-files
with:
path: ~/.ccache
path: |
~/.ccache
~/.cache
key: ${{ matrix.os }}-cmake-build-${{ hashFiles('CMakeLists.txt', '**/CMakeLists.txt', '**/*.cmake') }}
restore-keys: |
${{ matrix.os }}-cmake-build
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-20.04, ubuntu-latest ]
os: [ ubuntu-20.04, ubuntu-22.04 ]
env:
CC: gcc-10
CXX: g++-10
Expand All @@ -29,7 +29,9 @@ jobs:
env:
cache-name: cache-cmake-files
with:
path: ~/.ccache
path: |
~/.ccache
~/.cache
key: ${{ matrix.os }}-cmake-build-${{ hashFiles('CMakeLists.txt', '**/CMakeLists.txt', '**/*.cmake') }}
restore-keys: |
${{ matrix.os }}-cmake-build
Expand Down Expand Up @@ -103,7 +105,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-20.04, ubuntu-latest ]
os: [ ubuntu-20.04, ubuntu-22.04 ]

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-20.04, ubuntu-latest ]
os: [ ubuntu-20.04, ubuntu-22.04 ]
env:
CC: gcc-10
CXX: g++-10
Expand Down Expand Up @@ -81,7 +81,7 @@ jobs:
if: github.repository == 'Certseeds/CS203_DSAA_template'
needs: test
# The type of runner that the job will run on
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
# Steps represent a sequence of tasks that will be executed as part of the job
steps:

Expand Down Expand Up @@ -147,7 +147,7 @@ jobs:
if: github.repository == 'Certseeds/CS203_DSAA_template'
needs: [ test, build ]
# The type of runner that the job will run on
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
env:
GCC_V: 11
# Steps represent a sequence of tasks that will be executed as part of the job
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rust_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ windows-latest,ubuntu-20.04, ubuntu-latest ]
os: [ windows-latest,ubuntu-20.04, ubuntu-22.04 ]
# Steps represent a sequence of tasks that will be executed as part of the job
steps:

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
test:
name: test dev
# The type of runner that the job will run on
runs-on: ubuntu-latest
runs-on: ubuntu-22.04

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.16.6)

set(PROJECT_VERSION_MAJOR 0)
set(PROJECT_VERSION_MINOR 11)
set(PROJECT_VERSION_PATCH 0) # delete endsWith strs when master merge dev
set(PROJECT_VERSION_PATCH 1) # delete endsWith strs when master merge dev

project(CS203_DSAA_template
DESCRIPTION "Template for Algorithm Based on C++11 and Modern CMake"
Expand Down
2 changes: 1 addition & 1 deletion algorithm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set(dependencies dp fft sort tree binary_search string_search graph moderncpp)
LIST(APPEND dependencies 2021F stack array list effective_cpp)
LIST(APPEND dependencies string trie math disjoint_set divide_merge)
LIST(APPEND dependencies matrix cs302 queue associative_container)
LIST(APPEND dependencies 2020S)
LIST(APPEND dependencies 2020S greedy)
foreach (elementName IN LISTS dependencies)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${elementName})
endforeach ()
Expand Down
3 changes: 2 additions & 1 deletion algorithm/array/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ 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)
list(APPEND leetcode_order 1217 1329 1360 1365 1371)
list(APPEND leetcode_order 1391)
LIST(TRANSFORM leetcode_order PREPEND leetcode_)

set(dependencies ${dependencies} ${leetcode_order})
Expand Down
48 changes: 48 additions & 0 deletions algorithm/array/leetcode_1360.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
/*
CS203_DSAA_template

Copyright (C) 2022-2023 nanoseeds

*/
#include "leetcode_1360_test.hpp"
#include <numeric>

namespace leetcode_1360 {
namespace leetcode_1360 {

static constexpr const std::array<int32_t, 13> days_of_365{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 365};
static constexpr const std::array<int32_t, 13> days_of_366{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 366};

std::array<int32_t, 13> year_days(int32_t year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return days_of_366;
}
return days_of_365;
}

int32_t daysBetweenDates(const string &date1, const string &date2) {
const auto value = date1.compare(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 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)};
const auto days_between_years = [](int32_t begin, int32_t end) {
int32_t will_return{0};
for (int32_t x{begin}; x < end; x++) {
will_return += year_days(x).back();
}
return will_return;
}(yy1, yy2);
const auto days = days_between_years + days_of_begin2 - days_of_begin1;
// 先归零, 统一计算到1月1号有多少天, 然后计算间隔多少year
return days;
}

}
}
60 changes: 60 additions & 0 deletions algorithm/array/leetcode_1360_test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
/*
CS203_DSAA_template

Copyright (C) 2022-2023 nanoseeds

*/
//@Tag array
//@Tag 数组
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1360_TEST_HPP
#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1360_TEST_HPP

#include <catch_main.hpp>
#include <cstdint>
#include <cstddef>
#include <vector>
#include <string>

namespace leetcode_1360 {
using std::vector;
using std::string;

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

using Catch::Matchers::Equals;

TEST_CASE("test case 1-1 {test_1360}", "{test_1360}") {
constexpr const char *const begin{"2019-06-29"};
constexpr const char *const end{"2019-06-30"};
constexpr const int32_t value{1};
CHECK(value == leetcode_1360::daysBetweenDates(begin, end));
}

TEST_CASE("test case 1-2 {test_1360}", "{test_1360}") {
constexpr const char *const begin{"2020-01-15"};
constexpr const char *const end{"2019-12-31"};
constexpr const int32_t value{15};
CHECK(value == leetcode_1360::daysBetweenDates(begin, end));
}

TEST_CASE("test case 1-3 {test_1360}", "{test_1360}") {
constexpr const char *const begin{"1971-06-29"};
constexpr const char *const end{"2010-09-23"};
constexpr const int32_t value{14331};
CHECK(value == leetcode_1360::daysBetweenDates(begin, end));
}

TEST_CASE("test case 1-4 {test_1360}", "{test_1360}") {
constexpr const char *const begin{"2009-08-18"};
constexpr const char *const end{"2080-08-08"};
constexpr const int32_t value{25923};
CHECK(value == leetcode_1360::daysBetweenDates(begin, end));
}



}
#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
41 changes: 41 additions & 0 deletions algorithm/array/leetcode_1371.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
/*
CS203_DSAA_template

Copyright (C) 2023 nanoseeds

*/
#include "leetcode_1371_test.hpp"

namespace leetcode_1371 {
// 把寻找最长字串的问题, 转化成在[0,i)之间寻找一个点, [0,point), [0,i)上, 元音字母的odd/even相同
// 经典的把O(N^2)的组合问题 拆分成O(N)的(0,N)之后两者作差
int32_t leetcode_1371::findTheLongestSubstring(const string &s) {
constexpr const auto chnums{26};
constexpr const std::array<int32_t, chnums> nums{
0b1, 0, 0, 0, 0b10,
0, 0, 0, 0b100, 0,
0, 0, 0, 0, 0b1000,
0, 0, 0, 0, 0,
0b10000, 0, 0, 0, 0,
0
};
constexpr const auto specials{5};
std::array<int32_t, (1 << specials)> map{-1,}; // key, even-odd值
for (size_t i{1}; i < (1 << specials); ++i) {
map[i] = 0x3f3f3f3f;
}
// value latest prefix
int32_t distance{0};
for (int32_t i{0}, count_num{0}; i < static_cast<int32_t>(s.size()); ++i) {
const auto ch{s[i]};
count_num ^= (nums[ch - 'a']);
map[count_num] = std::min(map[count_num], i); // 把值赋给最早的
if (map[count_num] != 0x3f3f3f3f) {
distance = std::max(distance, i - map[count_num]);
}
}
return distance;
}

}
33 changes: 33 additions & 0 deletions algorithm/array/leetcode_1371_test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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_1371_TEST_HPP
#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1371_TEST_HPP

#include <catch_main.hpp>
#include <cstdint>
#include <cstddef>
#include <vector>
#include <string>

namespace leetcode_1371 {
using std::vector;

namespace leetcode_1371 {
int32_t findTheLongestSubstring(const std::string &s);
}

TEST_CASE("test case 1-1 {test_1371}", "{test_1371}") {
constexpr const char*const input{"leetcodeisgreat"};
constexpr const auto output{5};
CHECK(output == leetcode_1371::findTheLongestSubstring(input));
}

}
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1371_TEST_HPP
Loading