Advertisement
1988coder

215. Kth Largest Element in an Array (4 Approaches)

Dec 25th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.14 KB | None | 0 0
  1. /*
  2. Refer: https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/60294/Solution-explained
  3. Also Check top voted comments and its replies: https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/60294/Solution-explained/61503
  4. */
  5.  
  6. /*
  7. Using Quick Select algorithm. Same algorithm is used in Quick Sort.
  8. This does not shuffles the input. Thus worst case is not good.
  9. Time Complexity: O(N) best case / O(N^2) worst case
  10. Space Complexity: O(1)
  11. */
  12. class Solution {
  13.     public int findKthLargest(int[] nums, int k) throws IllegalArgumentException {
  14.         if (nums == null || nums.length == 0) {
  15.             throw new IllegalArgumentException("Input nums array is null / empty");
  16.         }
  17.         if (k < 0 || k > nums.length) {
  18.             throw new IllegalArgumentException("Invalid k value");
  19.         }
  20.        
  21.         int left = 0;
  22.         int right = nums.length - 1;
  23.         k = nums.length - k;
  24.        
  25.         while (left < right) {
  26.             int pivot = partition(nums, left, right);
  27.             if (k == pivot) {
  28.                 return nums[k];
  29.             }
  30.             if (k < pivot) {
  31.                 right = pivot - 1;
  32.             } else {
  33.                 left = pivot + 1;
  34.             }
  35.         }
  36.        
  37.         return nums[left];
  38.     }
  39.    
  40.     private int partition(int[] nums, int l, int r) {
  41.         int pivot = l;
  42.         int k = pivot;
  43.         l++;
  44.        
  45.         while (l <= r) {
  46.             if (nums[l] <= nums[pivot]) {
  47.                 k++;
  48.                 if (k != l) {
  49.                     swap(nums, k, l);
  50.                 }
  51.             }
  52.             l++;
  53.         }
  54.        
  55.         swap(nums, k, pivot);
  56.         return k;
  57.     }
  58.    
  59.     private void swap(int[] nums, int i, int j) {
  60.         int temp = nums[i];
  61.         nums[i] = nums[j];
  62.         nums[j] = temp;
  63.     }
  64. }
  65.  
  66.  
  67. /*
  68. Using Quick Select algorithm. Same algorithm is used in Quick Sort.
  69. This algorithm shuffles the input array. It uses Fisher–Yates shuffle algorithm (https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle)
  70. Time Complexity: O(N) best case / O(N^2) worst case
  71. Space Complexity: O(1)
  72. */
  73. class Solution {
  74.     public int findKthLargest(int[] nums, int k) throws IllegalArgumentException {
  75.         if (nums == null || nums.length == 0) {
  76.             throw new IllegalArgumentException("Input nums array is null / empty");
  77.         }
  78.         if (k < 0 || k > nums.length) {
  79.             throw new IllegalArgumentException("Invalid k value");
  80.         }
  81.        
  82.         shuffle(nums);
  83.        
  84.         int left = 0;
  85.         int right = nums.length - 1;
  86.         k = nums.length - k;
  87.        
  88.         while (left < right) {
  89.             int pivot = partition(nums, left, right);
  90.             if (k == pivot) {
  91.                 return nums[k];
  92.             }
  93.             if (k < pivot) {
  94.                 right = pivot - 1;
  95.             } else {
  96.                 left = pivot + 1;
  97.             }
  98.         }
  99.        
  100.         return nums[left];
  101.     }
  102.    
  103.     private void shuffle(int[] nums) {
  104.         Random random = new Random();
  105.         for (int i = nums.length-1; i > 0; i--) {
  106.             int r = random.nextInt(i+1);
  107.             swap(nums, r, i);
  108.         }
  109.     }
  110.    
  111.     private int partition(int[] nums, int l, int r) {
  112.         int pivot = l;
  113.         int k = pivot;
  114.         l++;
  115.        
  116.         while (l <= r) {
  117.             if (nums[l] <= nums[pivot]) {
  118.                 k++;
  119.                 if (k != l) {
  120.                     swap(nums, k, l);
  121.                 }
  122.             }
  123.             l++;
  124.         }
  125.        
  126.         swap(nums, k, pivot);
  127.         return k;
  128.     }
  129.    
  130.     private void swap(int[] nums, int i, int j) {
  131.         int temp = nums[i];
  132.         nums[i] = nums[j];
  133.         nums[j] = temp;
  134.     }
  135. }
  136.  
  137.  
  138. /*
  139. Using Priority Queue
  140. Time Complexity: O(N * log k)
  141. Space Complexity: O(k)
  142.  
  143. N = number of elements in the input array
  144. */
  145. class Solution {
  146.     public int findKthLargest(int[] nums, int k) throws IllegalArgumentException {
  147.         if (nums == null || nums.length == 0) {
  148.             throw new IllegalArgumentException("Input nums array is null / empty");
  149.         }
  150.         if (k < 0 || k > nums.length) {
  151.             throw new IllegalArgumentException("Invalid k value");
  152.         }
  153.        
  154.         PriorityQueue<Integer> queue = new PriorityQueue();
  155.         for (int num : nums) {
  156.             queue.offer(num);
  157.            
  158.             if (queue.size() > k) {
  159.                 queue.poll();
  160.             }
  161.         }
  162.        
  163.         return queue.peek();
  164.     }
  165. }
  166.  
  167.  
  168. /*
  169. Using Array Sort function
  170. Time Complexity: O(N * log N)
  171. Space Complexity: O(1)
  172.  
  173. N = number of elements in the input array
  174. */
  175. class Solution {
  176.     public int findKthLargest(int[] nums, int k) throws IllegalArgumentException {
  177.         if (nums == null || nums.length == 0) {
  178.             throw new IllegalArgumentException("Input nums array is null / empty");
  179.         }
  180.         if (k < 0 || k > nums.length) {
  181.             throw new IllegalArgumentException("Invalid k value");
  182.         }
  183.        
  184.         Arrays.sort(nums);
  185.        
  186.         return nums[nums.length - k];
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement