forked from Audacity21/DSA-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterval_scheduling.java
More file actions
70 lines (56 loc) · 1.46 KB
/
Interval_scheduling.java
File metadata and controls
70 lines (56 loc) · 1.46 KB
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
import java.io.*;
import java.lang.*;
import java.util.*;
class Interval_scheduling {
// Pair class
static class Pair {
int first;
int second;
Pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
static void SelectActivities(int s[], int f[])
{
// Vector to store results.
ArrayList<Pair> ans = new ArrayList<>();
// Minimum Priority Queue to sort activities in
// ascending order of finishing time (f[i]).
PriorityQueue<Pair> p = new PriorityQueue<>(
(p1, p2) -> p1.first - p2.first);
for (int i = 0; i < s.length; i++) {
// Pushing elements in priority queue where the
// key is f[i]
p.add(new Pair(f[i], s[i]));
}
Pair it = p.poll();
int start = it.second;
int end = it.first;
ans.add(new Pair(start, end));
while (!p.isEmpty()) {
Pair itr = p.poll();
if (itr.second >= end) {
start = itr.second;
end = itr.first;
ans.add(new Pair(start, end));
}
}
System.out.println(
"Following Activities should be selected. \n");
for (Pair itr : ans) {
System.out.println(
"Activity started at: " + itr.first
+ " and ends at " + itr.second);
}
}
// Driver Code
public static void main(String[] args)
{
int s[] = { 1, 3, 0, 5, 8, 5 };
int f[] = { 2, 4, 6, 7, 9, 9 };
// Function call
SelectActivities(s, f);
}
}