Skip to content

Commit 1103c9c

Browse files
committed
Added Solution - GfG to GitHub
1 parent cc04630 commit 1103c9c

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//{ Driver Code Starts
2+
// Initial Template for Java
3+
import java.util.*;
4+
5+
6+
// } Driver Code Ends
7+
8+
class Solution {
9+
public ArrayList<Integer> jobSequencing(int[] d, int[] p) {
10+
int n = d.length, cnt = 0, tot = 0;
11+
PriorityQueue<Integer> pq = new PriorityQueue<>();
12+
List<int[]> jobs = new ArrayList<>();
13+
14+
for (int i = 0; i < n; i++) jobs.add(new int[] {d[i], p[i]});
15+
jobs.sort(Comparator.comparingInt(a -> a[0]));
16+
17+
for (int[] job : jobs) {
18+
if (job[0] > pq.size()) pq.add(job[1]);
19+
else if (pq.peek() < job[1]) { pq.poll(); pq.add(job[1]); }
20+
}
21+
22+
cnt = pq.size();
23+
while (!pq.isEmpty()) tot += pq.poll();
24+
25+
return new ArrayList<>(Arrays.asList(cnt, tot));
26+
}
27+
}
28+
29+
//{ Driver Code Starts.
30+
31+
public class Main {
32+
public static void main(String[] args) {
33+
Scanner sc = new Scanner(System.in);
34+
int t = Integer.parseInt(sc.nextLine().trim());
35+
36+
while (t-- > 0) {
37+
String[] deadlineInput = sc.nextLine().trim().split("\\s+");
38+
int[] deadline =
39+
Arrays.stream(deadlineInput).mapToInt(Integer::parseInt).toArray();
40+
41+
String[] profitInput = sc.nextLine().trim().split("\\s+");
42+
int[] profit =
43+
Arrays.stream(profitInput).mapToInt(Integer::parseInt).toArray();
44+
Solution obj = new Solution();
45+
ArrayList<Integer> result = obj.jobSequencing(deadline, profit);
46+
System.out.println(result.get(0) + " " + result.get(1));
47+
System.out.println("~");
48+
}
49+
50+
sc.close();
51+
}
52+
}
53+
// } Driver Code Ends

0 commit comments

Comments
 (0)