Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Solutions that include sorting are suboptimal - Sorting requires O(n log n) complexity, whether building a heap takes O(n) time complexity.
As we only need unordered subset of top K elements of an array, Min Heap can be used.
+1
This is what I was attempting but fell short. Very clever.
Please don't make an unnecessary copy of that array when you can achieve comparable readability without doing so. Sorry, but I would definitely not consider that solution best practice.
This is my first translation. If I've done anything wrong then tell me and I'll try to fix it :)Just realised that the tests aren't done in random order.