-
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.
Find The Number of Ways to Place People II
- Loading branch information
Showing
5 changed files
with
62 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
1 change: 1 addition & 0 deletions
1
solutions/find-the-number-of-ways-to-place-people-ii/CMakeLists.txt
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_find_the_number_of_ways_to_place_people_ii test.cpp) |
29 changes: 29 additions & 0 deletions
29
solutions/find-the-number-of-ways-to-place-people-ii/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,29 @@ | ||
#pragma once | ||
|
||
#include <climits> | ||
#include <ranges> | ||
#include <vector> | ||
|
||
// Time: O(N^2) | ||
// Space: O(N) | ||
|
||
class Solution { | ||
public: | ||
static int numberOfPairs(std::vector<std::vector<int>> points) { | ||
std::ranges::sort(points, [](auto &lhs, auto &rhs) { | ||
return std::tie(lhs[1], rhs[0]) > std::tie(rhs[1], lhs[0]); | ||
}); | ||
|
||
int ans = 0; | ||
for (size_t i = 0; i < points.size(); ++i) { | ||
auto minx = INT_MAX; | ||
for (size_t j = i + 1; j < points.size(); ++j) { | ||
if (points[j][0] >= points[i][0] && points[j][0] < minx) { | ||
++ans; | ||
minx = std::min(minx, points[j][0]); | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
30 changes: 30 additions & 0 deletions
30
solutions/find-the-number-of-ways-to-place-people-ii/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 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
std::vector<std::vector<int>> points; | ||
int expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.points{{1, 1}, {2, 2}, {3, 3}}, | ||
.expected = 0, | ||
}, | ||
{ | ||
.points{{6, 2}, {4, 4}, {2, 6}}, | ||
.expected = 2, | ||
}, | ||
{ | ||
.points{{3, 1}, {1, 3}, {1, 1}}, | ||
.expected = 2, | ||
}, | ||
}; | ||
|
||
for (const auto &[points, expected] : test_cases) { | ||
const auto actual = Solution::numberOfPairs(points); | ||
REQUIRE(expected == actual); | ||
} | ||
} |