leetcode 215. Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.Example 1:
Input: and k = 2
Output: 5
Example 2:
Input: and k = 4
Output: 4
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
class Solution {
public int findKthLargest(int[] nums, int k) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for(int i :nums){
if(i > max) max = i;
if(i < min) min = i;
}
int[] arr = new int;
for(int i = 0 ; i< nums.length; i++){
arr - min]++;
}
for(int i = arr.length-1; i>-1; i--){
if(arr != 0) {
k = k - arr;
if(k <= 0) return i +min;
}
if(k == 0) return i+min;
}
return -1;
}
}
页:
[1]