Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5a76144

Browse files
authored
215 kth largest element in an array (#14)
1 parent 4256bb6 commit 5a76144

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ continually updating 😃.
1818
* [88. Merge Sorted Array](./src/0088_merge_sorted_array/msa.go)   *`sort;`*  *`array`*
1919
* [167. Two Sum II - Input array is sorted](./src/0167_two_sum2/two_sum2.go)   *`double index;`*  *`binary search`*
2020
* [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)   *`sliding window`*
21+
* [215. Kth Largest Element in an Array](./src/0215_kth_largest_element_in_an_array/kthleiaa.go)   *`sort`*
2122
* [283. Move Zeroes(solution1)](./src/0283_move_zeroes/move_zeroes.go)   *`sliding window`*
2223
* [283. Move Zeroes(solution2)](./src/0283_move_zeroes/move_zeroes2.go)   *`sliding window`*
2324
* [349. Intersection of Two Arrays](./src/0349_intersection_of_2_arrays/intersection_of_two_arrays.go)   *`set`*
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
215. Kth Largest Element in an Array
3+
https://leetcode.com/problems/kth-largest-element-in-an-array/
4+
5+
Find the kth largest element in an unsorted array.
6+
Note that it is the kth largest element in the sorted order, not the kth distinct element.
7+
8+
You may assume k is always valid, 1 ≤ k ≤ array's length.
9+
10+
*/
11+
12+
// time: 2018年12月22日
13+
14+
package kthleiaa
15+
16+
// heap sort
17+
// time complexity: O(k * logn)
18+
// sapce complexity: O(1)
19+
func findKthLargest(nums []int, k int) int {
20+
// heapify
21+
for i := (len(nums) - 1) / 2; i >= 0; i-- {
22+
shiftDown(nums, len(nums), i)
23+
}
24+
25+
for i := len(nums) - 1; i >= len(nums)-k; i-- {
26+
nums[i], nums[0] = nums[0], nums[i]
27+
shiftDown(nums, i, 0)
28+
}
29+
return nums[len(nums)-k]
30+
}
31+
32+
func shiftDown(nums []int, n int, i int) {
33+
for 2*i+1 < n {
34+
j := 2*i + 1
35+
if j+1 < n && nums[j+1] > nums[j] {
36+
j++
37+
}
38+
39+
if nums[i] >= nums[j] {
40+
break
41+
}
42+
nums[j], nums[i] = nums[i], nums[j]
43+
i = j
44+
}
45+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package kthleiaa
2+
3+
import "testing"
4+
5+
func TestFindKthLargest(t *testing.T) {
6+
type arg struct {
7+
nums []int
8+
k int
9+
}
10+
11+
testCases := []arg{
12+
{nums: []int{3, 2, 1, 5, 6, 4}, k: 2},
13+
{nums: []int{3, 2, 3, 1, 2, 4, 5, 5, 6}, k: 4},
14+
}
15+
16+
expected := []int{5, 4}
17+
18+
for index, data := range testCases {
19+
if res := findKthLargest(data.nums, data.k); res != expected[index] {
20+
t.Errorf("expected %d, got %d", expected[index], res)
21+
}
22+
}
23+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
3535
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|
3636
|0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*|
37+
|0215|[215. Kth Largest Element in an Array](0215_kth_largest_element_in_an_array/kthleiaa.go)|Medium|*`sort`*|
3738
|0226|[Invert Binary Tree](./0226_invert_binary_tree/invert_binary_tree.go)|Easy|*`recursion; `* *`binary tree`*|
3839
|0283|[Move Zeroes(solution1)](./0283_move_zeroes/move_zeroes.go) <br/> [Move Zeroes(solution2)](./0283_move_zeroes/move_zeroes2.go)|Easy|*`array`*|
3940
|0300|[Longest Increasing Subsequence](./0300_longest_increasing_subsequence/lis.go)|Medium|*`dp`*|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /