using System; using System.Collections.Generic; class Job { public char Id; public int Deadline; public int Profit; public Job(char id, int deadline, int profit) { Id = id; Deadline = deadline; Profit = profit; } } class Program { static void Main() { List<Job> jobs = new List<Job> { new Job('a', 2, 100), new Job('b', 1, 19), new Job('c', 2, 27), new Job('d', 1, 25), new Job('e', 3, 15) }; jobs.Sort((x, y) => y.Profit.CompareTo(x.Profit)); int n = jobs.Count; char[] result = new char[n]; bool[] slot = new bool[n]; for (int i = 0; i < n; i++) { for (int j = Math.Min(n, jobs[i].Deadline) - 1; j >= 0; j--) { if (!slot[j]) { slot[j] = true; result[j] = jobs[i].Id; break; } } } for (int i = 0; i < n; i++) { if (slot[i]) Console.Write(result[i] + " "); } }
The code sorts jobs by profit descending: a(100), c(27), d(25), b(19), e(15).
It tries to schedule each job in the latest available slot before its deadline.
Slots: 0,1,2 (3 slots for 3 max deadline).
Job 'a' (deadline 2) goes to slot 1.
Job 'c' (deadline 2) goes to slot 0.
Job 'd' (deadline 1) cannot fit (slot 0 taken), skipped.
Job 'b' (deadline 1) cannot fit, skipped.
Job 'e' (deadline 3) goes to slot 2.
Output is 'c a e '.
Jobs: Job A: deadline=2, profit=100 Job B: deadline=1, profit=19 Job C: deadline=2, profit=27 Job D: deadline=1, profit=25 Job E: deadline=3, profit=15
The maximum deadline is 3, so at most 3 jobs can be scheduled.
By scheduling jobs greedily by profit, 3 jobs fit without deadline conflicts.
using System; using System.Collections.Generic; class Job { public char Id; public int Deadline; public int Profit; public Job(char id, int deadline, int profit) { Id = id; Deadline = deadline; Profit = profit; } } class Program { static void Main() { List<Job> jobs = new List<Job> { new Job('a', 2, 100), new Job('b', 1, 19), new Job('c', 3, 27) }; jobs.Sort((x, y) => y.Profit.CompareTo(x.Profit)); int n = jobs.Count; char[] result = new char[n]; bool[] slot = new bool[n]; for (int i = 0; i < n; i++) { for (int j = jobs[i].Deadline; j >= 0; j--) { if (!slot[j]) { slot[j] = true; result[j] = jobs[i].Id; break; } } } for (int i = 0; i < n; i++) { if (slot[i]) Console.Write(result[i] + " "); } }
The inner loop starts at jobs[i].Deadline which can be equal to n (array length), causing index out of range when accessing slot[j].
Array indices go from 0 to n-1, so j should start at Math.Min(n, jobs[i].Deadline) - 1.
var jobs = new List<Job> {
new Job('a', 2, 100),
new Job('b', 1, 19),
new Job('c', 2, 27)
};
jobs.Sort((x, y) => y.Profit.CompareTo(x.Profit));
int n = jobs.Count;
char[] result = new char[n];
bool[] slot = new bool[n];
for (int i = 0; i < n; i++) {
for (int j = Math.Min(n, jobs[i].Deadline) - 1; j >= 0; j--) {
if (!slot[j]) {
slot[j] = true;
result[j] = jobs[i].Id;
break;
}
}
The line 'result[j] = jobs[i].Id' is missing a semicolon at the end, causing a syntax error.
Adding the semicolon fixes the syntax error.
Jobs: Job A: deadline=4, profit=70 Job B: deadline=1, profit=80 Job C: deadline=1, profit=30 Job D: deadline=2, profit=100 Job E: deadline=3, profit=40 Job F: deadline=2, profit=20
Sort jobs by profit descending: D(100), B(80), A(70), E(40), C(30), F(20).
Schedule D at slot 1 (deadline 2), B at slot 0 (deadline 1), A at slot 3 (deadline 4), E at slot 2 (deadline 3).
Jobs C and F cannot be scheduled due to slot conflicts.
Total profit = 100 + 80 + 70 + 40 = 290.
