The first sentence in the Time Complexity paragraph states, “The first loop inserts k elements into an array, which takes O(k) time complexity.” Wouldn’t that be O(k log k) since we are adding items to the binary heap?
The last sentence reports, “The overall time complexity is therefore O(k + (n − k) × log k), which simplifies to O((n − k) × log k).” If we are running O(k log k) in the first loop, wouldn’t that be a total of O(n log k) in the worst case?
Course: Grokking Coding Interview Patterns in JavaScript - Learn Interactively
Lesson: Solution: Kth Largest Element in an Array - Grokking Coding Interview Patterns in JavaScript
Course: Grokking Coding Interview Patterns in JavaScript - Learn Interactively
Lesson: Solution: Kth Largest Element in an Array - Grokking Coding Interview Patterns in JavaScript