Skip to content

Commit

Permalink
Add Contest 384
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Feb 14, 2024
1 parent b2290a4 commit 1e948ba
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ add_task(maximum-number-of-vowels-in-a-substring-of-given-length)
add_task(maximum-number-of-words-found-in-sentences)
add_task(maximum-number-of-words-you-can-type)
add_task(maximum-odd-binary-number)
add_task(maximum-palindromes-after-operations)
add_task(maximum-performance-of-a-team)
add_task(maximum-points-after-collecting-coins-from-all-nodes)
add_task(maximum-population-year)
Expand Down Expand Up @@ -1024,6 +1025,7 @@ add_task(missing-element-in-sorted-array)
add_task(missing-number)
add_task(missing-number-in-arithmetic-progression)
add_task(missing-ranges)
add_task(modify-the-matrix)
add_task(monotonic-array)
add_task(most-common-word)
add_task(most-expensive-item-that-can-not-be-bought)
Expand Down Expand Up @@ -1106,6 +1108,8 @@ add_task(number-of-strings-which-can-be-rearranged-to-contain-substring)
add_task(number-of-students-doing-homework-at-a-given-time)
add_task(number-of-students-unable-to-eat-lunch)
add_task(number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold)
add_task(number-of-subarrays-that-match-a-pattern-i)
add_task(number-of-subarrays-that-match-a-pattern-ii)
add_task(number-of-subarrays-with-gcd-equal-to-k)
add_task(number-of-submatrices-that-sum-to-target)
add_task(number-of-subsequences-that-satisfy-the-given-sum-condition)
Expand Down
4 changes: 4 additions & 0 deletions PROBLEM_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -1876,3 +1876,7 @@
| 3029. | [Minimum Time to Revert Word to Initial State I](https://leetcode.com/problems/minimum-time-to-revert-word-to-initial-state-i/) | [C++](./solutions/minimum-time-to-revert-word-to-initial-state-i/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(N)| | |
| 3030. | [Find the Grid of Region Average](https://leetcode.com/problems/find-the-grid-of-region-average/) | [C++](./solutions/find-the-grid-of-region-average/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(MN) / O(MN)| | |
| 3031. | [Minimum Time to Revert Word to Initial State II](https://leetcode.com/problems/minimum-time-to-revert-word-to-initial-state-ii/) | [C++](./solutions/minimum-time-to-revert-word-to-initial-state-ii/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(N) / O(N)| | |
| 3033. | [Modify the Matrix](https://leetcode.com/problems/modify-the-matrix/) | [C++](./solutions/modify-the-matrix/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | O(NM) / O(NM)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Matrix-5e337f?style=flat-square'/> | |
| 3034. | [Number of Subarrays That Match a Pattern I](https://leetcode.com/problems/number-of-subarrays-that-match-a-pattern-i/) | [C++](./solutions/number-of-subarrays-that-match-a-pattern-i/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NM) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Rolling Hash-6b7f33?style=flat-square'/> <img src='https://img.shields.io/badge/String Matching-7f3346?style=flat-square'/> <img src='https://img.shields.io/badge/Hash Function-647f33?style=flat-square'/> | |
| 3035. | [Maximum Palindromes After Operations](https://leetcode.com/problems/maximum-palindromes-after-operations/) | [C++](./solutions/maximum-palindromes-after-operations/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NlogN) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> <img src='https://img.shields.io/badge/Counting-7f7333?style=flat-square'/> | |
| 3036. | [Number of Subarrays That Match a Pattern II](https://leetcode.com/problems/number-of-subarrays-that-match-a-pattern-ii/) | [C++](./solutions/number-of-subarrays-that-match-a-pattern-ii/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(N+M) / O(M)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Rolling Hash-6b7f33?style=flat-square'/> <img src='https://img.shields.io/badge/String Matching-7f3346?style=flat-square'/> <img src='https://img.shields.io/badge/Hash Function-647f33?style=flat-square'/> | |
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 solutions/maximum-palindromes-after-operations/solution.hpp
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;
}
};
30 changes: 30 additions & 0 deletions solutions/maximum-palindromes-after-operations/test.cpp
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);
}
}
1 change: 1 addition & 0 deletions solutions/modify-the-matrix/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_modify_the_matrix test.cpp)
26 changes: 26 additions & 0 deletions solutions/modify-the-matrix/solution.hpp
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;
}
};
26 changes: 26 additions & 0 deletions solutions/modify-the-matrix/test.cpp
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);
}
}
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 solutions/number-of-subarrays-that-match-a-pattern-i/solution.hpp
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 solutions/number-of-subarrays-that-match-a-pattern-i/test.cpp
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);
}
}
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 solutions/number-of-subarrays-that-match-a-pattern-ii/solution.hpp
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 solutions/number-of-subarrays-that-match-a-pattern-ii/test.cpp
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);
}
}

0 comments on commit 1e948ba

Please sign in to comment.