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 n, int k,vector<vector<int>> &arr){sort(arr.begin(), arr.end());multiset<int> st;int ans = 0;for (int i = 0; i < n; i++){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 = 5, k = 2;vector<vector<int>> a = {{1, 5},{8, 10},{3, 6},{2, 5},{6, 9}};vector<vector<int>> arr;for (int i = 0; i < n; i++){int l = a[i][0], r = a[i][1];arr.push_back({r, l});}cout << movieFestivalII(n, k, arr) << "\n";return 0;}
No comments:
Post a Comment