Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5339fcc

Browse files
committed
inital commit
1 parent 64c2ac1 commit 5339fcc

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

‎Get_max_presentations.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /