Monday, August 14th

 K-th largest element

Given an integer array and a value k, return the kth largest element in the array

Image by Postdlf, courtesy of Wikipedia.  Distributed under the Creative Commons Attribution-Share Alike 3.0 Unported license.  No changes made.

There are many different ways to approach the problem.

One may trivially sort the array then choose the kth largest

Another approach would be to create a max priority queue and maintain up to k elements

A third approach would be to use Counting Sort

A fourth approach would be to use Quickselect.  The algorithm randomly select a pivot element, then divide the array into 3 partitions: elements less than the pivot, equal to the pivot, and greater than the pivot.  Depending on the number of elements in each partition, the search space can be reduced, and the quickselect function can be called recursively.  

In the end, the array will be roughly sorted around the pivot, with the pivot being the target element, all elements to the left of the pivot lower in value, and all elements to the right greater than the pivot.  Average time complexity is linear, while quadratic in the worst case, which is unlikely

Moreover, there is an optimization for selecting the pivot using a median-of-medians, which brings the worst case time complexity to linear time

Even though Quickselect isn't too complicated code-wise, the Editorial says that a candidate would not be expected to derive it in an interview setting.  Still, we can skip the implementation and call the nth_element() function in C++ which is (I presume) based off quickselect

    int findKthLargest(vector<int>& nums, int k) {
int k2 = nums.size()-k; //kth largest not kth smallest
vector<int>::iterator i = nums.begin()+k2;
nth_element(nums.begin(), i, nums.end());
return nums[k2];
}

This nth_element function may be useful in future problems that look for a kth element as a subproblem

The nth_largest() approach runs around the 99th percentile for speed

In a previous post, I made an argument against an algorithm that operated in logarithmic time on average, and worst case linear, instead opting for a linear scan.  Here the worst case is quadratic but performs in linear time on average.  In both cases the code is 4 lines long.

Nice review of Quicksort.  Definitely implementable in an interview.  The nth_element() function, like sort(), may be very useful.


Comments

Popular posts from this blog

Wednesday, September 20th

Monday, September 11th

Tuesday, September 19th