Skip to content

Commit

Permalink
Eliminate Maximum Number of Monsters
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Nov 7, 2023
1 parent 5854363 commit 81e6fdf
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ add_task(duplicate-zeros)
add_task(earliest-possible-day-of-full-bloom)
add_task(edit-distance)
add_task(element-appearing-more-than-25-in-sorted-array)
add_task(eliminate-maximum-number-of-monsters)
add_task(equal-row-and-column-pairs)
add_task(evaluate-boolean-binary-tree)
add_task(evaluate-division)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ leetcode pull -d
| 1909. | [Remove One Element to Make the Array Strictly Increasing](https://leetcode.com/problems/remove-one-element-to-make-the-array-strictly-increasing/) | [C++](./solutions/remove-one-element-to-make-the-array-strictly-increasing/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> | |
| 1913. | [Maximum Product Difference Between Two Pairs](https://leetcode.com/problems/maximum-product-difference-between-two-pairs/) | [C++](./solutions/maximum-product-difference-between-two-pairs/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> | |
| 1920. | [Build Array from Permutation](https://leetcode.com/problems/build-array-from-permutation/) | [C++](./solutions/build-array-from-permutation/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Simulation-7e7f33?style=flat-square'/> | |
| 1921. | [Eliminate Maximum Number of Monsters](https://leetcode.com/problems/eliminate-maximum-number-of-monsters/) | [C++](./solutions/eliminate-maximum-number-of-monsters/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> | |
| 1925. | [Count Square Sum Triples](https://leetcode.com/problems/count-square-sum-triples/) | [C++](./solutions/count-square-sum-triples/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> <img src='https://img.shields.io/badge/Enumeration-7f7a33?style=flat-square'/> | |
| 1926. | [Nearest Exit from Entrance in Maze](https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/) | [C++](./solutions/nearest-exit-from-entrance-in-maze/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Breadth First Search-377f33?style=flat-square'/> <img src='https://img.shields.io/badge/Matrix-5e337f?style=flat-square'/> | |
| 1929. | [Concatenation of Array](https://leetcode.com/problems/concatenation-of-array/) | [C++](./solutions/concatenation-of-array/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> | |
Expand Down
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 solutions/eliminate-maximum-number-of-monsters/solution.hpp
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
43 changes: 43 additions & 0 deletions solutions/eliminate-maximum-number-of-monsters/test.cpp
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);
}
}
}

0 comments on commit 81e6fdf

Please sign in to comment.