forked from anshuman8800/Interivew-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintervalscheduling.cpp
More file actions
85 lines (79 loc) · 2.1 KB
/
intervalscheduling.cpp
File metadata and controls
85 lines (79 loc) · 2.1 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits.\stdc++.h>
using namespace std;
int main()
{
int n;
cout << "Enter number of jobs" << endl;
cin >> n;
int f[100] = {}, s[100] = {};
int ft[100] = {};
for (int i = 0; i < n; i++)
{
cout << "Enter start time :";
cin >> s[i];
cout << "Enter Finish time: " << endl;
cin >> f[i];
ft[i] = f[i];
}
sort(ft, ft + n);
int k = 0;
int st[100] = {};
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (f[j] == ft[i])
{
st[k] = s[j];
f[j] = 0;
k++;
break;
}
}
}
cout << endl;
int i = 0;
cout << "Set of jobs with maximum cardinality which can be scheduled \n";
cout << st[0] << "," << ft[0] << " ";
for (int j = i + 1; j < n; j++)
{
if (st[j] > ft[i])
{
cout << "The starting and finishing time is of selected interval is: ";
cout << st[j] << "," << ft[j] << " ";
i = j;
}
}
cout << "\n";
int incomp = 0, comp = 0;
int maxcon = 0, mincon = 0, indmax = 0, indmin = 0;
for (int i = 0; i <= n - 1; i++)
{
incomp = 0;
comp = 0;
for (int j = i + 1; j < n; j++)
{
if (st[j] < ft[i])
{
incomp++;
}
else
{
comp++;
}
}
maxcon = max(maxcon, incomp);
mincon = max(mincon, comp);
if (maxcon == incomp)
{
indmax = i;
}
if (mincon == comp)
{
indmin = i;
}
cout << "For job " << i << " number of conflicting jobs are " << incomp << "\n";
}
cout << "Job with maximum conflicts is with starting time " << st[indmax] << " and finish time " << ft[indmax] << "\n";
cout << "Job with minimum conflicts is with starting time " << st[indmin] << " and finish time " << ft[indmin] << "\n";
}