Skip to content

Commit 2546691

Browse files
committed
Add solution and test-cases for problem 1471
1 parent a337ff8 commit 2546691

File tree

3 files changed

+85
-12
lines changed

3 files changed

+85
-12
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [1471.The k Strongest Values in an Array][title]
2+
3+
## Description
4+
Given an array of integers `arr` and an integer `k`.
5+
6+
A value `arr[i]` is said to be stronger than a value `arr[j]` if `|arr[i] - m| > |arr[j] - m|` where `m` is the **median** of the array.
7+
If `|arr[i] - m| == |arr[j] - m|`, then `arr[i]` is said to be stronger than `arr[j]` if `arr[i] > arr[j]`.
8+
9+
Return a list of the strongest `k` values in the array. return the answer **in any arbitrary order**.
10+
11+
**Median** is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position `((n - 1) / 2)` in the sorted list **(0-indexed)**.
12+
13+
- For `arr = [6, -3, 7, 2, 11]`, `n = 5` and the median is obtained by sorting the array `arr = [-3, 2, 6, 7, 11]` and the median is `arr[m]` where `m = ((5 - 1) / 2) = 2`. The median is `6`.
14+
- For `arr = [-7, 22, 17, 3]`, `n = 4` and the median is obtained by sorting the array `arr = [-7, 3, 17, 22]` and the median is `arr[m]` where `m = ((4 - 1) / 2) = 1`. The median is `3`.
15+
16+
**Example 1:**
17+
18+
```
19+
Input: arr = [1,2,3,4,5], k = 2
20+
Output: [5,1]
21+
Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer.
22+
Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1.
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: arr = [1,1,3,5,5], k = 2
29+
Output: [5,5]
30+
Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5].
31+
```
32+
33+
**Example 3:**
34+
35+
```
36+
Input: arr = [6,7,11,7,6,8], k = 5
37+
Output: [11,8,6,6,7]
38+
Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7].
39+
Any permutation of [11,8,6,6,7] is accepted.
40+
```
41+
42+
## 结语
43+
44+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
45+
46+
[title]: https://leetcode.com/problems/the-k-strongest-values-in-an-array
47+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(arr []int, k int) []int {
6+
l := len(arr)
7+
sort.Ints(arr)
8+
median := arr[(l-1)/2]
9+
ai, bi := 0, l-1
10+
ans := make([]int, k)
11+
index := 0
12+
for ; k > 0; k, index = k-1, index+1 {
13+
a := arr[ai] - median
14+
b := arr[bi] - median
15+
if a < 0 {
16+
a = -a
17+
}
18+
if b < 0 {
19+
b = -b
20+
}
21+
if a > b || (a == b && arr[ai] > arr[bi]) {
22+
ans[index] = arr[ai]
23+
ai++
24+
continue
25+
}
26+
ans[index] = arr[bi]
27+
bi--
28+
}
29+
return ans
530
}

leetcode/1401-1500/1471.The-k-Strongest-Values-in-an-Array/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
arr []int
14+
k int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 2, 3, 4, 5}, 2, []int{5, 1}},
18+
{"TestCase2", []int{1, 1, 3, 5, 5}, 2, []int{5, 5}},
19+
{"TestCase3", []int{6, 7, 11, 7, 6, 8}, 5, []int{11, 8, 6, 6, 7}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.arr, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.arr, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)