Skip to content

Commit

Permalink
Number of Flowers in Full Bloom
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Oct 11, 2023
1 parent 4e52d41 commit 256d926
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ add_task(number-of-1-bits)
add_task(number-of-closed-islands)
add_task(number-of-digit-one)
add_task(number-of-enclaves)
add_task(number-of-flowers-in-full-bloom)
add_task(number-of-good-pairs)
add_task(number-of-increasing-paths-in-a-grid)
add_task(number-of-islands)
Expand Down
1 change: 1 addition & 0 deletions solutions/number-of-flowers-in-full-bloom/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_number_of_flowers_in_full_bloom test.cpp)
30 changes: 30 additions & 0 deletions solutions/number-of-flowers-in-full-bloom/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <algorithm>
#include <vector>

class Solution {
public:
static std::vector<int>
fullBloomFlowers(const std::vector<std::vector<int>> &flowers,
const std::vector<int> &people) {
std::vector<int> starts(flowers.size());
std::vector<int> ends(flowers.size());
for (size_t i = 0; i < flowers.size(); ++i) {
starts[i] = flowers[i][0];
ends[i] = flowers[i][1];
}

std::sort(starts.begin(), starts.end());
std::sort(ends.begin(), ends.end());

std::vector<int> ans;
for (auto person : people) {
auto ub = std::upper_bound(starts.begin(), starts.end(), person);
auto lb = std::lower_bound(ends.begin(), ends.end(), person);
ans.push_back(std::distance(starts.begin(), ub) -
std::distance(ends.begin(), lb));
}
return ans;
}
};
18 changes: 18 additions & 0 deletions solutions/number-of-flowers-in-full-bloom/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <catch.hpp>

#include <solution.hpp>

TEST_CASE("Simple") {
{
std::vector<std::vector<int>> flowers{{1, 6}, {3, 7}, {9, 12}, {4, 13}};
std::vector<int> people{2, 3, 7, 11};
std::vector<int> expected{1, 2, 2, 2};
REQUIRE(expected == Solution::fullBloomFlowers(flowers, people));
}
{
std::vector<std::vector<int>> flowers{{1, 10}, {3, 3}};
std::vector<int> people{3, 3, 2};
std::vector<int> expected{2, 2, 1};
REQUIRE(expected == Solution::fullBloomFlowers(flowers, people));
}
}

0 comments on commit 256d926

Please sign in to comment.