Skip to content

Commit

Permalink
Find The Number of Ways to Place People II
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Feb 3, 2024
1 parent 09c3c75 commit f3e9b32
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ add_task(find-the-middle-index-in-array)
add_task(find-the-minimum-and-maximum-number-of-nodes-between-critical-points)
add_task(find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k)
add_task(find-the-number-of-ways-to-place-people-i)
add_task(find-the-number-of-ways-to-place-people-ii)
add_task(find-the-original-array-of-prefix-xor)
add_task(find-the-peaks)
add_task(find-the-pivot-integer)
Expand Down
1 change: 1 addition & 0 deletions PROBLEM_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -1864,3 +1864,4 @@
| 3024. | [Type of Triangle II](https://leetcode.com/problems/type-of-triangle-ii/) | [C++](./solutions/type-of-triangle-ii/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | O(1) / O(1)| | |
| 3025. | [Find the Number of Ways to Place People I](https://leetcode.com/problems/find-the-number-of-ways-to-place-people-i/) | [C++](./solutions/find-the-number-of-ways-to-place-people-i/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N<sup>3</sup>) / O(1)| | |
| 3026. | [Maximum Good Subarray Sum](https://leetcode.com/problems/maximum-good-subarray-sum/) | [C++](./solutions/maximum-good-subarray-sum/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(N)| | |
| 3027. | [Find the Number of Ways to Place People II](https://leetcode.com/problems/find-the-number-of-ways-to-place-people-ii/) | [C++](./solutions/find-the-number-of-ways-to-place-people-ii/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(N<sup>2</sup>) / O(N)| | |
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 solutions/find-the-number-of-ways-to-place-people-ii/solution.hpp
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 solutions/find-the-number-of-ways-to-place-people-ii/test.cpp
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);
}
}

0 comments on commit f3e9b32

Please sign in to comment.