-
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.
Maximum Number of Operations With the Same Score II
- Loading branch information
Showing
5 changed files
with
74 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
1 change: 1 addition & 0 deletions
1
solutions/maximum-number-of-operations-with-the-same-score-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_maximum_number_of_operations_with_the_same_score_ii test.cpp) |
45 changes: 45 additions & 0 deletions
45
solutions/maximum-number-of-operations-with-the-same-score-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,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; | ||
} | ||
}; |
26 changes: 26 additions & 0 deletions
26
solutions/maximum-number-of-operations-with-the-same-score-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,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); | ||
} | ||
} |