Skip to content

Latest commit

 

History

History
129 lines (109 loc) · 3.27 KB

215. Kth Largest Element in an Array.md

File metadata and controls

129 lines (109 loc) · 3.27 KB

leetcode-cn Daily Challange on June 29th, 2020.

leetcode Daily Challange on January 16th, 2021.


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: [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

Note:

  • You may assume k is always valid, 1 ≤ k ≤ array's length.

Solution

  • mine
    • Java
      • Arrays.sort Runtime: 1 ms, faster than 97.99%, Memory Usage: 40 MB, less than 47.16% of Java online submissions
        //O(N*logN) O(N)space
        public int findKthLargest(int[] nums, int k) {
            Arrays.sort(nums);
            return nums[nums.length - k];
        }
        

  • leetcode solution
    • Qucik sort Runtime: 1 ms, faster than 97.99%, Memory Usage: 40.2 MB, less than 32.73% of Java online submissions

      //O(N)time O(logN)space
      Random random = new Random();
      
      public int findKthLargest(int[] nums, int k) {
          return quickSelect(nums, 0, nums.length - 1, nums.length - k);
      }
      
      public int quickSelect(int[] a, int l, int r, int index) {
          int q = randomPartition(a, l, r);
          if (q == index) {
              return a[q];
          } else {
              return q < index ? quickSelect(a, q + 1, r, index) : quickSelect(a, l, q - 1, index);
          }
      }
      
      public int randomPartition(int[] a, int l, int r) {
          int i = random.nextInt(r - l + 1) + l;
          swap(a, i, r);
          return partition(a, l, r);
      }
      
      public int partition(int[] a, int l, int r) {
          int x = a[r], i = l - 1;
          for (int j = l; j < r; ++j) {
              if (a[j] <= x) {
                  swap(a, ++i, j);
              }
          }
          swap(a, i + 1, r);
          return i + 1;
      }
      
      public void swap(int[] a, int i, int j) {
          int temp = a[i];
          a[i] = a[j];
          a[j] = temp;
      }
      
    • Heap Sort Runtime: 1 ms, faster than 97.99%, Memory Usage: 40 MB, less than 44.18% of Java online submissions

      //O(N*logN)time O(logN)space
      public int findKthLargest(int[] nums, int k) {
          int heapSize = nums.length;
          buildMaxHeap(nums, heapSize);
          for (int i = nums.length - 1; i >= nums.length - k + 1; --i) {
              swap(nums, 0, i);
              --heapSize;
              maxHeapify(nums, 0, heapSize);
          }
          return nums[0];
      }
      
      public void buildMaxHeap(int[] a, int heapSize) {
          for (int i = heapSize / 2; i >= 0; --i) {
              maxHeapify(a, i, heapSize);
          }
      }
      
      public void maxHeapify(int[] a, int i, int heapSize) {
          int l = i * 2 + 1, r = i * 2 + 2, largest = i;
          if (l < heapSize && a[l] > a[largest]) {
              largest = l;
          }
          if (r < heapSize && a[r] > a[largest]) {
              largest = r;
          }
          if (largest != i) {
              swap(a, i, largest);
              maxHeapify(a, largest, heapSize);
          }
      }
      
      public void swap(int[] a, int i, int j) {
          int temp = a[i];
          a[i] = a[j];
          a[j] = temp;
      }