Festivals

Alice likes festivals a lot as we all do. He also likes spending money on these festivals. He spends money on buying various things at these festivals. But he has a problem with forgetting. He only remembers his top three maximum spendings for any festival.

For eg, on Holi, he spends 25 units on colors, 50 units on water sprays, 100 units on gifts, 150 units on sweets but he remembers only his top 3 spendings ,i.e., 50, 100 & 150.
Now as the year ends he wants to know the festival on which he spent most of his money that he can remember.

Example:

Input:  n = 3, vec={{"abc",10},{"xyx",15},{"oop",8}}
Output: xyz 15

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void festivals(long long nvector<pair<stringlong long>> &vec)
{
    string s;
    long long x;

    priority_queue<pair<stringlong long>> v;
    for (long long i = 0i < ni++)
    {
        s = vec[i].first;
        x = vec[i].second;
        v.push({sx});
    }

    string res;
    long long max1 = INT_MIN;
    while (!v.empty())
    {
        string x = v.top().first;
        long long y = v.top().second;
        long long ans = y;
        v.pop();
        long long cnt = 1;
        while (!v.empty() && v.top().first == x)
        {
            if (cnt == 3)
                v.pop();
            else
            {
                ans += v.top().second;
                cnt++;
                v.pop();
            }
        }
        if (ans >= max1)
        {
            max1 = ans;
            res = x;
        }
    }
    cout << res << " " << max1 << "\n";
}

int main()
{
    long long n = 3;

    vector<pair<stringlong long>> vec = {{"abc"10},
                                           {"xyz"15},
                                           {"oop"8}};

    festivals(nvec);

    return 0;
}


No comments:

Post a Comment