Editorial for Very Handy Danny
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
For both subtasks, we imploy a greedy solution where we add the next largest book to the smallest hand. Alternatively, we can add the next smallest book to the largest hand. Both solutions are equivalent
Subtask 1
Subtask 1 was included for solutions which did not use a priority queue to store the total weights of each hand, but instead iterated through all hands to find the largest/smallest hand.
Time complexity: \(O(N\log N + NH) = O(N^2)\)
Subtask 2
Store the total weight of each hand in an ascending priority queue. Sort the weights and iterate from the largest weight to the smallest. Add the next largest weight to the hand at the top of the priority queue.
Time complexity: \(O(N \log N + N \log H + H \log H) = O(N \log N)\)
Comments