-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
</subject> Branch: master <type>: - [ ] Bug fix - [ ] Bug fix (Test) - [x] New feature - [ ] Breaking change - [ ] Documentation update - [ ] This change requires a documentation update <body> <footer> Signed-off-by: Certseeds <[email protected]>
- Loading branch information
Showing
17 changed files
with
501 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// SPDX-License-Identifier: MIT | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
#include "leetcode_941_test.hpp" | ||
|
||
namespace leetcode_941 { | ||
|
||
bool leetcode_941::validMountainArray(const vector<int32_t> &arr) { | ||
const auto arr_size{arr.size()}; | ||
if (arr_size < 3) { | ||
return false; | ||
} else if (arr_size == 3) { | ||
return arr[0] < arr[1] && arr[1] > arr[2]; | ||
} | ||
size_t count{0}; | ||
for (size_t i{1}; i + 1 < arr_size; ++i) { | ||
if (arr[i - 1] == arr[i] || arr[i] == arr[i + 1]) { | ||
return false; | ||
} | ||
count += (arr[i - 1] < arr[i]) == (arr[i] < arr[i + 1]); | ||
} | ||
if (count + 3 == arr_size) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: MIT | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
//@Tag array | ||
//@Tag | ||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_941_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_941_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <vector> | ||
|
||
namespace leetcode_941 { | ||
using std::vector; | ||
|
||
struct leetcode_941 { | ||
static bool validMountainArray(const vector<int32_t> &arr); | ||
}; | ||
|
||
|
||
TEST_CASE("test case 1 {test_941}", "{test_941}") { | ||
const vector<int32_t> input{4, 5, 10, 7}; | ||
CHECK(leetcode_941::validMountainArray(input)); | ||
} | ||
|
||
TEST_CASE("test case 2 {test_941}", "{test_941}") { | ||
const vector<int32_t> input{0, 3, 2, 1}; | ||
CHECK(leetcode_941::validMountainArray(input)); | ||
} | ||
|
||
TEST_CASE("test case 3 {test_941}", "{test_941}") { | ||
const vector<int32_t> input{114, 514, 191}; | ||
CHECK(leetcode_941::validMountainArray(input)); | ||
} | ||
|
||
TEST_CASE("test case 4 {test_941}", "{test_941}") { | ||
const vector<int32_t> input{1, 8, 0}; | ||
CHECK(leetcode_941::validMountainArray(input)); | ||
} | ||
|
||
TEST_CASE("test case 5 {test_941}", "{test_941}") { | ||
const vector<int32_t> input{9, 1, 9}; | ||
CHECK_FALSE(leetcode_941::validMountainArray(input)); | ||
} | ||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_941_TEST_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
#include "leetcode_942_test.hpp" | ||
|
||
namespace leetcode_942 { | ||
|
||
vector<int32_t> leetcode_942::diStringMatch(const string &str) { | ||
const auto str_size{str.size()}; | ||
int32_t min{0}, max{static_cast<int32_t>(str_size)}; | ||
vector<int32_t> willreturn(max + 1); | ||
for (size_t i{0}; i < str_size; i++) { | ||
if (str[i] == 'I') { | ||
willreturn[i] = min; | ||
min++; | ||
} else { | ||
willreturn[i] = max; | ||
max--; | ||
} | ||
} | ||
willreturn.back() = min; | ||
return willreturn; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-License-Identifier: MIT | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
//@Tag array | ||
//@Tag | ||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_942_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_942_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <vector> | ||
#include <string> | ||
|
||
namespace leetcode_942 { | ||
using std::vector; | ||
using std::string; | ||
|
||
struct leetcode_942 { | ||
static vector<int> diStringMatch(const string &str); | ||
}; | ||
|
||
static void check(const string &str, const vector<int32_t> &vec) { | ||
const auto str_size{str.size()}, vec_size{vec.size()}; | ||
CHECK(str_size + 1 == vec_size); | ||
for (size_t i{0}; i < str_size; ++i) { | ||
CHECK((str[i] == 'I' || str[i] == 'D')); | ||
if (str[i] == 'I') { | ||
CHECK(vec[i] < vec[i + 1]); | ||
} else { | ||
CHECK(vec[i] > vec[i + 1]); | ||
} | ||
} | ||
} | ||
|
||
TEST_CASE("test case 1 {test_942}", "{test_942}") { | ||
static constexpr const char *const str{"IDID"}; | ||
check(str, leetcode_942::diStringMatch(str)); | ||
} | ||
|
||
TEST_CASE("test case 2 {test_942}", "{test_942}") { | ||
static constexpr const char *const str{"III"}; | ||
check(str, leetcode_942::diStringMatch(str)); | ||
} | ||
|
||
TEST_CASE("test case 3 {test_942}", "{test_942}") { | ||
static constexpr const char *const str{"DDI"}; | ||
check(str, leetcode_942::diStringMatch(str)); | ||
} | ||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_942_TEST_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-License-Identifier: MIT | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
#include "leetcode_944_test.hpp" | ||
|
||
namespace leetcode_944 { | ||
|
||
int leetcode_944::minDeletionSize(const vector<string> &strs) { | ||
int count{0}; | ||
const auto lines{strs.size()}; | ||
const auto str_size{strs[0].size()}; | ||
for (size_t i{0},notdeletex{true}; i < str_size; ++i,notdeletex = true) { | ||
for (size_t j{1}; j < lines; ++j) { | ||
notdeletex = notdeletex & (strs[j][i] >= strs[j - 1][i]); | ||
} | ||
count += !notdeletex; | ||
} | ||
return count; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-License-Identifier: MIT | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
//@Tag array | ||
//@Tag | ||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_944_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_944_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <vector> | ||
#include <string> | ||
|
||
namespace leetcode_944 { | ||
using std::vector; | ||
using std::string; | ||
|
||
struct leetcode_944 { | ||
static int minDeletionSize(const vector<string> &A); | ||
}; | ||
|
||
TEST_CASE("test case 1 {test_944}", "{test_944}") { | ||
static constexpr const std::array<const char *const, 3> input{ | ||
"abc", "bce", "cae" | ||
}; | ||
static constexpr const auto result{1}; | ||
CHECK(result == leetcode_944::minDeletionSize({input.begin(), input.end()})); | ||
} | ||
|
||
TEST_CASE("test case 2 {test_944}", "{test_944}") { | ||
static constexpr const std::array<const char *const, 2> input{ | ||
"a", "b" | ||
}; | ||
static constexpr const auto result{0}; | ||
CHECK(result == leetcode_944::minDeletionSize({input.begin(), input.end()})); | ||
} | ||
TEST_CASE("test case 3 {test_944}", "{test_944}") { | ||
static constexpr const std::array<const char *const, 3> input{ | ||
"zyx", "wvu", "tsr" | ||
}; | ||
static constexpr const auto result{3}; | ||
CHECK(result == leetcode_944::minDeletionSize({input.begin(), input.end()})); | ||
} | ||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_944_TEST_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-License-Identifier: MIT | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
#include "leetcode_977_test.hpp" | ||
|
||
namespace leetcode_977 { | ||
|
||
// best O(n) | ||
vector<int32_t> leetcode_977::sortedSquares(const vector<int32_t> &nums) { | ||
const auto nums_size{nums.size()}; | ||
static constexpr const auto cmp = [](int32_t x, int32_t y) { | ||
return x * x <= y * y; | ||
}; | ||
vector<int32_t> will_return{}; | ||
const size_t posi = std::min_element(nums.cbegin(), nums.cend(), cmp) - nums.cbegin(); | ||
size_t left{posi + 1}, right{posi + 1}; | ||
while (0 < left && right < nums_size) { | ||
const auto cmpValue = cmp(nums[left - 1], nums[right]); | ||
if (cmpValue) { | ||
will_return.push_back(nums[left - 1] * nums[left - 1]); | ||
--left; | ||
} else { | ||
will_return.push_back(nums[right] * nums[right]); | ||
++right; | ||
} | ||
} | ||
for (; 0 < left; --left) { will_return.push_back(nums[left - 1] * nums[left - 1]); } | ||
for (; right < nums_size; ++right) { will_return.push_back(nums[right] * nums[right]); } | ||
return will_return; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-License-Identifier: MIT | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
//@Tag array | ||
//@Tag | ||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_977_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_977_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <vector> | ||
#include <string> | ||
|
||
namespace leetcode_977 { | ||
using std::vector; | ||
using std::string; | ||
|
||
struct leetcode_977 { | ||
static vector<int32_t> sortedSquares(const vector<int32_t> &nums); | ||
}; | ||
|
||
using Catch::Matchers::Equals; | ||
|
||
TEST_CASE("test case 1 {test_977}", "{test_977}") { | ||
const vector<int32_t> input{-4, -1, 0, 3, 10}; | ||
const vector<int32_t> result{0, 1, 9, 16, 100}; | ||
CHECK_THAT(result, Equals(leetcode_977::sortedSquares(input))); | ||
} | ||
|
||
TEST_CASE("test case 2 {test_977}", "{test_977}") { | ||
const vector<int32_t> input{-7, -3, 2, 3, 11}; | ||
const vector<int32_t> result{4, 9,9, 49, 121}; | ||
CHECK_THAT(result, Equals(leetcode_977::sortedSquares(input))); | ||
} | ||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_977_TEST_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// SPDX-License-Identifier: MIT | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
#include "leetcode_961_test.hpp" | ||
#include <unordered_set> | ||
|
||
namespace leetcode_961 { | ||
using std::unordered_set; | ||
|
||
int32_t leetcode_961::repeatedNTimes(const vector<int32_t> &nums) { | ||
unordered_set<int32_t> uset{}; | ||
for (const auto num: nums) { | ||
if (uset.count(num) != 0) { | ||
return num; | ||
} else { | ||
uset.insert(num); | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
} |
Oops, something went wrong.