-
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
4 changed files
with
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_catch(test_number_of_flowers_in_full_bloom 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,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; | ||
} | ||
}; |
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,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)); | ||
} | ||
} |