filmov
tv
Programming Interview 36: Find (kth largest) Kth item from array after sorting in ascending order

Показать описание
Step by step to crack Programming Interview questions 36: Find Kth largest item from array
Solution: (Based on quicksort algorithm)
1. Pick a pivot and scan array to put pivot in position
2. If pivot equals K, return
3. If pivot is larger than k, check the right half only
3.1. All left half are less than pivot thus cannot be our candidate
4. If pivot position is smaller than k, check the left half only
4.1. All right half are larger than pivot thus cannot be our candidate
In worst case (unbalanced spliting, 1 and n-1): T(n) = O(n) + T(n-1) = O(n) +O(n-1)+O(n-2)+... = O(n2).
In average case (equal spliting): T(n) = O(n) + T(n divided by 2) = O(n)+O(n divided by 2)+O(n divided by 4)+...
which is LESS than O(2n) = O(n) thus faster than O(nk) in average.
Solution: (Based on quicksort algorithm)
1. Pick a pivot and scan array to put pivot in position
2. If pivot equals K, return
3. If pivot is larger than k, check the right half only
3.1. All left half are less than pivot thus cannot be our candidate
4. If pivot position is smaller than k, check the left half only
4.1. All right half are larger than pivot thus cannot be our candidate
In worst case (unbalanced spliting, 1 and n-1): T(n) = O(n) + T(n-1) = O(n) +O(n-1)+O(n-2)+... = O(n2).
In average case (equal spliting): T(n) = O(n) + T(n divided by 2) = O(n)+O(n divided by 2)+O(n divided by 4)+...
which is LESS than O(2n) = O(n) thus faster than O(nk) in average.
Programming Interview 36: Find (kth largest) Kth item from array after sorting in ascending order
Kth Largest Element in an Array
Kth Smallest Element in a BST
Find Kth smallest element in an array using Quick Select
Searching Algorithm - (Q&A -13) - Find Kth Largest or Kth Smallest Element In Array
Find the k th Smallest/Largest Element | Quick Select Algorithm | Optimizing Quick Sort
How to find Kth smallest element || Competitive Programming
Kth Smallest Element in a BST | Leetcode | Java | CODING INTERVIEW QUESTION
Webinar | Explore studies at KTH
Kth Smallest element question (Using Quick Select) | Lecture 50
10 Common Coding Interview Problems - Solved!
LeetCode 1539. Kth Missing Positive Number - Interview Prep Ep 87
Kth To Last Node in Linked List: Google & Microsoft Coding
Find the Kth Smallest element using partition algo
Find k-th smallest pair Distance | Leetcode hard | Interview Question
K-th Smallest in Lexicographical Order
Google Coding Interview Question | Kth Largest | Cracking the coding interviews [Cracking FAANG]
L45. K-th Smallest/Largest Element in BST
1_Zeyboard_Find Kth Maximum
Find the Kth Smallest Element in an Array | DSA Series | Part-5 | @UltimateStudy
Find Kth Smallest Element in a BST | BST | FavTutor
Array - 36: Find all Triplets for given Sum | Check if Triplet exists for given Sum
Remove Nth Node from End of List - Oracle Interview Question - Leetcode 19
Kth Missing Positive Number - Google Interview Question
Комментарии