diff --git a/solution/3000-3099/3025.Find the Number of Ways to Place People I/README.md b/solution/3000-3099/3025.Find the Number of Ways to Place People I/README.md index c4ab35fc0be59..8b7eca4c2c980 100644 --- a/solution/3000-3099/3025.Find the Number of Ways to Place People I/README.md +++ b/solution/3000-3099/3025.Find the Number of Ways to Place People I/README.md @@ -73,24 +73,120 @@ ## 解法 -### 方法一 +### 方法一:排序 + 枚举 + +我们不妨考虑枚举矩形左上角的点 $(x_1, y_1)$,那么根据题目,右下角的点 $(x_2, y_2)$ 随着 $x$ 的增大,纵坐标 $y$ 也会要严格增大,才符合题意。 + +因此,我们对所有点按照 $x$ 坐标升序排序,如果 $x$ 坐标相同,按照 $y$ 坐标降序排序。 + +然后我们枚举左上角的点 $(x_1, y_1)$,并且维护一个最大的 $y_2$,记为 $maxY$,表示所有右下角的点的纵坐标的最大值。然后我们枚举右下角的点 $(x_2, y_2)$,如果 $y_2$ 大于 $maxY$ 并且小于等于 $y_1$,那么我们就找到了一个合法的方案,将答案加一,然后更新 $maxY$ 为 $y_2$。 + +枚举完所有的点对后,我们就得到了答案。 + +时间复杂度 $O(n^2)$,空间复杂度 $O(\log n)$。其中 $n$ 是点的数量。 ```python - +class Solution: + def numberOfPairs(self, points: List[List[int]]) -> int: + points.sort(key=lambda x: (x[0], -x[1])) + ans = 0 + for i, (_, y1) in enumerate(points): + max_y = -inf + for _, y2 in points[i + 1 :]: + if max_y < y2 <= y1: + max_y = y2 + ans += 1 + return ans ``` ```java - +class Solution { + public int numberOfPairs(int[][] points) { + Arrays.sort(points, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); + int ans = 0; + int n = points.length; + final int inf = 1 << 30; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = -inf; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int numberOfPairs(vector>& points) { + sort(points.begin(), points.end(), [](const vector& a, const vector& b) { + return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1]); + }); + int n = points.size(); + int ans = 0; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = INT_MIN; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +}; ``` ```go +func numberOfPairs(points [][]int) (ans int) { + sort.Slice(points, func(i, j int) bool { + return points[i][0] < points[j][0] || points[i][0] == points[j][0] && points[j][1] < points[i][1] + }) + for i, p1 := range points { + y1 := p1[1] + maxY := math.MinInt32 + for _, p2 := range points[i+1:] { + y2 := p2[1] + if maxY < y2 && y2 <= y1 { + maxY = y2 + ans++ + } + } + } + return +} +``` +```ts +function numberOfPairs(points: number[][]): number { + points.sort((a, b) => (a[0] === b[0] ? b[1] - a[1] : a[0] - b[0])); + const n = points.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + const [_, y1] = points[i]; + let maxY = -Infinity; + for (let j = i + 1; j < n; ++j) { + const [_, y2] = points[j]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; +} ``` diff --git a/solution/3000-3099/3025.Find the Number of Ways to Place People I/README_EN.md b/solution/3000-3099/3025.Find the Number of Ways to Place People I/README_EN.md index b677201d12cb8..2fa0b479103e6 100644 --- a/solution/3000-3099/3025.Find the Number of Ways to Place People I/README_EN.md +++ b/solution/3000-3099/3025.Find the Number of Ways to Place People I/README_EN.md @@ -63,24 +63,119 @@ Note that it does not matter if the fence encloses any area, the first and secon ## Solutions -### Solution 1 +### Solution 1: Sorting and Classification + +First, we sort the array. Then, we can classify the results based on the properties of a triangle. + +- If the sum of the two smaller numbers is less than or equal to the largest number, it cannot form a triangle. Return "Invalid". +- If the three numbers are equal, it is an equilateral triangle. Return "Equilateral". +- If two numbers are equal, it is an isosceles triangle. Return "Isosceles". +- If none of the above conditions are met, it is a scalene triangle. Return "Scalene". + +The time complexity is $O(1)$, and the space complexity is $O(1)$. ```python - +class Solution: + def numberOfPairs(self, points: List[List[int]]) -> int: + points.sort(key=lambda x: (x[0], -x[1])) + ans = 0 + for i, (_, y1) in enumerate(points): + max_y = -inf + for _, y2 in points[i + 1 :]: + if max_y < y2 <= y1: + max_y = y2 + ans += 1 + return ans ``` ```java - +class Solution { + public int numberOfPairs(int[][] points) { + Arrays.sort(points, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); + int ans = 0; + int n = points.length; + final int inf = 1 << 30; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = -inf; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int numberOfPairs(vector>& points) { + sort(points.begin(), points.end(), [](const vector& a, const vector& b) { + return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1]); + }); + int n = points.size(); + int ans = 0; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = INT_MIN; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +}; ``` ```go +func numberOfPairs(points [][]int) (ans int) { + sort.Slice(points, func(i, j int) bool { + return points[i][0] < points[j][0] || points[i][0] == points[j][0] && points[j][1] < points[i][1] + }) + for i, p1 := range points { + y1 := p1[1] + maxY := math.MinInt32 + for _, p2 := range points[i+1:] { + y2 := p2[1] + if maxY < y2 && y2 <= y1 { + maxY = y2 + ans++ + } + } + } + return +} +``` +```ts +function numberOfPairs(points: number[][]): number { + points.sort((a, b) => (a[0] === b[0] ? b[1] - a[1] : a[0] - b[0])); + const n = points.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + const [_, y1] = points[i]; + let maxY = -Infinity; + for (let j = i + 1; j < n; ++j) { + const [_, y2] = points[j]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; +} ``` diff --git a/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.cpp b/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.cpp new file mode 100644 index 0000000000000..2983afa7a1539 --- /dev/null +++ b/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int numberOfPairs(vector>& points) { + sort(points.begin(), points.end(), [](const vector& a, const vector& b) { + return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1]); + }); + int n = points.size(); + int ans = 0; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = INT_MIN; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.go b/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.go new file mode 100644 index 0000000000000..a6bbb440a0289 --- /dev/null +++ b/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.go @@ -0,0 +1,17 @@ +func numberOfPairs(points [][]int) (ans int) { + sort.Slice(points, func(i, j int) bool { + return points[i][0] < points[j][0] || points[i][0] == points[j][0] && points[j][1] < points[i][1] + }) + for i, p1 := range points { + y1 := p1[1] + maxY := math.MinInt32 + for _, p2 := range points[i+1:] { + y2 := p2[1] + if maxY < y2 && y2 <= y1 { + maxY = y2 + ans++ + } + } + } + return +} \ No newline at end of file diff --git a/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.java b/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.java new file mode 100644 index 0000000000000..8fb18163d4d16 --- /dev/null +++ b/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.java @@ -0,0 +1,20 @@ +class Solution { + public int numberOfPairs(int[][] points) { + Arrays.sort(points, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); + int ans = 0; + int n = points.length; + final int inf = 1 << 30; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = -inf; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.py b/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.py new file mode 100644 index 0000000000000..e0021c5699dd7 --- /dev/null +++ b/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.py @@ -0,0 +1,11 @@ +class Solution: + def numberOfPairs(self, points: List[List[int]]) -> int: + points.sort(key=lambda x: (x[0], -x[1])) + ans = 0 + for i, (_, y1) in enumerate(points): + max_y = -inf + for _, y2 in points[i + 1 :]: + if max_y < y2 <= y1: + max_y = y2 + ans += 1 + return ans diff --git a/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.ts b/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.ts new file mode 100644 index 0000000000000..7961430502d42 --- /dev/null +++ b/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.ts @@ -0,0 +1,17 @@ +function numberOfPairs(points: number[][]): number { + points.sort((a, b) => (a[0] === b[0] ? b[1] - a[1] : a[0] - b[0])); + const n = points.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + const [_, y1] = points[i]; + let maxY = -Infinity; + for (let j = i + 1; j < n; ++j) { + const [_, y2] = points[j]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; +} diff --git a/solution/3000-3099/3027.Find the Number of Ways to Place People II/README.md b/solution/3000-3099/3027.Find the Number of Ways to Place People II/README.md index 8b52bc8d8a55c..9f43bb31f3e0f 100644 --- a/solution/3000-3099/3027.Find the Number of Ways to Place People II/README.md +++ b/solution/3000-3099/3027.Find the Number of Ways to Place People II/README.md @@ -73,24 +73,120 @@ ## 解法 -### 方法一 +### 方法一:排序 + 枚举 + +我们不妨考虑枚举矩形左上角的点 $(x_1, y_1)$,那么根据题目,右下角的点 $(x_2, y_2)$ 随着 $x$ 的增大,纵坐标 $y$ 也会要严格增大,才符合题意。 + +因此,我们对所有点按照 $x$ 坐标升序排序,如果 $x$ 坐标相同,按照 $y$ 坐标降序排序。 + +然后我们枚举左上角的点 $(x_1, y_1)$,并且维护一个最大的 $y_2$,记为 $maxY$,表示所有右下角的点的纵坐标的最大值。然后我们枚举右下角的点 $(x_2, y_2)$,如果 $y_2$ 大于 $maxY$ 并且小于等于 $y_1$,那么我们就找到了一个合法的方案,将答案加一,然后更新 $maxY$ 为 $y_2$。 + +枚举完所有的点对后,我们就得到了答案。 + +时间复杂度 $O(n^2)$,空间复杂度 $O(\log n)$。其中 $n$ 是点的数量。 ```python - +class Solution: + def numberOfPairs(self, points: List[List[int]]) -> int: + points.sort(key=lambda x: (x[0], -x[1])) + ans = 0 + for i, (_, y1) in enumerate(points): + max_y = -inf + for _, y2 in points[i + 1 :]: + if max_y < y2 <= y1: + max_y = y2 + ans += 1 + return ans ``` ```java - +class Solution { + public int numberOfPairs(int[][] points) { + Arrays.sort(points, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); + int ans = 0; + int n = points.length; + final int inf = 1 << 30; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = -inf; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int numberOfPairs(vector>& points) { + sort(points.begin(), points.end(), [](const vector& a, const vector& b) { + return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1]); + }); + int n = points.size(); + int ans = 0; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = INT_MIN; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +}; ``` ```go +func numberOfPairs(points [][]int) (ans int) { + sort.Slice(points, func(i, j int) bool { + return points[i][0] < points[j][0] || points[i][0] == points[j][0] && points[j][1] < points[i][1] + }) + for i, p1 := range points { + y1 := p1[1] + maxY := math.MinInt32 + for _, p2 := range points[i+1:] { + y2 := p2[1] + if maxY < y2 && y2 <= y1 { + maxY = y2 + ans++ + } + } + } + return +} +``` +```ts +function numberOfPairs(points: number[][]): number { + points.sort((a, b) => (a[0] === b[0] ? b[1] - a[1] : a[0] - b[0])); + const n = points.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + const [_, y1] = points[i]; + let maxY = -Infinity; + for (let j = i + 1; j < n; ++j) { + const [_, y2] = points[j]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; +} ``` diff --git a/solution/3000-3099/3027.Find the Number of Ways to Place People II/README_EN.md b/solution/3000-3099/3027.Find the Number of Ways to Place People II/README_EN.md index 3938b6436b0d0..26a93bebcd7c8 100644 --- a/solution/3000-3099/3027.Find the Number of Ways to Place People II/README_EN.md +++ b/solution/3000-3099/3027.Find the Number of Ways to Place People II/README_EN.md @@ -63,24 +63,119 @@ Note that it does not matter if the fence encloses any area, the first and secon ## Solutions -### Solution 1 +### Solution 1: Sorting and Classification + +First, we sort the array. Then, we can classify the results based on the properties of a triangle. + +- If the sum of the two smaller numbers is less than or equal to the largest number, it cannot form a triangle. Return "Invalid". +- If the three numbers are equal, it is an equilateral triangle. Return "Equilateral". +- If two numbers are equal, it is an isosceles triangle. Return "Isosceles". +- If none of the above conditions are met, it is a scalene triangle. Return "Scalene". + +The time complexity is $O(1)$, and the space complexity is $O(1)$. ```python - +class Solution: + def numberOfPairs(self, points: List[List[int]]) -> int: + points.sort(key=lambda x: (x[0], -x[1])) + ans = 0 + for i, (_, y1) in enumerate(points): + max_y = -inf + for _, y2 in points[i + 1 :]: + if max_y < y2 <= y1: + max_y = y2 + ans += 1 + return ans ``` ```java - +class Solution { + public int numberOfPairs(int[][] points) { + Arrays.sort(points, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); + int ans = 0; + int n = points.length; + final int inf = 1 << 30; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = -inf; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int numberOfPairs(vector>& points) { + sort(points.begin(), points.end(), [](const vector& a, const vector& b) { + return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1]); + }); + int n = points.size(); + int ans = 0; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = INT_MIN; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +}; ``` ```go +func numberOfPairs(points [][]int) (ans int) { + sort.Slice(points, func(i, j int) bool { + return points[i][0] < points[j][0] || points[i][0] == points[j][0] && points[j][1] < points[i][1] + }) + for i, p1 := range points { + y1 := p1[1] + maxY := math.MinInt32 + for _, p2 := range points[i+1:] { + y2 := p2[1] + if maxY < y2 && y2 <= y1 { + maxY = y2 + ans++ + } + } + } + return +} +``` +```ts +function numberOfPairs(points: number[][]): number { + points.sort((a, b) => (a[0] === b[0] ? b[1] - a[1] : a[0] - b[0])); + const n = points.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + const [_, y1] = points[i]; + let maxY = -Infinity; + for (let j = i + 1; j < n; ++j) { + const [_, y2] = points[j]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; +} ``` diff --git a/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.cpp b/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.cpp new file mode 100644 index 0000000000000..2983afa7a1539 --- /dev/null +++ b/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int numberOfPairs(vector>& points) { + sort(points.begin(), points.end(), [](const vector& a, const vector& b) { + return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1]); + }); + int n = points.size(); + int ans = 0; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = INT_MIN; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.go b/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.go new file mode 100644 index 0000000000000..a6bbb440a0289 --- /dev/null +++ b/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.go @@ -0,0 +1,17 @@ +func numberOfPairs(points [][]int) (ans int) { + sort.Slice(points, func(i, j int) bool { + return points[i][0] < points[j][0] || points[i][0] == points[j][0] && points[j][1] < points[i][1] + }) + for i, p1 := range points { + y1 := p1[1] + maxY := math.MinInt32 + for _, p2 := range points[i+1:] { + y2 := p2[1] + if maxY < y2 && y2 <= y1 { + maxY = y2 + ans++ + } + } + } + return +} \ No newline at end of file diff --git a/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.java b/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.java new file mode 100644 index 0000000000000..8fb18163d4d16 --- /dev/null +++ b/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.java @@ -0,0 +1,20 @@ +class Solution { + public int numberOfPairs(int[][] points) { + Arrays.sort(points, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); + int ans = 0; + int n = points.length; + final int inf = 1 << 30; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = -inf; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.py b/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.py new file mode 100644 index 0000000000000..e0021c5699dd7 --- /dev/null +++ b/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.py @@ -0,0 +1,11 @@ +class Solution: + def numberOfPairs(self, points: List[List[int]]) -> int: + points.sort(key=lambda x: (x[0], -x[1])) + ans = 0 + for i, (_, y1) in enumerate(points): + max_y = -inf + for _, y2 in points[i + 1 :]: + if max_y < y2 <= y1: + max_y = y2 + ans += 1 + return ans diff --git a/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.ts b/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.ts new file mode 100644 index 0000000000000..7961430502d42 --- /dev/null +++ b/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.ts @@ -0,0 +1,17 @@ +function numberOfPairs(points: number[][]): number { + points.sort((a, b) => (a[0] === b[0] ? b[1] - a[1] : a[0] - b[0])); + const n = points.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + const [_, y1] = points[i]; + let maxY = -Infinity; + for (let j = i + 1; j < n; ++j) { + const [_, y2] = points[j]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; +}