diff --git a/Google_Hash_Code_2020.java b/Google_Hash_Code_2020.java index 3e71c5c..e58000d 100644 --- a/Google_Hash_Code_2020.java +++ b/Google_Hash_Code_2020.java @@ -1,117 +1,105 @@ -//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. +import java.util.*; -//A few modifiactions can be done to improve the code and score further. +// Class representing a Library +class Library { + int id; + int books; + int signup; + int booksPerDay; + ArrayList bookIDs; -import java.util.*; + Library(int id, int books, int signup, int booksPerDay, ArrayList bookIDs) { + this.id = id; + this.books = books; + this.signup = signup; + this.booksPerDay = booksPerDay; + this.bookIDs = bookIDs; + } +} -//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 bookIDs; //IDs of books in the library - library(int i,int b, int s, int bp, ArrayList bIDs){ - id = i; - books = b; - signup = s; - booksPerDay = bp; - bookIDs = bIDs; +// Comparator for sorting libraries by signup time (greedy heuristic) +class SortBySignup implements Comparator { + public int compare(Library a, Library b) { + return a.signup - b.signup; } } -class SortbySignup implements Comparator -{ - // 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) { + Scanner sc = new Scanner(System.in); -public class hc { + int totalBooks = sc.nextInt(); + int numOfLib = sc.nextInt(); + int totalDays = sc.nextInt(); + + int[] bookScore = new int[totalBooks]; + + for (int i = 0; i < totalBooks; i++) { + bookScore[i] = sc.nextInt(); + } + + // Store libraries + HashMap map = new HashMap<>(); + + for (int i = 0; i < numOfLib; i++) { + + int books = sc.nextInt(); + int signup = sc.nextInt(); + int bpd = sc.nextInt(); + + ArrayList bid = new ArrayList<>(); + + for (int j = 0; j < books; j++) { + bid.add(sc.nextInt()); + } + + map.put(i, new Library(i, books, signup, bpd, bid)); + } + + // Convert to list for sorting + ArrayList libs = new ArrayList<>(map.values()); + libs.sort(new SortBySignup()); - 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 map = new HashMap(); - - //storing information of all libraries - for(int i=0;i bid = new ArrayList(); - - for(int j=0;j 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 map2 = new HashMap(); - int val = totalDays; - int i = 0; - - //calculating shipment days for each library - while(i0){ - 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 sol = new ArrayList(); - int cntLibs = 0; - - - for(Map.Entry 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 bid = llll.bookIDs; - shipBooks = Math.min(bid.size(),shipBooks); - System.out.println(ID+" "+shipBooks); - - for(int k=0;k selectedLibs = new ArrayList<>(); + int remainingDays = totalDays; + + for (Library lib : libs) { + + if (remainingDays <= lib.signup) break; + + remainingDays -= lib.signup; + + int shipCapacity = remainingDays * lib.booksPerDay; + + if (shipCapacity > 0) { + selectedLibs.add(lib.id); + } + } + + // Output + System.out.println(selectedLibs.size()); + + for (int id : selectedLibs) { + + Library lib = map.get(id); + + int remainingDaysAfterSignup = totalDays - lib.signup; + int shipCapacity = remainingDaysAfterSignup * lib.booksPerDay; + + shipCapacity = Math.min(shipCapacity, lib.bookIDs.size()); + + System.out.println(id + " " + shipCapacity); + + for (int k = 0; k < shipCapacity; k++) { + System.out.print(lib.bookIDs.get(k) + " "); + } + System.out.println(); + } + + sc.close(); + } }