Skip to content

Commit

Permalink
Earliest Second to Mark Indices I
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Feb 26, 2024
1 parent 44d13f4 commit feb47d6
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ add_task(double-modular-exponentiation)
add_task(dungeon-game)
add_task(duplicate-zeros)
add_task(earliest-possible-day-of-full-bloom)
add_task(earliest-second-to-mark-indices-i)
add_task(edit-distance)
add_task(egg-drop-with-2-eggs-and-n-floors)
add_task(element-appearing-more-than-25-in-sorted-array)
Expand Down
1 change: 1 addition & 0 deletions PROBLEM_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -1891,3 +1891,4 @@
| 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)| | |
| 3046. | [Split the Array](https://leetcode.com/problems/split-the-array/) | [C++](./solutions/split-the-array/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | O(N) / O(N)| | |
| 3047. | [Find the Largest Area of Square Inside Two Rectangles](https://leetcode.com/problems/find-the-largest-area-of-square-inside-two-rectangles/) | [C++](./solutions/find-the-largest-area-of-square-inside-two-rectangles/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N<sup>2</sup>) / O(1)| | |
| 3048. | [Earliest Second to Mark Indices I](https://leetcode.com/problems/earliest-second-to-mark-indices-i/) | [C++](./solutions/earliest-second-to-mark-indices-i/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O((M+N)logM) / O(N)| | |
1 change: 1 addition & 0 deletions solutions/earliest-second-to-mark-indices-i/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_earliest_second_to_mark_indices_i test.cpp)
47 changes: 47 additions & 0 deletions solutions/earliest-second-to-mark-indices-i/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include <vector>

// Time: O((M+N)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> last(nums.size(), -1);
for (int i = 0; i < m; ++i) {
last[changeIndices[i] - 1] = i;
}

if (std::ranges::any_of(last, [](int a) { return a == -1; })) {
return false;
}

int ops = 0;
for (int i = 0; i < m; ++i) {
if (i == last[changeIndices[i] - 1]) {
if (ops < nums[changeIndices[i] - 1]) {
return false;
}
ops -= nums[changeIndices[i] - 1];
} else {
++ops;
}
}
return true;
}
};
35 changes: 35 additions & 0 deletions solutions/earliest-second-to-mark-indices-i/test.cpp
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{1, 3},
.changeIndices{1, 1, 1, 2, 1, 1, 1},
.expected = 6,
},
{
.nums{2, 2, 0},
.changeIndices{2, 2, 2, 2, 3, 2, 2, 1},
.expected = 8,
},
{
.nums{0, 1},
.changeIndices{2, 2, 2},
.expected = -1,
},
};

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

0 comments on commit feb47d6

Please sign in to comment.