-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreedyWeights_2.java
More file actions
115 lines (96 loc) · 3.26 KB
/
Copy pathgreedyWeights_2.java
File metadata and controls
115 lines (96 loc) · 3.26 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package MyPractice;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.channels.InterruptedByTimeoutException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
public class greedyWeights_2 {
static List<Jobs> jobList = new ArrayList<>();
public static class Jobs
{
long weight;
long length;
double ratio;
public Jobs(long weight,long length)
{
this.weight=weight;
this.length=length;
this.ratio=(double)weight/length;
}
}
public static void main(String...args) {
// int completionTime=0;
// int wcomsum=0;
// PriorityQueue<Jobs> pq = new PriorityQueue<>(4,new JobComparator());
// Jobs job1=new Jobs(100,3);
// pq.add(job1);
// Jobs job2=new Jobs(100,30);
// pq.add(job2);
// Jobs job3=new Jobs(5,1);
// pq.add(job3);
// Jobs job4=new Jobs(8,7);
// pq.add(job4);
//
// while(!pq.isEmpty())
// {
// Jobs s = pq.poll();
// System.out.println((s.weight-s.length) + " with weight : " + s.weight);
// completionTime=completionTime+s.length;
// int weighedCompletionTime = s.weight*completionTime;
// wcomsum+=weighedCompletionTime;
// }
readFromFile();
PriorityQueue<Jobs> pq = new PriorityQueue<>(10000,new JobComparator());
for(Jobs j : jobList)
{
pq.add(j);
}
long completionTime=0;
long wcomsum=0;
while(!pq.isEmpty())
{
Jobs s = pq.poll();
// System.out.println(" with weight : " + s.weight + " and length " + s.length);
System.out.println((s.weight/s.length) + " with weight : " + s.weight + " and length " + s.length);
completionTime=completionTime+s.length;
System.out.println("Completion time : " + completionTime);
long weighedCompletionTime = s.weight*completionTime;
wcomsum+=weighedCompletionTime;
System.out.println("Wcomsum : " + wcomsum);
}
// System.out.println("Answer : " + wcomsum);
}
public static class JobComparator implements Comparator<Jobs>
{
@Override
public int compare(Jobs a,Jobs b)
{
if(a.ratio<b.ratio)
return 1;
else if(a.ratio==b.ratio)
return 0;
else
return -1;
}
}
public static void readFromFile() {
try {
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\gupta\\IdeaProjects\\untitled\\src\\MyPractice\\jobCompletion1.txt"));
String line = reader.readLine();
// line = line.replaceAll("\s+","t");
while (line != null) {
String [] values = line.split("\s");
long weight = Long.parseLong(values[0]);
long length = Long.parseLong(values[1]);
jobList.add(new Jobs( weight,length));
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}