Movie Festival II

In a movie festival, n movies will be shown. Syrjälä's movie club consists of k members, who will be all attending the festival.

You know the starting and ending times of each movie. What is the maximum total number of movies the club members can watch entirely if they act optimally?

Example:

Input: n = 5, k = 2, a = {{1, 5}, {8, 10}, {3, 6}, {2, 5}, {6, 9}}
Output: 4

Approach

C++

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

int movieFestivalII(int nint k,
                    vector<vector<int>> &arr)
{

    sort(arr.begin(), arr.end());
    multiset<intst;
    int ans = 0;
    for (int i = 0i < ni++)
    {
        auto it = st.upper_bound(arr[i][1]);
        if (st.size() < k && it == st.begin())
        {
            ans++;
            st.insert(arr[i][0]);
        }
        else if (it != st.begin())
        {
            ans++;
            it = prev(it);
            st.erase(it);
            st.insert(arr[i][0]);
        }
    }
    return ans;
}

int main()
{

    int n = 5k = 2;
    vector<vector<int>> a = {{15},
                             {810},
                             {36},
                             {25},
                             {69}};

    vector<vector<int>> arr;
    for (int i = 0i < ni++)
    {
        int l = a[i][0]r = a[i][1];

        arr.push_back({rl});
    }
    cout << movieFestivalII(nkarr<< "\n";

    return 0;
}


No comments:

Post a Comment