Find the largest k elements from given numbers
There's two part of input in one line
"k" as problem claimed,
"nums" represent a list of integers
A list of integers
The result must be ascending order form
Be careful! the k may be less than the length of nums (case 2)!!!
5 1 3 4 5 6
1 3 4 5 6
10 23123
23123
5 1231 3231 3212 3546 6786 751 321 321 32132 14564
3231 3546 6786 14564 32132
[Solution 1]You can simply "sort()"the numbers and then report the top-k. At the same time, please learn how to sort the number in ascending and descending order.
[Solution 2] A better approach is to maintain a "heap" to store the rolling top-k elements through an iteration (loop)
Which one is more efficient?
ID | User | Problem | Subject | Hit | Post Date |
沒有發現任何「解題報告」 |