algorithm - ListMap: remove the m smallest element with the smallest values -


i have function take in input integer m, , remove m element of map smallest values. main problem o(nlogn) complexity. (n size of map)

this solution:

if (map.isempty())     return; if (map.size()<m){ //remove keys       iterator<k> it=map.keys().iterator(); //collection of keys        while (it.hasnext())           map.remove(it.next()); } else{ (int i=0; i<m; i++){    key=map.findkeymin() //complexity:o(n)    map.remove(k); } 

you can in o(n) time. take @ http://en.wikipedia.org/wiki/selection_algorithm . essentially, find mth element , remove less or equal mth element.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -