Skip to content

Commit c49465b

Browse files
author
openset
committed
Add: Kth Largest Element in an Array
1 parent 9d22018 commit c49465b

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

internal/leetcode/problems_status.go

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var problemStatus = map[int]bool{
4949
101: true,
5050
104: true,
5151
108: true,
52+
110: true,
5253
118: true,
5354
119: true,
5455
121: true,
@@ -79,6 +80,7 @@ var problemStatus = map[int]bool{
7980
202: true,
8081
204: true,
8182
206: true,
83+
215: true,
8284
217: true,
8385
219: true,
8486
231: true,
@@ -89,6 +91,7 @@ var problemStatus = map[int]bool{
8991
258: true,
9092
268: true,
9193
283: true,
94+
290: true,
9295
303: true,
9396
326: true,
9497
342: true,
@@ -217,4 +220,5 @@ var problemStatus = map[int]bool{
217220
1154: true,
218221
1163: true,
219222
1185: true,
223+
1221: true,
220224
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
package kth_largest_element_in_an_array
1+
package problem_215
2+
3+
import "sort"
4+
5+
func findKthLargest(nums []int, k int) int {
6+
sort.Ints(nums)
7+
return nums[len(nums)-k]
8+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
1-
package kth_largest_element_in_an_array
1+
package problem_215
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input []int
7+
k int
8+
expected int
9+
}
10+
11+
func TestFindKthLargest(t *testing.T) {
12+
tests := [...]caseType{
13+
{
14+
input: []int{3, 2, 1, 5, 6, 4},
15+
k: 2,
16+
expected: 5,
17+
}, {
18+
input: []int{3, 2, 3, 1, 2, 4, 5, 5, 6},
19+
k: 4,
20+
expected: 4,
21+
},
22+
}
23+
for _, tc := range tests {
24+
output := findKthLargest(tc.input, tc.k)
25+
if output != tc.expected {
26+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)