- Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathGoogle_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.
importjava.util.*;
//class to store details of each library
classlibrary{
intid; //unique Library ID
intbooks; //number of books in library
intsignup; //Sign up time
intbooksPerDay; //number of books that can be processed per day
ArrayList<Integer> bookIDs; //IDs of books in the library
library(inti,intb, ints, intbp, ArrayList<Integer> bIDs){
id = i;
books = b;
signup = s;
booksPerDay = bp;
bookIDs = bIDs;
}
}
classSortbySignupimplementsComparator<library>
{
// Used for sorting in ascending order of library sign up time
publicintcompare(librarya, libraryb)
{
returna.signup - b.signup;
}
}
publicclasshc {
publicstaticvoidmain(String[] args) {
// TODO Auto-generated method stub
Scanners = newScanner(System.in);
inttotalBooks = s.nextInt();
intnumOfLib = s.nextInt();
inttotalDays = s.nextInt(); //total number of days where libraries can sign-up and books can be proceesed
intcost[] = newint[totalBooks]; //to store cost of each book
for(inti=0;i<totalBooks;i++) cost[i] = s.nextInt();
//to store Library Info with it's Unique ID
HashMap<Integer,library> map = newHashMap<Integer,library>();
//storing information of all libraries
for(inti=0;i<numOfLib;i++){
intbooks = s.nextInt(); //num of books in library
intsignup = s.nextInt(); //Sign-up time of library
intbpd = s.nextInt(); //number of books that can be processed per day
//to store book IDs of books in the library
ArrayList<Integer> bid = newArrayList<Integer>();
for(intj=0;j<books;j++){
bid.add(s.nextInt());
}
librarylib = newlibrary(i,books, signup, bpd, bid);
map.put(i,lib);
}
//sorting libraries by sign-up time
ArrayList<library> sortedBySignup = newArrayList(map.values());
Collections.sort(sortedBySignup, newSortbySignup());
//Map to store Library ID and the number of days when the books can be shipped
HashMap<Integer,Integer> map2 = newHashMap<Integer,Integer>();
intval = totalDays;
inti = 0;
//calculating shipment days for each library
while(i<numOfLib && val>0){
libraryll = sortedBySignup.get(i);
intremDays = val - ll.signup;
intshipment = remDays*ll.booksPerDay;
map2.put(ll.id,shipment);
val-=ll.signup;
i++;
}
//store library IDs to be displayed on console
ArrayList<Integer> sol = newArrayList<Integer>();
intcntLibs = 0;
for(Map.Entry<Integer,Integer> entry : map2.entrySet()){
intID = entry.getKey();
intship = 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(intj=0;j<cntLibs;j++){
intID = sol.get(j);
intshipBooks = map2.get(ID);
libraryllll = map.get(ID);
ArrayList<Integer> bid = llll.bookIDs;
shipBooks = Math.min(bid.size(),shipBooks);
System.out.println(ID+" "+shipBooks);
for(intk=0;k<shipBooks;k++){
System.out.print(bid.get(k)+" ");
}
System.out.println();
}
}
}