LeetCode 215 -- the K-th largest element in an array

Keywords: C++ less

1. topic

Find the kth largest element in an unordered array. Note that what you need to look for is the kth largest element after the array is sorted, not the kth different element.

Example 1:
Input: [3, 2, 1, 5, 6, 4] and k = 2
Output: 5
Example 2:

Input: [3, 2, 3, 1, 2, 4, 5, 5, 6] and k = 4
Output: 4

Explain:
You can assume that k is always valid and that 1 < k < the length of the array.

2. way of thinking

  • To solve this problem, we first think of sorting data from large to small using sorting algorithm, and then directly return to the descending K element.
  • In addition, we can also learn from the idea of quick sorting. Each time a pivot is selected, the number greater than pivot is placed on the left side of the pivot, and the number less than pivot is placed on the right side of the pivot.
  • At this point, if pivot happens to be the K-th data, then pivot is the K-th largest element in the array.

  • If pivot is less than K, then the K largest element in the array is on the right side of pivot. At this point, assume that pivot is located at which_max, which_max represents the pivot as the largest element in the array. At this point, we can find the largest element (K-which_max) from the data on the right side of pivot.

  • If pivot is located at a location greater than K, then the K largest element in the array is located on the left of pivot. At this point, the data on the left side of pivot is all larger than pivot, so we can continue to find the K largest element from the data on the left side of pivot.

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
    
        return quick_sort(nums, 0, nums.size()-1, k);
    }
    
    // The First Thought of Quick Line-up
    int quick_sort(vector<int>& data, int left, int right, int k)
    {
        int i = left;
        int j = right;
        int pivot = data[right];
        int len = right - left + 1;

        if (left < right)
        {  
            // Quick Array from Large to Small
            while(i < j)
            {
                while(i < j && data[i] >= pivot) // Find the first number smaller than pivot from left to right
                {
                    i++;
                }
                if (i < j)
                {
                    data[j--] = data[i];
                }

                while(i < j && data[j] <= pivot) // Find the first number larger than pivot from right to left
                {
                    j--;
                }
                if (i < j)
                {
                    data[i++] = data[j];
                }
            }
            
            data[i] = pivot; // At this point I = J

            // Pivot is located at index i at this time, i - left + 1 means pivot is the largest number
            int which_max = i - left + 1;
            if (which_max == k) // pivot happens to be the largest number in k
            {
                return pivot;
            }
  
            // The number of the largest K is on the right side of pivot, and the problem is transformed into finding the element of the largest (k - which_max) array on the right side.
            // For example, pivot is the fourth largest number. If you want to find the fifth largest number, you can continue to find the first largest number in the right array.
            else if(which_max < k)
            {
                return quick_sort(data, i + 1, right, k - which_max);
            }
            
            // The number of the largest k is on the left of pivot, and the problem is transformed into finding the element of the largest k of the array on the left.
            // For example, pivot is the third largest number. If you want to find the second largest number, you can continue to find the second largest number in the left array.
            else
            {
                return quick_sort(data, left, i - 1, k);
            }
        }
        else
        {
            return pivot;
        }
    }

     // The second kind of fast-track thought
    int quick_sort(vector<int>& data, int left, int right, int k)
    {
        int i = left;
        int j = left;
        int pivot = data[right];
        int len = right - left + 1;

        if (left < right)
        {
            
            // Quick Array from Large to Small
            for (; j < right; j++)
            {
                if (data[j] > pivot)
                {
                    int temp = data[i];
                    data[i] = data[j];
                    data[j] = temp;
                    i++;
                }
            }

            data[j] = data[i];
            data[i] = pivot;

            // Pivot is located at index i at this time, i - left + 1 means pivot is the largest number
            int which_max = i - left + 1;
            if (which_max == k) // pivot happens to be the largest number in k
            {
                return pivot;
            }
  
            // The number of the largest K is on the right side of pivot, and the problem is transformed into finding the element of the largest (k - which_max) array on the right side.
            // For example, pivot is the fourth largest number. If you want to find the fifth largest number, you can continue to find the first largest number in the right array.
            else if(which_max < k)
            {
                return quick_sort(data, i + 1, right, k - which_max);
            }
            
            // The number of the largest k is on the left of pivot, and the problem is transformed into finding the element of the largest k of the array on the left.
            // For example, pivot is the third largest number. If you want to find the second largest number, you can continue to find the second largest number in the left array.
            else
            {
                return quick_sort(data, left, i - 1, k);
            }
        }
        else
        {
            return pivot;
        }
    }
};

For more excitement, please pay attention to "seniusen".

Posted by thumrith on Wed, 30 Jan 2019 18:12:15 -0800