File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Movie marathon - Maximum how many shows one can watch?

 
kuldeep sidhu
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rico Felix
Ranch Hand
Posts: 407
5
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 407
5
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pleased to see the underscores are taken out of the field identifiers.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic