-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathActivitySelectionProblem.java
83 lines (76 loc) · 2.12 KB
/
ActivitySelectionProblem.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*https://practice.geeksforgeeks.org/problems/n-meetings-in-one-room-1587115620/1/*/
class Solution
{
public static int maxMeetings(int start[], int end[], int n)
{
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < n; ++i)
{
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(start[i]);
temp.add(end[i]);
list.add(temp);
}
//sort according to the finish times
Collections.sort(list,new Comparator<ArrayList<Integer>>()
{
public int compare(ArrayList<Integer> l1, ArrayList<Integer> l2)
{
return l1.get(1)-l2.get(1);
}
});
//count
int count = 0;
int lastStart = -1, lastEnd = -1;
for (ArrayList<Integer> duration : list)
{
//if the current meeting starts after the last meeting ends
if (duration.get(0) > lastEnd)
{
//increment the count
++count;
//update the last meeting
lastStart = duration.get(0);
lastEnd = duration.get(1);
}
}
return count;
}
}
/*Pratik's Solution*/
class Activity implements Comparable<Activity>
{
int start;
int end;
Activity(int start,int end)
{
this.start = start;
this.end = end;
}
public int compareTo(Activity a)
{
return this.end-a.end;
}
}
class Solution
{
//Function to find the maximum number of meetings that can
//be performed in a meeting room.
public static int maxMeetings(int start[], int end[], int n)
{
// add your code here
PriorityQueue<Activity> heap = new PriorityQueue<Activity>();
for(int i=0;i<n;i++)
{
heap.add(new Activity(start[i],end[i]));
}
int k=0;
while(!heap.isEmpty())
{
Activity cur = heap.poll();
k++;
while(!heap.isEmpty()&&heap.peek().start<=cur.end)heap.poll();
}
return k;
}
}