The provided solution has:
- time complexity of O((n+k)logn)
- space complexity of O(n)
In the case where k < n, in the first pass, do we need to store n elements, or can we just store k? It seems we’ll never use the elements beyond the first k rows. Could we edit line 7 to: for index in range(min(row_count, k)):
? And store just m = min(n, k) elements:
- time complexity of O(klogm)
- space complexity of O(m)
Course: Grokking Coding Interview Patterns in Python - Learn Interactively
Lesson: Solution: Kth Smallest Element in a Sorted Matrix - Grokking Coding Interview Patterns in Python