-
Notifications
You must be signed in to change notification settings - Fork 33
/
Google_Hash_Code_2020.java
117 lines (94 loc) · 3.96 KB
/
Google_Hash_Code_2020.java
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
116
117
//This is my submission for the Google Hash Code 2020 Online Qualification Round. In fetched me a score of 15,435,636
//and a Global Rank of 5364 out of 10724 teams.
//A few modifiactions can be done to improve the code and score further.
import java.util.*;
//class to store details of each library
class library{
int id; //unique Library ID
int books; //number of books in library
int signup; //Sign up time
int booksPerDay; //number of books that can be processed per day
ArrayList<Integer> bookIDs; //IDs of books in the library
library(int i,int b, int s, int bp, ArrayList<Integer> bIDs){
id = i;
books = b;
signup = s;
booksPerDay = bp;
bookIDs = bIDs;
}
}
class SortbySignup implements Comparator<library>
{
// Used for sorting in ascending order of library sign up time
public int compare(library a, library b)
{
return a.signup - b.signup;
}
}
public class hc {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int totalBooks = s.nextInt();
int numOfLib = s.nextInt();
int totalDays = s.nextInt(); //total number of days where libraries can sign-up and books can be proceesed
int cost[] = new int[totalBooks]; //to store cost of each book
for(int i=0;i<totalBooks;i++) cost[i] = s.nextInt();
//to store Library Info with it's Unique ID
HashMap<Integer,library> map = new HashMap<Integer,library>();
//storing information of all libraries
for(int i=0;i<numOfLib;i++){
int books = s.nextInt(); //num of books in library
int signup = s.nextInt(); //Sign-up time of library
int bpd = s.nextInt(); //number of books that can be processed per day
//to store book IDs of books in the library
ArrayList<Integer> bid = new ArrayList<Integer>();
for(int j=0;j<books;j++){
bid.add(s.nextInt());
}
library lib = new library(i,books, signup, bpd, bid);
map.put(i,lib);
}
//sorting libraries by sign-up time
ArrayList<library> sortedBySignup = new ArrayList(map.values());
Collections.sort(sortedBySignup, new SortbySignup());
//Map to store Library ID and the number of days when the books can be shipped
HashMap<Integer,Integer> map2 = new HashMap<Integer,Integer>();
int val = totalDays;
int i = 0;
//calculating shipment days for each library
while(i<numOfLib && val>0){
library ll = sortedBySignup.get(i);
int remDays = val - ll.signup;
int shipment = remDays*ll.booksPerDay;
map2.put(ll.id,shipment);
val-=ll.signup;
i++;
}
//store library IDs to be displayed on console
ArrayList<Integer> sol = new ArrayList<Integer>();
int cntLibs = 0;
for(Map.Entry<Integer,Integer> entry : map2.entrySet()){
int ID = entry.getKey();
int ship = entry.getValue();
if(ship>0){ //to check for a valid value
cntLibs++;
sol.add(ID);
}
}
//Printing solution
System.out.println(cntLibs); //number of libraries
for(int j=0;j<cntLibs;j++){
int ID = sol.get(j);
int shipBooks = map2.get(ID);
library llll = map.get(ID);
ArrayList<Integer> bid = llll.bookIDs;
shipBooks = Math.min(bid.size(),shipBooks);
System.out.println(ID+" "+shipBooks);
for(int k=0;k<shipBooks;k++){
System.out.print(bid.get(k)+" ");
}
System.out.println();
}
}
}