C++ Program to find out how many movies an attendee can watch entirely at a Film festival

C++Server Side ProgrammingProgramming

Suppose there is a film festival going on that showcase various movies from various countries. Now, an attendee wants to attend the maximum number of movies that do not overlap with each other and we have to help them to find out how many movies they can attend.

There is a structure Movie that has the following members −

  • The beginning time of the movie.
  • The duration of the movie.
  • The ending time of the movie.

There is another structure Festival with the following members −

  • The number of movies at the festival.
  • An array of type Movie whose size is similar to the number of movies at the festival.

We have to create and initialize a Festival object with two arrays 'timeBegin' and 'duration' that contain the start time and duration of several movies respectively. An integer n denotes the total number of movies and that is also used to initialize the object. We further use that object to calculate how many movies an attendee can fully watch.

So, if the input is like timeBegin = {1, 3, 0, 5, 5, 8, 8}, duration = {3, 2, 2, 4, 3, 2, 3}, n = 7, then the output will be 4

The attendee can watch a total of 4 movies entirely at that festival.

To solve this, we will follow these steps −

  • struct Movie {
    • Define three member variables timeBegin, duration, timeEnd
    • Overload an operator ‘<’, this will take a Movie type variable another.
      • return timeEnd < another.timeEnd
  • struct Festival {
    • Define a member count
    • Define an array movies that contains item of type Movie
  • Define a function initialize(). This will take arrays timeBegin and timeEnd and an iteger n.
    • filmFestival := A new Festival object
    • count of filmFestival := count
    • for initialize i := 0, when i < count, update (increase i by 1), do −
      • temp := a new object of type Movie
      • timeBegin of temp:= timeBegin[i]
      • duration of temp:= duration[i]
      • timeEnd of temp := timeBegin[i] + duration[i]
      • insert temp into array movies of filmFestival
    • return filmFestival
  • Define a function solve(), this will take a variable fest of type Festival,
    • res := 0
    • sort the array movies of fest
    • timeEnd := -1
    • for initialize i := 0, when i < fest - > count, update (increase i by 1), do −
      • if timeBegin of movies[i] of fest >= timeEnd, then −
        • (increase res by 1)
        • timeEnd := timeEnd of movies[i] of fest
    • return res

Example

Let us see the following implementation to get better understanding −

#include<bits/stdc++.h>

using namespace std;

struct Movie {
   int timeBegin, duration, timeEnd;
   bool operator<(const Movie& another) const {
      return timeEnd < another.timeEnd;
   }
};

struct Festival {
   int count;
   vector<Movie> movies;
};
Festival* initialize(int timeBegin[], int duration[], int count) {
   Festival* filmFestival = new Festival;
   filmFestival->count = count;
   for (int i = 0; i < count; i++) {
      Movie temp;
      temp.timeBegin = timeBegin[i];
      temp.duration = duration[i];
      temp.timeEnd = timeBegin[i] + duration[i];
      filmFestival->movies.push_back(temp);
   }
   return filmFestival;
}
int solve(Festival* fest) {
   int res = 0;
   sort(fest->movies.begin(), fest->movies.end());
   int timeEnd = -1;
   for (int i = 0; i < fest->count; i++) {
      if (fest->movies[i].timeBegin >= timeEnd) {
         res++;
            timeEnd = fest->movies[i].timeEnd;
      }
   }
   return res;
}

int main(int argc, char *argv[]) {
int timeBegin[] = {1, 3, 0, 5, 5, 8, 8};
int duration[] = {3, 2, 2, 4, 3, 2, 3};
Festival * fest;
fest = initialize(timeBegin,duration, 7);
cout << solve(fest) << endl;
return 0;
}

Input

int timeBegin[] = {1, 3, 0, 5, 5, 8, 8};
int duration[] = {3, 2, 2, 4, 3, 2, 3};
Festival * fest;
fest = initialize(timeBegin,duration, 7);

Output

4
raja
Updated on 20-Oct-2021 08:14:19

Advertisements