Skip to content

Commit 1b79528

Browse files
authored
Merge pull request #914 from dsrao711/issue_913
Python solution for activity selection in DSA 450
2 parents 1a45bee + 09505d7 commit 1b79528

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

DSA 450 GFG/ActivitySelection.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Link : https://practice.geeksforgeeks.org/problems/n-meetings-in-one-room-1587115620/1
2+
3+
# Input:
4+
# N = 6
5+
# start[] = {1,3,0,5,8,5}
6+
# end[] = {2,4,6,7,9,9}
7+
# Output:
8+
# 4
9+
10+
# Meetings = [(1,2) , (3,4) , (0,6) , (5,7) , (8,9) , (5,9)]
11+
# Required Output - > Get the max number of meetings that can be scheduled from the timings given
12+
13+
# Get the maximum number of short meetings from the list of meetings
14+
15+
# Sorting The meetings wrt Start time -> [(0,6),(1,2),(3,4),(5,7),(5,9)]
16+
# O/p = (0,6) => 1 meeting can be scheduled ad the end time of the meeting is 6 and there is no meeting
17+
# starting after 6 , so Op is 1
18+
19+
# Sorting the meetings wrt End Time , Start time-> [(1,2) , (3,4) , (0,6) , (5,7) , (5,9) , (8,9) ]
20+
21+
# Op = [(1,2) , (3,4) , (5,7) , (8,9) ] => 4
22+
23+
# Condition for selecting a meeting - > Start time of the meeting should be greater than
24+
# the end time of the previous meeting
25+
26+
# Approach :
27+
28+
# Variables :
29+
# i = Track the previous meetings -> i = 0
30+
# j = Track the current meetings -> j = 1
31+
# counter = Count of number of meetings that can be scheduled -> counter = 1
32+
# By default the first meeting will be scheduled , so counter initialized with value 1
33+
34+
# 1. Make a list of Meetings with start time and end time
35+
# 2. Sort the list wrt start time and then sort it wrt to end time
36+
# 3. Iterate through the meetings and check the condition that whether the start time of the current
37+
# meeting is greater than the end time of the previous meeting
38+
39+
# for j in range(1, n):
40+
# if(Meetings[j][0] > Meetings[i][1]):
41+
# counter ++
42+
# i = j
43+
44+
# 4. Return counter
45+
46+
47+
import sys
48+
import io
49+
50+
class Solution:
51+
52+
#Function to find the maximum number of meetings that can
53+
#be performed in a meeting room.
54+
def maximumMeetings(self,n,start,end):
55+
# code here
56+
Meetings = []
57+
for i in range(n):
58+
Meetings.append([start[i] , end[i]])
59+
60+
Meetings.sort(key = lambda x : x[0])
61+
Meetings.sort(key = lambda x : x[1])
62+
# To track the previous meeting
63+
i = 0
64+
# To track the current meeting
65+
j = 1
66+
# First meeting always gets selected
67+
counter = 1
68+
69+
for j in range(1,n):
70+
# Condition for scheduling
71+
if(Meetings[j][0] > Meetings[i][1]):
72+
counter += 1
73+
i = j
74+
75+
# print(Meetings)
76+
return counter
77+
78+
if __name__ == '__main__':
79+
test_cases = int(input())
80+
for cases in range(test_cases):
81+
n = int(input())
82+
start_time = list(map(int , input().strip().split()))
83+
end_time = list(map(int , input().strip().split()))
84+
ob = Solution()
85+
print(ob.maximumMeetings(n,start_time , end_time))

0 commit comments

Comments
 (0)