File tree 1 file changed +38
-0
lines changed 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ This is an Activity Selection problem where we want to find the maximum number of non-overlapping presentations that can be attended.
3
+ Use Greedy approach to solve this problem.
4
+ - Sort the timings in the increasing order of their end times.
5
+ - Iterate through the sorted list and select presentations that start after or at the end of the last selected presentation.
6
+ - Count the number of such presentations.
7
+ - Return the count as the result.
8
+ """
9
+
10
+ def get_max_presentations (start_times , end_times ):
11
+ """
12
+ Calculates the maximum number of non-overlapping presentations that can be attended.
13
+
14
+ Args:
15
+ start_times (list of int): List of start times for each presentation.
16
+ end_times (list of int): List of end times for each presentation.
17
+
18
+ Returns:
19
+ int: The maximum number of non-overlapping presentations.
20
+ """
21
+ # Sort the presentations by their end times (greedy approach)
22
+ intervals = sorted (zip (start_times , end_times ), key = lambda x : x [1 ])
23
+ count = 0
24
+ prev_end = 0
25
+
26
+ # Iterate through each presentation
27
+ for start , end in intervals :
28
+ # If the current presentation starts after or at the end of the last selected one
29
+ if prev_end <= start :
30
+ count += 1
31
+ prev_end = end # Update the end time to the current presentation's end
32
+
33
+ return count
34
+
35
+ # Example usage
36
+ start = [1 , 1 , 2 , 3 ]
37
+ end = [2 , 3 , 3 , 4 ]
38
+ print (get_max_presentations (start , end ))
You can’t perform that action at this time.
0 commit comments