Skip to content

Commit 3ae7154

Browse files
authored
Merge pull request #1302 from 0xff-dev/3027
Add solution and test-cases for problem 3027
2 parents 0b1c46f + cdf6eb7 commit 3ae7154

File tree

7 files changed

+82
-22
lines changed

7 files changed

+82
-22
lines changed
54.8 KB
Loading
29.2 KB
Loading
89.8 KB
Loading
87.9 KB
Loading

leetcode/3001-3100/3027.Find-the-Number-of-Ways-to-Place-People-II/README.md

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,58 @@
11
# [3027.Find the Number of Ways to Place People II][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a 2D array `points` of size `n x 2` representing integer coordinates of some points on a 2D-plane, where `points[i] = [xi, yi]`.
5+
6+
We define the **right** direction as positive x-axis (**increasing x-coordinate**) and the **left** direction as negative x-axis (**decreasing x-coordinate**). Similarly, we define the **up** direction as positive y-axis (**increasing y-coordinate**) and the **down** direction as negative y-axis (**decreasing y-coordinate**)
7+
8+
You have to place `n` people, including Alice and Bob, at these points such that there is **exactly one** person at every point. Alice wants to be alone with Bob, so Alice will build a rectangular fence with Alice's position as the **upper left corner** and Bob's position as the **lower right corner** of the fence (**Note** that the fence **might not** enclose any area, i.e. it can be a line). If any person other than Alice and Bob is either **inside** the fence or **on** the fence, Alice will be sad.
9+
10+
Return the number of **pairs of points** where you can place Alice and Bob, such that Alice **does not** become sad on building the fence.
11+
12+
**Note** that Alice can only build a fence with Alice's position as the upper left corner, and Bob's position as the lower right corner. For example, Alice cannot build either of the fences in the picture below with four corners `(1, 1), (1, 3), (3, 1), and (3, 3)`, because:
13+
14+
- With Alice at `(3, 3)` and Bob at `(1, 1)`, Alice's position is not the upper left corner and Bob's position is not the lower right corner of the fence.
15+
- With Alice at `(1, 3)` and Bob at `(1, 1)`, Bob's position is not the lower right corner of the fence.
16+
17+
![1](./1.png)
718

8-
**Example 1:**
19+
20+
**Example 1:**
21+
22+
![2](./2.png)
923

1024
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
25+
Input: points = [[1,1],[2,2],[3,3]]
26+
Output: 0
27+
Explanation: There is no way to place Alice and Bob such that Alice can build a fence with Alice's position as the upper left corner and Bob's position as the lower right corner. Hence we return 0.
1328
```
1429

15-
## 题意
16-
> ...
30+
**Example 2:**
1731

18-
## 题解
32+
![3](./3.png)
1933

20-
### 思路1
21-
> ...
22-
Find the Number of Ways to Place People II
23-
```go
2434
```
35+
Input: points = [[6,2],[4,4],[2,6]]
36+
Output: 2
37+
Explanation: There are two ways to place Alice and Bob such that Alice will not be sad:
38+
- Place Alice at (4, 4) and Bob at (6, 2).
39+
- Place Alice at (2, 6) and Bob at (4, 4).
40+
You cannot place Alice at (2, 6) and Bob at (6, 2) because the person at (4, 4) will be inside the fence.
41+
```
42+
43+
**Example 3:**
2544

45+
![4](./4.png)
46+
47+
```
48+
Input: points = [[3,1],[1,3],[1,1]]
49+
Output: 2
50+
Explanation: There are two ways to place Alice and Bob such that Alice will not be sad:
51+
- Place Alice at (1, 1) and Bob at (3, 1).
52+
- Place Alice at (1, 3) and Bob at (1, 1).
53+
You cannot place Alice at (1, 3) and Bob at (3, 1) because the person at (1, 1) will be on the fence.
54+
Note that it does not matter if the fence encloses any area, the first and second fences in the image are valid.
55+
```
2656

2757
## 结语
2858

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"math"
5+
"sort"
6+
)
7+
8+
func Solution(points [][]int) int {
9+
ans := 0
10+
sort.Slice(points, func(i, j int) bool {
11+
if points[i][0] == points[j][0] {
12+
return points[i][1] > points[j][1]
13+
}
14+
return points[i][0] < points[j][0]
15+
})
16+
17+
for i := 0; i < len(points)-1; i++ {
18+
pointA := points[i]
19+
xMin := pointA[0] - 1
20+
xMax := math.MaxInt32
21+
yMin := math.MinInt32
22+
yMax := pointA[1] + 1
23+
24+
for j := i + 1; j < len(points); j++ {
25+
pointB := points[j]
26+
if pointB[0] > xMin && pointB[0] < xMax &&
27+
pointB[1] > yMin && pointB[1] < yMax {
28+
ans++
29+
xMin = pointB[0]
30+
yMin = pointB[1]
31+
}
32+
}
33+
}
34+
return ans
535
}

leetcode/3001-3100/3027.Find-the-Number-of-Ways-to-Place-People-II/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1, 1}, {2, 2}, {3, 3}}, 0},
17+
{"TestCase2", [][]int{{6, 2}, {4, 4}, {2, 6}}, 2},
18+
{"TestCase3", [][]int{{3, 1}, {1, 3}, {1, 1}}, 2},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)