File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Movie marathon - Maximum how many shows one can watch? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Movie marathon - Maximum how many shows one can watch?" Watch "Movie marathon - Maximum how many shows one can watch?" New topic
Author

Movie marathon - Maximum how many shows one can watch?

kuldeep sidhu
Greenhorn

Joined: Jan 07, 2014
Posts: 18
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 .
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2431
    
  28

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..
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rico Felix
Ranch Hand

Joined: Mar 08, 2014
Posts: 302
    
    4

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
Bartender

Joined: Jan 17, 2008
Posts: 2431
    
  28

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

Joined: Mar 08, 2014
Posts: 302
    
    4

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Pleased to see the underscores are taken out of the field identifiers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Movie marathon - Maximum how many shows one can watch?