Efficiently get largest 3 integers in C++ Linked List (unsorted) -
i heard there std functions give largest n integers of array, how linked list?
i think solution have few loops iterate on linked list, seems if there simpler solution in c++ libraries.
thanks.
i if can't use data structure:
typedef std::list<int> intlist; instlist list = <your_values>; int top[3]; (size_t = 0; < 3; i++) top[i] = std::numeric_limits<int>::min(); intlist::iterator it, end; (it = list.begin(), end = list.end(); != end; ++it) { const int& value = *it; if (value > top[2]) { top[0] = top[1]; top[1] = top[2]; top[2] = value; } else if (value > top[1]) { top[0] = top[1]; top[1] = value; } else if (value > top[0]) { top[0] = value; } }
Comments
Post a Comment