Skip to content

Commit ca207cf

Browse files
authored
Merge pull request #223 from 0xff-dev/973
Add solution and test-cases for problem 973
2 parents 8d007ef + 188dfe3 commit ca207cf

File tree

4 files changed

+78
-23
lines changed

4 files changed

+78
-23
lines changed

leetcode/901-1000/0973.K-Closest-Points-to-Origin/README.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
# [973.K Closest Points to Origin][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+
Given an array of `points` where points[i] = [x<sub>i</sub>, y<sub>i</sub>] represents a point on the **X-Y** plane and an integer `k`, return the `k` closest points to the origin `(0, 0)`.
75

8-
**Example 1:**
6+
The distance between two points on the **X-Y** plane is the Euclidean distance (i.e., √(x<sub>1</sub> - x<sub>2</sub>)<sup>2</sup> + (y<sub>1</sub> - y<sub>2</sub>)<sup>2</sup>).
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
You may return the answer in **any order**. The answer is **guaranteed** to be **unique** (except for the order that it is in).
9+
10+
<br>
1411

15-
## 题意
16-
> ...
12+
**Example 1:**
1713

18-
## 题解
14+
<img src="./closestplane1.jpg" width=300 height=300>
1915

20-
### 思路1
21-
> ...
22-
K Closest Points to Origin
23-
```go
16+
```
17+
Input: points = [[1,3],[-2,2]], k = 1
18+
Output: [[-2,2]]
19+
Explanation:
20+
The distance between (1, 3) and the origin is sqrt(10).
21+
The distance between (-2, 2) and the origin is sqrt(8).
22+
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
23+
We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]].
24+
```
25+
26+
**Example 2:**
27+
```
28+
Input: points = [[3,3],[5,-1],[-2,4]], k = 2
29+
Output: [[3,3],[-2,4]]
30+
Explanation: The answer [[-2,4],[3,3]] would also be accepted.
2431
```
2532

2633

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(points [][]int, k int) [][]int {
4+
result := make([][]int, 0)
5+
for {
6+
m := kClosestPartion(points)
7+
if m == k-1 {
8+
result = append(result, points[:m+1]...)
9+
return result
10+
}
11+
if m < k {
12+
result = append(result, points[:m+1]...)
13+
points = points[m+1:]
14+
k -= m + 1
15+
continue
16+
}
17+
points = points[:m]
18+
}
19+
}
20+
21+
func kClosestPartion(points [][]int) int {
22+
if len(points) == 0 {
23+
return 0
24+
}
25+
x, y := points[0][0], points[0][1]
26+
cmpObj, idx := x*x+y*y, 0
27+
28+
for walker := 1; walker < len(points); walker++ {
29+
x, y = points[walker][0], points[walker][1]
30+
dis := x*x + y*y
31+
if dis < cmpObj {
32+
idx++
33+
points[idx], points[walker] = points[walker], points[idx]
34+
}
35+
}
36+
points[idx], points[0] = points[0], points[idx]
37+
return idx
538
}

leetcode/901-1000/0973.K-Closest-Points-to-Origin/Solution_test.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package Solution
22

33
import (
44
"reflect"
5+
"sort"
56
"strconv"
67
"testing"
78
)
@@ -10,18 +11,32 @@ func TestSolution(t *testing.T) {
1011
// 测试用例
1112
cases := []struct {
1213
name string
13-
inputs bool
14-
expect bool
14+
inputs [][]int
15+
k int
16+
expect [][]int
1517
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
18+
{"TestCase1", [][]int{{1, 3}, {-2, 2}}, 1, [][]int{{-2, 2}}},
19+
{"TestCase2", [][]int{{3, 3}, {5, -1}, {-2, 4}}, 2, [][]int{{3, 3}, {-2, 4}}},
20+
{"TestCase3", [][]int{{3, 3}, {5, -1}, {-2, 4}}, 3, [][]int{{3, 3}, {-2, 4}, {5, -1}}},
1921
}
2022

2123
// 开始测试
2224
for i, c := range cases {
2325
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
26+
got := Solution(c.inputs, c.k)
27+
sort.Slice(got, func(i, j int) bool {
28+
if got[i][0] == got[j][0] {
29+
return got[i][1] < got[j][1]
30+
}
31+
return got[i][0] < got[j][0]
32+
})
33+
34+
sort.Slice(c.expect, func(i, j int) bool {
35+
if c.expect[i][0] == c.expect[j][0] {
36+
return c.expect[i][1] < c.expect[j][1]
37+
}
38+
return c.expect[i][0] < c.expect[j][0]
39+
})
2540
if !reflect.DeepEqual(got, c.expect) {
2641
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2742
c.expect, got, c.inputs)
17 KB
Loading

0 commit comments

Comments
 (0)