a107: Finding top-k
Tags :
Accepted rate : 178人/208人 ( 86% ) [非即時]
評分方式:
Strictly

最近更新 : 2022-10-21 13:59

Content

Find the largest k elements from given numbers

Input

There's two part of input in one line

"k" as problem claimed,

"nums" represent a list of integers

Output

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)!!!

 

Sample Input #1
5 1 3 4 5 6
Sample Output #1
1 3 4 5 6
Sample Input #2
10 23123
Sample Output #2
23123
Sample Input #3
5 1231 3231 3212 3546 6786 751 321 321 32132 14564
Sample Output #3
3231 3546 6786 14564 32132
測資資訊:
記憶體限制: 64 MB
不公開 測資點#0 (25%): 1.0s , <1K
不公開 測資點#1 (25%): 1.0s , <1K
不公開 測資點#2 (25%): 1.0s , <1K
不公開 測資點#3 (25%): 1.0s , <1K
Hint :

[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?

Tags:
出處:
[管理者:
kichan@g.pui... (陳嘉賢CHAN KA IN)
]


ID User Problem Subject Hit Post Date
沒有發現任何「解題報告」