-
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
6 changed files
with
98 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#pragma once | ||
|
||
#include <ranges> | ||
#include <vector> | ||
|
||
// Time: O((M+N)logM) | ||
|
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_earliest_second_to_mark_indices_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,59 @@ | ||
#pragma once | ||
|
||
#include <numeric> | ||
#include <queue> | ||
#include <ranges> | ||
#include <vector> | ||
|
||
// Time: O((NlogN+M)logM) | ||
// Space: O(N) | ||
|
||
class Solution { | ||
public: | ||
static int | ||
earliestSecondToMarkIndices(const std::vector<int> &nums, | ||
const std::vector<int> &changeIndices) { | ||
const int m = changeIndices.size(); | ||
int left = 1, right = m + 1; | ||
while (left < right) { | ||
const auto middle = left + (right - left) / 2; | ||
canMark(nums, changeIndices, middle) ? right = middle : left = middle + 1; | ||
} | ||
return left == m + 1 ? -1 : left; | ||
} | ||
|
||
private: | ||
static bool canMark(const std::vector<int> &nums, | ||
const std::vector<int> &changeIndices, int m) { | ||
std::vector<int> first(nums.size(), -1); | ||
for (int i = m - 1; i >= 0; --i) { | ||
if (nums[changeIndices[i] - 1]) { | ||
first[changeIndices[i] - 1] = i; | ||
} | ||
} | ||
|
||
std::priority_queue<int, std::vector<int>, std::greater<>> minHeap; | ||
int ops = 0; | ||
for (int i = m - 1; i >= 0; --i) { | ||
if (i == first[changeIndices[i] - 1]) { | ||
minHeap.push(nums[changeIndices[i] - 1]); | ||
if (ops) { | ||
--ops; | ||
} else { | ||
++ops; | ||
minHeap.pop(); | ||
} | ||
} else { | ||
++ops; | ||
} | ||
} | ||
|
||
long long sum = std::reduce(nums.begin(), nums.end(), 0LL) + nums.size() - | ||
minHeap.size(); | ||
while (!minHeap.empty()) { | ||
sum -= minHeap.top(); | ||
minHeap.pop(); | ||
} | ||
return sum <= ops; | ||
} | ||
}; |
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,35 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
std::vector<int> nums; | ||
std::vector<int> changeIndices; | ||
int expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.nums{0, 0, 1, 2}, | ||
.changeIndices{1, 2, 1, 2, 1, 2, 1, 2}, | ||
.expected = 7, | ||
}, | ||
{ | ||
.nums{3, 2, 3}, | ||
.changeIndices{1, 3, 2, 2, 2, 2, 3}, | ||
.expected = 6, | ||
}, | ||
{ | ||
.nums{1, 2, 3}, | ||
.changeIndices{1, 2, 3}, | ||
.expected = -1, | ||
}, | ||
}; | ||
|
||
for (const auto &[nums, changeIndices, expected] : test_cases) { | ||
const auto actual = | ||
Solution::earliestSecondToMarkIndices(nums, changeIndices); | ||
REQUIRE(expected == actual); | ||
} | ||
} |