Simplify minimizing lateness (#4999)
This commit is contained in:
parent
704b5878b6
commit
0c881e39f2
@ -0,0 +1,43 @@
|
|||||||
|
package com.thealgorithms.greedyalgorithms;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class MinimizingLateness {
|
||||||
|
|
||||||
|
public static class Job {
|
||||||
|
String jobName;
|
||||||
|
int startTime = 0;
|
||||||
|
int lateness = 0;
|
||||||
|
int processingTime;
|
||||||
|
int deadline;
|
||||||
|
|
||||||
|
public Job(String jobName, int processingTime, int deadline) {
|
||||||
|
this.jobName = jobName;
|
||||||
|
this.processingTime = processingTime;
|
||||||
|
this.deadline = deadline;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Job of(String jobName, int processingTime, int deadline) {
|
||||||
|
return new Job(jobName, processingTime, deadline);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("%s, startTime: %d, endTime: %d, lateness: %d", jobName, startTime, processingTime + startTime, lateness);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void calculateLateness(Job... jobs) {
|
||||||
|
|
||||||
|
// sort the jobs based on their deadline
|
||||||
|
Arrays.sort(jobs, (a, b) -> a.deadline - b.deadline);
|
||||||
|
|
||||||
|
int startTime = 0;
|
||||||
|
|
||||||
|
for (Job job : jobs) {
|
||||||
|
job.startTime = startTime;
|
||||||
|
startTime += job.processingTime;
|
||||||
|
job.lateness = Math.max(0, startTime - job.deadline); // if the job finishes before deadline the lateness is 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,55 +0,0 @@
|
|||||||
package com.thealgorithms.minimizinglateness;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.FileReader;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.StringTokenizer;
|
|
||||||
|
|
||||||
public class MinimizingLateness {
|
|
||||||
|
|
||||||
private static class Schedule { // Schedule class
|
|
||||||
|
|
||||||
int t = 0; // Time required for the operation to be performed
|
|
||||||
int d = 0; // Time the job should be completed
|
|
||||||
public Schedule(int t, int d) {
|
|
||||||
this.t = t;
|
|
||||||
this.d = d;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
|
||||||
StringTokenizer token;
|
|
||||||
|
|
||||||
BufferedReader in = new BufferedReader(new FileReader("MinimizingLateness/lateness_data.txt"));
|
|
||||||
String ch = in.readLine();
|
|
||||||
if (ch == null || ch.isEmpty()) {
|
|
||||||
in.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int indexCount = Integer.parseInt(ch);
|
|
||||||
System.out.println("Input Data : ");
|
|
||||||
System.out.println(indexCount); // number of operations
|
|
||||||
Schedule[] array = new Schedule[indexCount]; // Create an array to hold the operation
|
|
||||||
int i = 0;
|
|
||||||
while ((ch = in.readLine()) != null) {
|
|
||||||
token = new StringTokenizer(ch, " ");
|
|
||||||
// Include the time required for the operation to be performed in the array and the time
|
|
||||||
// it should be completed.
|
|
||||||
array[i] = new Schedule(Integer.parseInt(token.nextToken()), Integer.parseInt(token.nextToken()));
|
|
||||||
i++;
|
|
||||||
System.out.println(array[i - 1].t + " " + array[i - 1].d);
|
|
||||||
}
|
|
||||||
|
|
||||||
int tryTime = 0; // Total time worked
|
|
||||||
int lateness = 0; // Lateness
|
|
||||||
for (int j = 0; j < indexCount - 1; j++) {
|
|
||||||
tryTime = tryTime + array[j].t; // Add total work time
|
|
||||||
// Lateness
|
|
||||||
lateness = lateness + Math.max(0, tryTime - array[j].d);
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
System.out.println("Output Data : ");
|
|
||||||
System.out.println(lateness);
|
|
||||||
in.close();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
6
|
|
||||||
3 6
|
|
||||||
2 8
|
|
||||||
1 9
|
|
||||||
4 9
|
|
||||||
3 14
|
|
||||||
2 15
|
|
@ -0,0 +1,43 @@
|
|||||||
|
package com.thealgorithms.greedyalgorithms;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import com.thealgorithms.greedyalgorithms.MinimizingLateness.Job;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class MinimizingLatenessTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCalculateLateness() {
|
||||||
|
// Test case with three jobs
|
||||||
|
Job job1 = new Job("Job1", 4, 6);
|
||||||
|
Job job2 = new Job("Job2", 2, 8);
|
||||||
|
Job job3 = new Job("Job3", 1, 9);
|
||||||
|
Job job4 = new Job("Job4", 5, 9);
|
||||||
|
Job job5 = new Job("Job5", 4, 10);
|
||||||
|
Job job6 = new Job("Job6", 3, 5);
|
||||||
|
|
||||||
|
MinimizingLateness.calculateLateness(job1, job2, job3, job4, job5, job6);
|
||||||
|
|
||||||
|
// Check lateness for each job
|
||||||
|
assertEquals(6, job4.lateness);
|
||||||
|
assertEquals(0, job6.lateness);
|
||||||
|
assertEquals(1, job2.lateness);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCheckStartTime() {
|
||||||
|
|
||||||
|
Job job1 = new Job("Job1", 2, 5);
|
||||||
|
Job job2 = new Job("Job2", 1, 7);
|
||||||
|
Job job3 = new Job("Job3", 3, 8);
|
||||||
|
Job job4 = new Job("Job4", 2, 4);
|
||||||
|
Job job5 = new Job("Job5", 4, 10);
|
||||||
|
|
||||||
|
MinimizingLateness.calculateLateness(job1, job2, job3, job4, job5);
|
||||||
|
|
||||||
|
assertEquals(2, job1.startTime);
|
||||||
|
assertEquals(5, job3.startTime);
|
||||||
|
assertEquals(8, job5.startTime);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user