programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Junilu Lacar
• Liutauras Vilda
Sheriffs:
• Paul Clapham
• Jeanne Boyarsky
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Piet Souris
• Carey Brown
Bartenders:
• Jesse Duncan
• Frits Walraven
• Mikalai Zaikin

# Movie marathon - Maximum how many shows one can watch?

Ranch Hand
Posts: 37
• Number of slices to send:
Optional 'thank-you' note:
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 .

Rancher
Posts: 2759
32
• Number of slices to send:
Optional 'thank-you' note:
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..

lowercase baba
Posts: 13056
67
• Number of slices to send:
Optional 'thank-you' note:
side note: parallel arrays like this are a terrible design. They are prone to errors and getting out of sync. Now, you may have been handed this and are stuck with it, but if not, re-design that NOW.

Ranch Hand
Posts: 411
5
• Number of slices to send:
Optional 'thank-you' note:
Since you are using an object-oriented programming language it would be better to use the programming style that the language supports...

Although the following is not a suggested template, I would have gone in this direction:

Create a Movie class to represent movie objects that contain movie information:

Create a class to represent the marathon event holding relevant information and methods:

The following class is just a quick test:

The main point is you should consider using object notation to design your application by creating the necessary classes to represent the relevant objects that interact within your system...

Jayesh A Lalwani
Rancher
Posts: 2759
32
• Number of slices to send:
Optional 'thank-you' note:
Rico,

Your algorithm for computing the total watch time is wrong. It doesn;t take into account overlaps between movie times

Rico Felix
Ranch Hand
Posts: 411
5
• Number of slices to send:
Optional 'thank-you' note:

Jayesh A Lalwani wrote:Rico,

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.

Marshal
Posts: 75867
361
• Number of slices to send:
Optional 'thank-you' note:
Pleased to see the underscores are taken out of the field identifiers.

 pie. tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
Similar Threads