-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMovieNight.java
55 lines (47 loc) · 1.39 KB
/
MovieNight.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package by.andd3dfx.common;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* <pre>
* Implement the canViewAll() method, which given a collection of movies, checks if they all can be
* viewed completely without overlap.
*
* For example, for the movies below, the method should return true because they don't overlap:
* - 1/1/2015 20:00-21:30
* - 1/1/2015 23:10-23:30
* - 1/1/2015 21:30-23:00
* </pre>
*
* @see <a href="https://youtu.be/2hGoj3v5JVQ">Video solution</a>
*/
public class MovieNight {
public static boolean canViewAll(List<Movie> movies) {
movies = new ArrayList<>(movies);
Collections.sort(movies);
for (int i = 1; i < movies.size(); i++) {
if (movies.get(i - 1).end.after(movies.get(i).start)) {
return false;
}
}
return true;
}
@AllArgsConstructor
@Getter
public static class Movie implements Comparable<Movie> {
private Date start, end;
@Override
public int compareTo(Movie movie) {
if (start.before(movie.start)) {
return -1;
}
if (start.after(movie.start)) {
return 1;
}
return 0;
}
}
}