binary search and simple answer in Java


  • 0
    G

    public class Solution {
    public int findKthLargest(int[] nums, int k) {
    int min=Integer.MAX_VALUE,max=Integer.MIN_VALUE,mid=0;
    for(int i:nums){
    if(i>max) max=i;
    if(i<min) min=i;
    }
    int count=0;
    while(min<max){
    count=0;
    mid=min+(max-min)/2+1;
    for(int j:nums){
    if(j>=mid) count++;
    }
    if(count>=k) min=mid;
    else max=mid-1;
    }
    return min;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.