Skip to content

Commit

Permalink
Maximum Number of Operations With the Same Score II
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Feb 18, 2024
1 parent 9790e7c commit 5d9dcff
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ add_task(maximum-number-of-fish-in-a-grid)
add_task(maximum-number-of-non-overlapping-palindrome-substrings)
add_task(maximum-number-of-ones)
add_task(maximum-number-of-operations-with-the-same-score-i)
add_task(maximum-number-of-operations-with-the-same-score-ii)
add_task(maximum-number-of-pairs-in-array)
add_task(maximum-number-of-points-from-grid-queries)
add_task(maximum-number-of-removable-characters)
Expand Down
1 change: 1 addition & 0 deletions PROBLEM_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,7 @@
| 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'/> | |
| 3038. | [Maximum Number of Operations With the Same Score I](https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-i/) | [C++](./solutions/maximum-number-of-operations-with-the-same-score-i/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | O(N) / O(1)| | |
| 3039. | [Apply Operations to Make String Empty](https://leetcode.com/problems/apply-operations-to-make-string-empty/) | [C++](./solutions/apply-operations-to-make-string-empty/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(1)| | |
| 3040. | [Maximum Number of Operations With the Same Score II](https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-ii/) | [C++](./solutions/maximum-number-of-operations-with-the-same-score-ii/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N<sup>2</sup>) / O(N<sup>2</sup>)| | |
| 3042. | [Count Prefix and Suffix Pairs I](https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/) | [C++](./solutions/count-prefix-and-suffix-pairs-i/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | O(N<sup>2</sup>K) / O(1)| | |
| 3043. | [Find the Length of the Longest Common Prefix](https://leetcode.com/problems/find-the-length-of-the-longest-common-prefix/) | [C++](./solutions/find-the-length-of-the-longest-common-prefix/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N+M) / O(N)| | |
| 3044. | [Most Frequent Prime](https://leetcode.com/problems/most-frequent-prime/) | [C++](./solutions/most-frequent-prime/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N<sup>2</sup>M<sup>2</sup>K<sup>0.5</sup>) / O(1)| | |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_maximum_number_of_operations_with_the_same_score_ii test.cpp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <functional>
#include <ranges>
#include <vector>

// Time: O(N^2)
// Space: O(N^2)

class Solution {
public:
static int maxOperations(const std::vector<int> &nums) {
std::function<int(int, int, int, std::vector<std::vector<int>> *)> maxOps =
[&](int l, int r, int score,
std::vector<std::vector<int>> *memo) -> int {
if (r - l < 1) {
return 0;
}
auto &ans = (*memo)[l][r];
if (ans == -1) {
ans = 0;
if (nums[l] + nums[l + 1] == score) {
ans = std::max(ans, 1 + maxOps(l + 2, r, score, memo));
}
if (nums[r - 1] + nums[r] == score) {
ans = std::max(ans, 1 + maxOps(l, r - 2, score, memo));
}
if (nums[l] + nums[r] == score) {
ans = std::max(ans, 1 + maxOps(l + 1, r - 1, score, memo));
}
}
return ans;
};

const int n = nums.size();
int ans = 0;
std::vector memo(n, std::vector<int>(n));
for (auto score : {nums[0] + nums[1], nums[n - 2] + nums[n - 1],
nums[0] + nums[n - 1]}) {
std::ranges::fill(memo, std::vector<int>(n, -1));
ans = std::max(ans, maxOps(0, n - 1, score, &memo));
}
return ans;
}
};
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<int> nums;
int expected;
};

std::vector<TestCase> test_cases{
{
.nums{3, 2, 1, 2, 3, 4},
.expected = 3,
},
{
.nums{3, 2, 6, 1, 4},
.expected = 2,
},
};

for (const auto &[nums, expected] : test_cases) {
const auto actual = Solution::maxOperations(nums);
REQUIRE(expected == actual);
}
}

0 comments on commit 5d9dcff

Please sign in to comment.