Amazon Online Assessment (OA) - Movies on Flight
You are on a flight and want to watch two movies during this flight.
You are given a list of integers
that includes all the movie durations and also given the duration of the flight which is d
in minutes.
Now, you need to pick two movies and the total duration of the two movies must be less than or equal to (d - 30min)
.
Find the pair of movies with the longest total duration. If multiple are found, return the pair with the longest movie.
Input
The input consists of two arguments:
movie_duration
: a list of integers
representing the duration of movies
d
: an integer
representing the duration of the flight
Output
return the movie pair.
Examples
Example 1:
Input:
movie_duration = [90, 85, 75, 60, 120, 150, 125]
d = 250
Output: [90, 125]
Explanation:
90min + 125min = 215
is the maximum sum within 220
(250min - 30min)
Try it yourself
Solution
1def movies_on_flight(movie_duration: list[int], d: int) -> list[int]:
2 def search():
3 s = sorted(movie_duration)
4 l = 0
5 r = len(s) - 1
6 while l < r:
7 if s[l] + s[r] <= d - 30:
8 yield s[l], s[r]
9 l += 1
10 else:
11 r -= 1
12
13 return max(search(), key=sum)
14
15if __name__ == "__main__":
16 movie_duration = [int(x) for x in input().split()]
17 d = int(input())
18 res = movies_on_flight(movie_duration, d)
19 print(" ".join(map(str, res)))
20