diff --git a/CMakeLists.txt b/CMakeLists.txt index 18b4f2b1..eb7a4d91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/PROBLEM_LIST.md b/PROBLEM_LIST.md index bb7bfa3a..a9a5e5e8 100644 --- a/PROBLEM_LIST.md +++ b/PROBLEM_LIST.md @@ -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) | | 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) | | O(N3) / O(1)| | | | 3026. | [Maximum Good Subarray Sum](https://leetcode.com/problems/maximum-good-subarray-sum/) | [C++](./solutions/maximum-good-subarray-sum/solution.hpp) | | 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) | | O(N2) / O(N)| | | diff --git a/solutions/find-the-number-of-ways-to-place-people-ii/CMakeLists.txt b/solutions/find-the-number-of-ways-to-place-people-ii/CMakeLists.txt new file mode 100644 index 00000000..895e1b28 --- /dev/null +++ b/solutions/find-the-number-of-ways-to-place-people-ii/CMakeLists.txt @@ -0,0 +1 @@ +add_catch(test_find_the_number_of_ways_to_place_people_ii test.cpp) diff --git a/solutions/find-the-number-of-ways-to-place-people-ii/solution.hpp b/solutions/find-the-number-of-ways-to-place-people-ii/solution.hpp new file mode 100644 index 00000000..cc52a03b --- /dev/null +++ b/solutions/find-the-number-of-ways-to-place-people-ii/solution.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include + +// Time: O(N^2) +// Space: O(N) + +class Solution { +public: + static int numberOfPairs(std::vector> 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; + } +}; diff --git a/solutions/find-the-number-of-ways-to-place-people-ii/test.cpp b/solutions/find-the-number-of-ways-to-place-people-ii/test.cpp new file mode 100644 index 00000000..fee59556 --- /dev/null +++ b/solutions/find-the-number-of-ways-to-place-people-ii/test.cpp @@ -0,0 +1,30 @@ +#include + +#include + +TEST_CASE("Simple") { + struct TestCase { + std::vector> points; + int expected; + }; + + std::vector 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); + } +}