Topcoder high school SRM 3 250 pt - Level one - Best Seller editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm3
Topcoder high school SRM 3 250 pt - Level one - Best Seller solution:
#include <iostream> #include <vector> #include <climits> #include <algorithm> using namespace std; class BestSeller { public: string findBestSeller(vector <string> items) { int a[55]={0}, r=0, max=INT_MIN; sort(items.begin(), items.end()); for(int i=0; i<items.size(); i++) { for(int j=0; j<=i; j++) { if(items[j]==items[i]) a[j]++; if(a[j]>max) max=a[j], r=j; } } return items[r]; } };
note: i used sort() of <algorithm> library. after sorting find maximum(max) number sold per item. loop through items, if current item sold more than max then save it to max, and save its index to r.
No comments:
Post a Comment