From 2546691942a581e23aa183b8329a28162570c47e Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 27 Dec 2024 09:17:33 +0800 Subject: [PATCH] Add solution and test-cases for problem 1471 --- .../README.md | 47 +++++++++++++++++++ .../Solution.go | 29 +++++++++++- .../Solution_test.go | 21 +++++---- 3 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 leetcode/1401-1500/1471.The-k-Strongest-Values-in-an-Array/README.md diff --git a/leetcode/1401-1500/1471.The-k-Strongest-Values-in-an-Array/README.md b/leetcode/1401-1500/1471.The-k-Strongest-Values-in-an-Array/README.md new file mode 100644 index 000000000..925ace52f --- /dev/null +++ b/leetcode/1401-1500/1471.The-k-Strongest-Values-in-an-Array/README.md @@ -0,0 +1,47 @@ +# [1471.The k Strongest Values in an Array][title] + +## Description +Given an array of integers `arr` and an integer `k`. + +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. +If `|arr[i] - m| == |arr[j] - m|`, then `arr[i]` is said to be stronger than `arr[j]` if `arr[i] > arr[j]`. + +Return a list of the strongest `k` values in the array. return the answer **in any arbitrary order**. + +**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)**. + +- 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`. +- 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`. + +**Example 1:** + +``` +Input: arr = [1,2,3,4,5], k = 2 +Output: [5,1] +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. +Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. +``` + +**Example 2:** + +``` +Input: arr = [1,1,3,5,5], k = 2 +Output: [5,5] +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]. +``` + +**Example 3:** + +``` +Input: arr = [6,7,11,7,6,8], k = 5 +Output: [11,8,6,6,7] +Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. +Any permutation of [11,8,6,6,7] is accepted. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/the-k-strongest-values-in-an-array +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/1401-1500/1471.The-k-Strongest-Values-in-an-Array/Solution.go b/leetcode/1401-1500/1471.The-k-Strongest-Values-in-an-Array/Solution.go index d115ccf5e..38c83938a 100755 --- a/leetcode/1401-1500/1471.The-k-Strongest-Values-in-an-Array/Solution.go +++ b/leetcode/1401-1500/1471.The-k-Strongest-Values-in-an-Array/Solution.go @@ -1,5 +1,30 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(arr []int, k int) []int { + l := len(arr) + sort.Ints(arr) + median := arr[(l-1)/2] + ai, bi := 0, l-1 + ans := make([]int, k) + index := 0 + for ; k > 0; k, index = k-1, index+1 { + a := arr[ai] - median + b := arr[bi] - median + if a < 0 { + a = -a + } + if b < 0 { + b = -b + } + if a > b || (a == b && arr[ai] > arr[bi]) { + ans[index] = arr[ai] + ai++ + continue + } + ans[index] = arr[bi] + bi-- + } + return ans } diff --git a/leetcode/1401-1500/1471.The-k-Strongest-Values-in-an-Array/Solution_test.go b/leetcode/1401-1500/1471.The-k-Strongest-Values-in-an-Array/Solution_test.go index 14ff50eb4..6f8ec73e5 100755 --- a/leetcode/1401-1500/1471.The-k-Strongest-Values-in-an-Array/Solution_test.go +++ b/leetcode/1401-1500/1471.The-k-Strongest-Values-in-an-Array/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + arr []int + k int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 2, 3, 4, 5}, 2, []int{5, 1}}, + {"TestCase2", []int{1, 1, 3, 5, 5}, 2, []int{5, 5}}, + {"TestCase3", []int{6, 7, 11, 7, 6, 8}, 5, []int{11, 8, 6, 6, 7}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.arr, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.arr, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }