#P107. Finding top-k

Finding top-k

題目描述

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

 

Samples

["5 1 3 4 5 6","10 23123","5 1231 3231 3212 3546 6786 751 321 321 32132 14564"]
["1 3 4 5 6","23123","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?

原始資料

  • Zero1 題號:a107
  • Hydro 題號:Z0107
  • Locale:zh_TW
  • Display:open