-
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.
Eliminate Maximum Number of Monsters
- Loading branch information
Showing
5 changed files
with
111 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
add_catch(test_eliminate_maximum_number_of_monsters test.cpp) |
65 changes: 65 additions & 0 deletions
65
solutions/eliminate-maximum-number-of-monsters/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,65 @@ | ||
#pragma once | ||
|
||
#include <numeric> | ||
#include <ranges> | ||
#include <vector> | ||
|
||
// Time: O(N) | ||
// Space: O(N) | ||
|
||
namespace sorting { | ||
|
||
// Time: O(NlogN) | ||
// Space: O(N) | ||
class Solution { | ||
public: | ||
static int eliminateMaximum(const std::vector<int> &dist, | ||
const std::vector<int> &speed) { | ||
const int n = dist.size(); | ||
|
||
std::vector<int> arrival(n); | ||
for (int i = 0; i < n; ++i) { | ||
arrival[i] = (dist[i] + speed[i] - 1) / speed[i]; | ||
} | ||
std::ranges::sort(arrival); | ||
|
||
for (int i = 0; i < n; ++i) { | ||
if (i >= arrival[i]) { | ||
return i; | ||
} | ||
} | ||
return n; | ||
} | ||
}; | ||
|
||
} // namespace sorting | ||
|
||
namespace counting { | ||
|
||
// Time: O(N) | ||
// Space: O(N) | ||
class Solution { | ||
public: | ||
static int eliminateMaximum(const std::vector<int> &dist, | ||
const std::vector<int> &speed) { | ||
const int n = dist.size(); | ||
|
||
std::vector<int> counter(n); | ||
for (int i = 0; i < n; ++i) { | ||
const auto arrival = (dist[i] + speed[i] - 1) / speed[i]; | ||
if (arrival < n) { | ||
++counter[arrival]; | ||
} | ||
} | ||
std::partial_sum(counter.cbegin(), counter.cend(), counter.begin()); | ||
|
||
for (int i = 0; i < n; ++i) { | ||
if (counter[i] > i) { | ||
return i; | ||
} | ||
} | ||
return n; | ||
} | ||
}; | ||
|
||
} // namespace counting |
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,43 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
std::vector<int> dist; | ||
std::vector<int> speed; | ||
int expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.dist{1, 3, 4}, | ||
.speed{1, 1, 1}, | ||
.expected = 3, | ||
}, | ||
{ | ||
.dist{1, 1, 2, 3}, | ||
.speed{1, 1, 1, 1}, | ||
.expected = 1, | ||
}, | ||
{ | ||
.dist{3, 2, 4}, | ||
.speed{5, 3, 2}, | ||
.expected = 1, | ||
}, | ||
}; | ||
|
||
SECTION("Sorting") { | ||
for (const auto &[dist, speed, expected] : test_cases) { | ||
const auto actual = sorting::Solution::eliminateMaximum(dist, speed); | ||
REQUIRE(expected == actual); | ||
} | ||
} | ||
|
||
SECTION("Counting") { | ||
for (const auto &[dist, speed, expected] : test_cases) { | ||
const auto actual = counting::Solution::eliminateMaximum(dist, speed); | ||
REQUIRE(expected == actual); | ||
} | ||
} | ||
} |