-
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.
- Loading branch information
Showing
14 changed files
with
258 additions
and
0 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 @@ | ||
add_catch(test_maximum_palindromes_after_operations test.cpp) |
31 changes: 31 additions & 0 deletions
31
solutions/maximum-palindromes-after-operations/solution.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,31 @@ | ||
#pragma once | ||
|
||
#include <array> | ||
#include <ranges> | ||
#include <string> | ||
#include <vector> | ||
|
||
// Time: O(NlogN) | ||
// Space: O(N) | ||
|
||
class Solution { | ||
public: | ||
static int maxPalindromesAfterOperations(std::vector<std::string> words) { | ||
std::array<int, 26> counter{}; | ||
int cnt = 0; | ||
for (const auto &word : words) { | ||
for (auto c : word) { | ||
++counter[c - 'a']; | ||
cnt += counter[c - 'a'] % 2 == 0; | ||
} | ||
} | ||
|
||
std::ranges::sort(words, {}, std::ranges::size); | ||
int ans = 0; | ||
for (const auto &word : words) { | ||
cnt -= word.size() / 2; | ||
ans += cnt >= 0; | ||
} | ||
return ans; | ||
} | ||
}; |
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,30 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
std::vector<std::string> words; | ||
int expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.words{"abbb", "ba", "aa"}, | ||
.expected = 3, | ||
}, | ||
{ | ||
.words{"abc", "ab"}, | ||
.expected = 2, | ||
}, | ||
{ | ||
.words{"cd", "ef", "a"}, | ||
.expected = 1, | ||
}, | ||
}; | ||
|
||
for (const auto &[words, expected] : test_cases) { | ||
const auto actual = Solution::maxPalindromesAfterOperations(words); | ||
REQUIRE(expected == actual); | ||
} | ||
} |
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 @@ | ||
add_catch(test_modify_the_matrix test.cpp) |
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 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
// Time: O(NM) | ||
// Space: O(NM) | ||
|
||
class Solution { | ||
public: | ||
static std::vector<std::vector<int>> | ||
modifiedMatrix(std::vector<std::vector<int>> matrix) { | ||
const int n = matrix.size(), m = matrix.back().size(); | ||
for (int j = 0; j < m; ++j) { | ||
auto max = -1; | ||
for (int i = 0; i < n; ++i) { | ||
max = std::max(max, matrix[i][j]); | ||
} | ||
for (int i = 0; i < n; ++i) { | ||
if (matrix[i][j] == -1) { | ||
matrix[i][j] = max; | ||
} | ||
} | ||
} | ||
return matrix; | ||
} | ||
}; |
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 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
std::vector<std::vector<int>> matrix; | ||
std::vector<std::vector<int>> expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.matrix{{1, 2, -1}, {4, -1, 6}, {7, 8, 9}}, | ||
.expected{{1, 2, 9}, {4, 8, 6}, {7, 8, 9}}, | ||
}, | ||
{ | ||
.matrix{{3, -1}, {5, 2}}, | ||
.expected{{3, 2}, {5, 2}}, | ||
}, | ||
}; | ||
|
||
for (const auto &[matrix, expected] : test_cases) { | ||
const auto actual = Solution::modifiedMatrix(matrix); | ||
REQUIRE(expected == actual); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
solutions/number-of-subarrays-that-match-a-pattern-i/CMakeLists.txt
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 @@ | ||
add_catch(test_number_of_subarrays_that_match_a_pattern_i test.cpp) |
24 changes: 24 additions & 0 deletions
24
solutions/number-of-subarrays-that-match-a-pattern-i/solution.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,24 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
// Time: O(NM) | ||
// Space: O(N) | ||
|
||
class Solution { | ||
public: | ||
static int countMatchingSubarrays(const std::vector<int> &nums, | ||
const std::vector<int> &pattern) { | ||
const int n = nums.size() - 1, m = pattern.size(); | ||
std::vector<int> vals(n); | ||
for (int i = 0; i < n; ++i) { | ||
vals[i] = (nums[i + 1] > nums[i]) - (nums[i + 1] < nums[i]); | ||
} | ||
|
||
int ans = 0; | ||
for (int i = 0; i + m <= n; ++i) { | ||
ans += std::equal(pattern.begin(), pattern.end(), vals.begin() + i); | ||
} | ||
return ans; | ||
} | ||
}; |
29 changes: 29 additions & 0 deletions
29
solutions/number-of-subarrays-that-match-a-pattern-i/test.cpp
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 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
std::vector<int> nums; | ||
std::vector<int> pattern; | ||
int expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.nums{1, 2, 3, 4, 5, 6}, | ||
.pattern{1, 1}, | ||
.expected = 4, | ||
}, | ||
{ | ||
.nums{1, 4, 4, 1, 3, 5, 5, 3}, | ||
.pattern{1, 0, -1}, | ||
.expected = 2, | ||
}, | ||
}; | ||
|
||
for (const auto &[nums, pattern, expected] : test_cases) { | ||
const auto actual = Solution::countMatchingSubarrays(nums, pattern); | ||
REQUIRE(expected == actual); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
solutions/number-of-subarrays-that-match-a-pattern-ii/CMakeLists.txt
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 @@ | ||
add_catch(test_number_of_subarrays_that_match_a_pattern_ii test.cpp) |
51 changes: 51 additions & 0 deletions
51
solutions/number-of-subarrays-that-match-a-pattern-ii/solution.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,51 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
// Time: O(N+M) | ||
// Space: O(M) | ||
|
||
class Solution { | ||
public: | ||
static int countMatchingSubarrays(const std::vector<int> &nums, | ||
const std::vector<int> &pattern) { | ||
const int n = nums.size() - 1, m = pattern.size(); | ||
const auto lps = LPS(pattern); | ||
|
||
int ans = 0; | ||
for (int i = 0, j = 0; i < n;) { | ||
const auto value = (nums[i + 1] > nums[i]) - (nums[i + 1] < nums[i]); | ||
if (value == pattern[j]) { | ||
i++, j++; | ||
if (j == m) { | ||
++ans; | ||
j = lps[j - 1]; | ||
} | ||
} else { | ||
if (j) { | ||
j = lps[j - 1]; | ||
} else { | ||
++i; | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
|
||
private: | ||
static std::vector<int> LPS(const std::vector<int> &s) { | ||
std::vector<int> lps(s.size()); | ||
for (int i = 1, j = 0; i < std::ssize(s);) { | ||
if (s[i] == s[j]) { | ||
lps[i++] = ++j; | ||
} else { | ||
if (j) { | ||
j = lps[j - 1]; | ||
} else { | ||
++i; | ||
} | ||
} | ||
} | ||
return lps; | ||
} | ||
}; |
29 changes: 29 additions & 0 deletions
29
solutions/number-of-subarrays-that-match-a-pattern-ii/test.cpp
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 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
std::vector<int> nums; | ||
std::vector<int> pattern; | ||
int expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.nums{1, 2, 3, 4, 5, 6}, | ||
.pattern{1, 1}, | ||
.expected = 4, | ||
}, | ||
{ | ||
.nums{1, 4, 4, 1, 3, 5, 5, 3}, | ||
.pattern{1, 0, -1}, | ||
.expected = 2, | ||
}, | ||
}; | ||
|
||
for (const auto &[nums, pattern, expected] : test_cases) { | ||
const auto actual = Solution::countMatchingSubarrays(nums, pattern); | ||
REQUIRE(expected == actual); | ||
} | ||
} |