diff --git a/leetcode/1901-2000/1985.Find-the-Kth-Largest-Integer-in-the-Array/README.md b/leetcode/1901-2000/1985.Find-the-Kth-Largest-Integer-in-the-Array/README.md index a425db3a1..8c53d01cc 100755 --- a/leetcode/1901-2000/1985.Find-the-Kth-Largest-Integer-in-the-Array/README.md +++ b/leetcode/1901-2000/1985.Find-the-Kth-Largest-Integer-in-the-Array/README.md @@ -1,28 +1,41 @@ # [1985.Find the Kth Largest Integer in the Array][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given an array of strings `nums` and an integer `k`. Each string in `nums` represents an integer without leading zeros. + +Return the string that represents the kth **largest integer** in `nums`. + +**Note**: Duplicate numbers should be counted distinctly. For example, if `nums` is `["1","2","2"]`, `"2"` is the first largest integer, `"2"` is the second-largest integer, and `"1"` is the third-largest integer. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = ["3","6","7","10"], k = 4 +Output: "3" +Explanation: +The numbers in nums sorted in non-decreasing order are ["3","6","7","10"]. +The 4th largest integer in nums is "3". ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Find the Kth Largest Integer in the Array -```go ``` +Input: nums = ["2","21","12","1"], k = 3 +Output: "2" +Explanation: +The numbers in nums sorted in non-decreasing order are ["1","2","12","21"]. +The 3rd largest integer in nums is "2". +``` + +**Example 3:** +``` +Input: nums = ["0","0"], k = 2 +Output: "0" +Explanation: +The numbers in nums sorted in non-decreasing order are ["0","0"]. +The 2nd largest integer in nums is "0". +``` ## 结语 diff --git a/leetcode/1901-2000/1985.Find-the-Kth-Largest-Integer-in-the-Array/Solution.go b/leetcode/1901-2000/1985.Find-the-Kth-Largest-Integer-in-the-Array/Solution.go index d115ccf5e..9a1ffc186 100644 --- a/leetcode/1901-2000/1985.Find-the-Kth-Largest-Integer-in-the-Array/Solution.go +++ b/leetcode/1901-2000/1985.Find-the-Kth-Largest-Integer-in-the-Array/Solution.go @@ -1,5 +1,60 @@ package Solution -func Solution(x bool) bool { +import ( + "container/heap" +) + +type hp1985 []string + +func greater1985(a, b string) bool { + la, lb := len(a), len(b) + if la > lb { + return true + } else if la < lb { + return false + } + for k := 0; k < la; k++ { + if a[k] == b[k] { + continue + } + return a[k] > b[k] + } + return true +} +func (h *hp1985) Len() int { + return len(*h) +} + +func (h *hp1985) Less(i, j int) bool { + return !greater1985((*h)[i], (*h)[j]) +} + +func (h *hp1985) Swap(i, j int) { + (*h)[i], (*h)[j] = (*h)[j], (*h)[i] +} +func (h *hp1985) Push(x interface{}) { + *h = append(*h, x.(string)) +} + +func (h *hp1985) Pop() interface{} { + old := *h + l := len(*h) + x := old[l-1] + *h = old[:l-1] return x } + +func Solution(nums []string, k int) string { + h := hp1985{} + for _, str := range nums { + if h.Len() == k { + if !greater1985(h[0], str) { + heap.Pop(&h) + heap.Push(&h, str) + } + continue + } + heap.Push(&h, str) + } + return h[0] +} diff --git a/leetcode/1901-2000/1985.Find-the-Kth-Largest-Integer-in-the-Array/Solution_test.go b/leetcode/1901-2000/1985.Find-the-Kth-Largest-Integer-in-the-Array/Solution_test.go index 14ff50eb4..ef7723099 100644 --- a/leetcode/1901-2000/1985.Find-the-Kth-Largest-Integer-in-the-Array/Solution_test.go +++ b/leetcode/1901-2000/1985.Find-the-Kth-Largest-Integer-in-the-Array/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []string + k int + expect string }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []string{"3", "6", "7", "10"}, 4, "3"}, + {"TestCase2", []string{"2", "21", "12", "1"}, 3, "2"}, + {"TestCase3", []string{"0", "0"}, 2, "0"}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, 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.inputs, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }