Given a list of items and their values. The task is to find top k items with the highest value. It is possible that two items have the same value, in that case item whose name comes first (lexicographically) will be given higher priority.
Examples:
Input: items[] = {Bat, Gloves, Wickets, Ball}, values[] = {100, 50, 200, 100}
k = 2Output: Wickets Ball
Wickets has the highest value.
Bat, Ball has the same value but Ball comes first lexicographically.
Approach: This question can be solved by picking the items greedily according to the values. We will sort use the items list in the decreasing order of the values and in case of the same values items will be sorted lexicographical order increasing order.
We will store the data in the form of pairs in a vector and will use an inbuilt sort function with boolean comparator function which will be used to compare two items.
Below is the implementation of the above approach:
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Boolean Comparator Function // to compare two pairs of item-value bool comp(pair<string, int > A, pair<string, int > B) { // Compare the name if the values are equal if (A.second == B.second) return A.first < B.first; // Else compare values return A.second > B.second; } // Driver code int main() { int k = 2; int n = 3; // Store data in a vector of Item-Value Pair vector<pair<string, int > > items; // inserting items-value pairs in the vector items.push_back(make_pair( "Bat" , 100)); items.push_back(make_pair( "Gloves" , 50)); items.push_back(make_pair( "Wickets" , 200)); items.push_back(make_pair( "Ball" , 100)); // Sort items using Inbuilt function sort(items.begin(), items.end(), comp); // Print top k values // or if n is less than k // Print all n items for ( int i = 0; i < min(n, k); ++i) { cout << items[i].first << '\n' ; } return 0; } |
Wickets Ball
Time Complexity: O(NlogN)
Further Optimization : We can further optimize above solutions using Heap Data Structure. Please see k largest elements in an array.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.