The problem is as follows-
there are two arrays one with movie start timings and one with movie end timings..we need to figure out maximum how many movies a person can watch using the given start and end timings.
so as per above timings
one can watch maximum of three shows i.e 8-9,9-2,4-7 or 8-9,1-3,4-7
if we need to write a sample program to calculate the no of shows one can watch..how can we achieve that?
i thought of parsing each element of both the arrays and then taking end time of show from movie_end array and checking in movie_start array if show starts after that time and do that recursively..
Please suggest .
First.. Your arrays are confusing.Does 8 refer to 8am or 8pm. I am assuming you intend to use a 24 hr clock in your array. SO, it should be
Second, Are your arrays sorted by start time?
Third, Are you trying to maximize the number of movies, or the amount of time you spent watching movies?
Fourth, what have you tried already? forget about how you will do this in code. How will you do this on paper? You could approach this multiple ways:You could
One way is. FInd the first movie when I'm free. That moves my free time to end of the movie. Then find the next movie when I'm free and so on.. BUT!! It could be that the first movie that I'm free is 3 hours long, and there are 2 back to back 2-hour movies that start when the 3 hour movie starts. So, it's better for me to miss the 3 hour movie and try the next movie. So, what you need is multiple
Or try to watch all movies. Then see if any of the movies collide. If no movies collide, you have your answer. If movies collides, try to remove one movie, and see if it still collides. If nothing collides after removing one movie then you have your answer. If something collides, then put the movie back and remove the next movie. THe problem is if you get collisions even after removing each colliding movie. You have to remove 2 colliding movies. So, you have to repeat the process after removing 2 colliding movies.. and then 3 and then 4..
Your algorithm for computing the total watch time is wrong. It doesn;t take into account overlaps between movie times
Thank you for pointing that out as it won't prove to be helpful... Guess I was too eager to make a suggestion
The implementation is also too low level by using arrays I guess
Here is my attempt at providing an improved solution:
Improved Movie class:
Improved MovieMarathon class:
Improved Cinema test class:
I should mention again that this suggestion is not to be taken as a template to use but only as my thoughts on the design path I would have took at implementing the application instead of using arrays which could probably give the OP an insight on a different approach.