I have an API service that returns the cpu count of each node in the cluster. My task is to return the top K nodes with cpu count.
How do I resolve this in an efficient way while building a cli tool?
Here is what I have thought of:
- Fetch, sort, return top k
- Maintain a heap, when the query comes, return the first k elements from heap.
I do not think both the methods are efficient as the first one will be slow and the second one will have intricacies involved with memory usage. What algorithm/method should I use?