Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fae11cf

Browse files
feat: add solutions to lc problems: No.3025,3027 (doocs#2311)
1 parent 745dd62 commit fae11cf

File tree

14 files changed

+572
-16
lines changed

14 files changed

+572
-16
lines changed

‎solution/3000-3099/3025.Find the Number of Ways to Place People I/README.md‎

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,120 @@
7373

7474
## 解法
7575

76-
### 方法一
76+
### 方法一:排序 + 枚举
77+
78+
我们不妨考虑枚举矩形左上角的点 $(x_1, y_1),ドル那么根据题目,右下角的点 $(x_2, y_2)$ 随着 $x$ 的增大,纵坐标 $y$ 也会要严格增大,才符合题意。
79+
80+
因此,我们对所有点按照 $x$ 坐标升序排序,如果 $x$ 坐标相同,按照 $y$ 坐标降序排序。
81+
82+
然后我们枚举左上角的点 $(x_1, y_1),ドル并且维护一个最大的 $y_2,ドル记为 $maxY,ドル表示所有右下角的点的纵坐标的最大值。然后我们枚举右下角的点 $(x_2, y_2),ドル如果 $y_2$ 大于 $maxY$ 并且小于等于 $y_1,ドル那么我们就找到了一个合法的方案,将答案加一,然后更新 $maxY$ 为 $y_2$。
83+
84+
枚举完所有的点对后,我们就得到了答案。
85+
86+
时间复杂度 $O(n^2),ドル空间复杂度 $O(\log n)$。其中 $n$ 是点的数量。
7787

7888
<!-- tabs:start -->
7989

8090
```python
81-
91+
class Solution:
92+
def numberOfPairs(self, points: List[List[int]]) -> int:
93+
points.sort(key=lambda x: (x[0], -x[1]))
94+
ans = 0
95+
for i, (_, y1) in enumerate(points):
96+
max_y = -inf
97+
for _, y2 in points[i + 1 :]:
98+
if max_y < y2 <= y1:
99+
max_y = y2
100+
ans += 1
101+
return ans
82102
```
83103

84104
```java
85-
105+
class Solution {
106+
public int numberOfPairs(int[][] points) {
107+
Arrays.sort(points, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
108+
int ans = 0;
109+
int n = points.length;
110+
final int inf = 1 << 30;
111+
for (int i = 0; i < n; ++i) {
112+
int y1 = points[i][1];
113+
int maxY = -inf;
114+
for (int j = i + 1; j < n; ++j) {
115+
int y2 = points[j][1];
116+
if (maxY < y2 && y2 <= y1) {
117+
maxY = y2;
118+
++ans;
119+
}
120+
}
121+
}
122+
return ans;
123+
}
124+
}
86125
```
87126

88127
```cpp
89-
128+
class Solution {
129+
public:
130+
int numberOfPairs(vector<vector<int>>& points) {
131+
sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int>& b) {
132+
return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1]);
133+
});
134+
int n = points.size();
135+
int ans = 0;
136+
for (int i = 0; i < n; ++i) {
137+
int y1 = points[i][1];
138+
int maxY = INT_MIN;
139+
for (int j = i + 1; j < n; ++j) {
140+
int y2 = points[j][1];
141+
if (maxY < y2 && y2 <= y1) {
142+
maxY = y2;
143+
++ans;
144+
}
145+
}
146+
}
147+
return ans;
148+
}
149+
};
90150
```
91151
92152
```go
153+
func numberOfPairs(points [][]int) (ans int) {
154+
sort.Slice(points, func(i, j int) bool {
155+
return points[i][0] < points[j][0] || points[i][0] == points[j][0] && points[j][1] < points[i][1]
156+
})
157+
for i, p1 := range points {
158+
y1 := p1[1]
159+
maxY := math.MinInt32
160+
for _, p2 := range points[i+1:] {
161+
y2 := p2[1]
162+
if maxY < y2 && y2 <= y1 {
163+
maxY = y2
164+
ans++
165+
}
166+
}
167+
}
168+
return
169+
}
170+
```
93171

172+
```ts
173+
function numberOfPairs(points: number[][]): number {
174+
points.sort((a, b) => (a[0] === b[0] ? b[1] - a[1] : a[0] - b[0]));
175+
const n = points.length;
176+
let ans = 0;
177+
for (let i = 0; i < n; ++i) {
178+
const [_, y1] = points[i];
179+
let maxY = -Infinity;
180+
for (let j = i + 1; j < n; ++j) {
181+
const [_, y2] = points[j];
182+
if (maxY < y2 && y2 <= y1) {
183+
maxY = y2;
184+
++ans;
185+
}
186+
}
187+
}
188+
return ans;
189+
}
94190
```
95191

96192
<!-- tabs:end -->

‎solution/3000-3099/3025.Find the Number of Ways to Place People I/README_EN.md‎

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,119 @@ Note that it does not matter if the fence encloses any area, the first and secon
6363

6464
## Solutions
6565

66-
### Solution 1
66+
### Solution 1: Sorting and Classification
67+
68+
First, we sort the array. Then, we can classify the results based on the properties of a triangle.
69+
70+
- If the sum of the two smaller numbers is less than or equal to the largest number, it cannot form a triangle. Return "Invalid".
71+
- If the three numbers are equal, it is an equilateral triangle. Return "Equilateral".
72+
- If two numbers are equal, it is an isosceles triangle. Return "Isosceles".
73+
- If none of the above conditions are met, it is a scalene triangle. Return "Scalene".
74+
75+
The time complexity is $O(1),ドル and the space complexity is $O(1)$.
6776

6877
<!-- tabs:start -->
6978

7079
```python
71-
80+
class Solution:
81+
def numberOfPairs(self, points: List[List[int]]) -> int:
82+
points.sort(key=lambda x: (x[0], -x[1]))
83+
ans = 0
84+
for i, (_, y1) in enumerate(points):
85+
max_y = -inf
86+
for _, y2 in points[i + 1 :]:
87+
if max_y < y2 <= y1:
88+
max_y = y2
89+
ans += 1
90+
return ans
7291
```
7392

7493
```java
75-
94+
class Solution {
95+
public int numberOfPairs(int[][] points) {
96+
Arrays.sort(points, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
97+
int ans = 0;
98+
int n = points.length;
99+
final int inf = 1 << 30;
100+
for (int i = 0; i < n; ++i) {
101+
int y1 = points[i][1];
102+
int maxY = -inf;
103+
for (int j = i + 1; j < n; ++j) {
104+
int y2 = points[j][1];
105+
if (maxY < y2 && y2 <= y1) {
106+
maxY = y2;
107+
++ans;
108+
}
109+
}
110+
}
111+
return ans;
112+
}
113+
}
76114
```
77115

78116
```cpp
79-
117+
class Solution {
118+
public:
119+
int numberOfPairs(vector<vector<int>>& points) {
120+
sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int>& b) {
121+
return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1]);
122+
});
123+
int n = points.size();
124+
int ans = 0;
125+
for (int i = 0; i < n; ++i) {
126+
int y1 = points[i][1];
127+
int maxY = INT_MIN;
128+
for (int j = i + 1; j < n; ++j) {
129+
int y2 = points[j][1];
130+
if (maxY < y2 && y2 <= y1) {
131+
maxY = y2;
132+
++ans;
133+
}
134+
}
135+
}
136+
return ans;
137+
}
138+
};
80139
```
81140
82141
```go
142+
func numberOfPairs(points [][]int) (ans int) {
143+
sort.Slice(points, func(i, j int) bool {
144+
return points[i][0] < points[j][0] || points[i][0] == points[j][0] && points[j][1] < points[i][1]
145+
})
146+
for i, p1 := range points {
147+
y1 := p1[1]
148+
maxY := math.MinInt32
149+
for _, p2 := range points[i+1:] {
150+
y2 := p2[1]
151+
if maxY < y2 && y2 <= y1 {
152+
maxY = y2
153+
ans++
154+
}
155+
}
156+
}
157+
return
158+
}
159+
```
83160

161+
```ts
162+
function numberOfPairs(points: number[][]): number {
163+
points.sort((a, b) => (a[0] === b[0] ? b[1] - a[1] : a[0] - b[0]));
164+
const n = points.length;
165+
let ans = 0;
166+
for (let i = 0; i < n; ++i) {
167+
const [_, y1] = points[i];
168+
let maxY = -Infinity;
169+
for (let j = i + 1; j < n; ++j) {
170+
const [_, y2] = points[j];
171+
if (maxY < y2 && y2 <= y1) {
172+
maxY = y2;
173+
++ans;
174+
}
175+
}
176+
}
177+
return ans;
178+
}
84179
```
85180

86181
<!-- tabs:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int numberOfPairs(vector<vector<int>>& points) {
4+
sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int>& b) {
5+
return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1]);
6+
});
7+
int n = points.size();
8+
int ans = 0;
9+
for (int i = 0; i < n; ++i) {
10+
int y1 = points[i][1];
11+
int maxY = INT_MIN;
12+
for (int j = i + 1; j < n; ++j) {
13+
int y2 = points[j][1];
14+
if (maxY < y2 && y2 <= y1) {
15+
maxY = y2;
16+
++ans;
17+
}
18+
}
19+
}
20+
return ans;
21+
}
22+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func numberOfPairs(points [][]int) (ans int) {
2+
sort.Slice(points, func(i, j int) bool {
3+
return points[i][0] < points[j][0] || points[i][0] == points[j][0] && points[j][1] < points[i][1]
4+
})
5+
for i, p1 := range points {
6+
y1 := p1[1]
7+
maxY := math.MinInt32
8+
for _, p2 := range points[i+1:] {
9+
y2 := p2[1]
10+
if maxY < y2 && y2 <= y1 {
11+
maxY = y2
12+
ans++
13+
}
14+
}
15+
}
16+
return
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int numberOfPairs(int[][] points) {
3+
Arrays.sort(points, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
4+
int ans = 0;
5+
int n = points.length;
6+
final int inf = 1 << 30;
7+
for (int i = 0; i < n; ++i) {
8+
int y1 = points[i][1];
9+
int maxY = -inf;
10+
for (int j = i + 1; j < n; ++j) {
11+
int y2 = points[j][1];
12+
if (maxY < y2 && y2 <= y1) {
13+
maxY = y2;
14+
++ans;
15+
}
16+
}
17+
}
18+
return ans;
19+
}
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def numberOfPairs(self, points: List[List[int]]) -> int:
3+
points.sort(key=lambda x: (x[0], -x[1]))
4+
ans = 0
5+
for i, (_, y1) in enumerate(points):
6+
max_y = -inf
7+
for _, y2 in points[i + 1 :]:
8+
if max_y < y2 <= y1:
9+
max_y = y2
10+
ans += 1
11+
return ans
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function numberOfPairs(points: number[][]): number {
2+
points.sort((a, b) => (a[0] === b[0] ? b[1] - a[1] : a[0] - b[0]));
3+
const n = points.length;
4+
let ans = 0;
5+
for (let i = 0; i < n; ++i) {
6+
const [_, y1] = points[i];
7+
let maxY = -Infinity;
8+
for (let j = i + 1; j < n; ++j) {
9+
const [_, y2] = points[j];
10+
if (maxY < y2 && y2 <= y1) {
11+
maxY = y2;
12+
++ans;
13+
}
14+
}
15+
}
16+
return ans;
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /