Roy and Trending Topics

Roy is trying to develop a widget that shows Trending Topics (similar to Facebook) on the home page of HackerEarth Academy

He has gathered a list of N Topics (their IDs) and their popularity score (say z-score) from the database. Now z-score change every day according to the following rules:

  1. When a topic is mentioned in a 'Post', its z-score is increased by 50.
  2. A 'Like' on such a Post, increases the z-score by 5.
  3. A 'Comment' increases z-score by 10.
  4. A 'Share' causes an increment of 20.

Now the Trending Topics are decided according to the change in z-score. One with the highest increment comes on top and list follows.
Roy seeks your help to write an algorithm to find the top 5 Trending Topics.
If change in z-score for any two topics is same, then rank them according to their ID (one with higher ID gets priority). It is guaranteed that IDs will be unique.

Example:

Input: n = 8, arr = {{1003, 100, 4, 0, 0, 0}, {1002, 200, 6, 0, 0, 0}, {1001, 300, 8, 0, 0, 0}, {1004, 100, 3, 0, 0, 0}, {1005, 200, 3, 0, 0, 0}, {1006, 300, 5, 0, 0, 0}, {1007, 100, 3, 0, 0, 0}, {999, 100, 4, 0, 0, 0}}

Output:

1007 85595 1006 85510 1005 85425 1004 85340 1003 85255

Approach:

C++

#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<long longpair<long longlong long>> a,
         pair<long longpair<long longlong long>> b)
{
    if (a.first == b.first)
        return a.second.second > b.second.second;
    return a.first > b.first;
}

void trendingTopics(long long n,
                    vector<vector<long long>> &arr)
{
    priority_queue<pair<long longlong long>> q;
    vector<pair<long longpair<long longlong long>>> v;
    for (int i = 0i < ni++)
    {
        for (int j = 0j < arr[i].size(); j++)
        {
            long long id = arr[i][j]score = arr[i][j];
            long long Z = arr[i][j]P = arr[i][j];
            long long L = arr[i][j]C = arr[i][j];
            long long new_score = Z * 50 + P * 5 +
                                  10 * L + 20 * C;
            long long change_score = new_score - score;
            v.push_back(make_pair(change_score,
                                  make_pair(new_scoreid)));
        }
    }

    sort(v.begin(), v.end(), cmp);

    long long cnt = 0;
    for (long long i = 0i < v.size(); i++)
    {
        cout << v[i].second.second << " " << v[i].second.first << "\n";
        cnt++;
        if (cnt == 5)
            break;
    }
}
int main()
{

    long long n = 8;

    vector<vector<long long>> arr = {{10031004000},
                                     {10022006000},
                                     {10013008000},
                                     {10041003000},
                                     {10052003000},
                                     {10063005000},
                                     {10071003000},
                                     {9991004000}};

    trendingTopics(narr);

    return 0;
}


No comments:

Post a Comment