Skip to content

Commit 9fab52c

Browse files
committed
kth_largest_element_in_an_array
1 parent 8694d0a commit 9fab52c

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
172172
#### [211. Add and Search Word - Data structure design](https://github.com/hitzzc/go-leetcode/tree/master/add_and_search_word)
173173
#### [213. House Robber II](https://github.com/hitzzc/go-leetcode/tree/master/add_and_search_word)
174174
#### [214. Shortest Palindrome (unsolved)](https://github.com/hitzzc/go-leetcode/tree/master/shortest_palindrome)
175+
#### [215. Kth Largest Element in an Array](https://github.com/hitzzc/go-leetcode/tree/master/kth_largest_element_in_an_array)
175176

176177

177178

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package kth_largest_element_in_an_array
2+
3+
func findKthLargest(nums []int, k int) int {
4+
return partition(nums, 0, len(nums)-1, k)
5+
}
6+
7+
func partition(nums []int, i, j, k int) (ret int) {
8+
p := i
9+
for m := i; m < j; m++ {
10+
if nums[m] > nums[j] {
11+
nums[p], nums[m] = nums[m], nums[p]
12+
p++
13+
}
14+
}
15+
nums[p], nums[j] = nums[j], nums[p]
16+
if p+1 == k {
17+
return nums[p]
18+
}
19+
if k < p+1 {
20+
return partition(nums, i, p-1, k)
21+
}
22+
return partition(nums, p+1, j, k)
23+
}

0 commit comments

Comments
 (0)