Count Distinct Hat Assignments and Maximize Hat Points
RobloxSoftware Engineer · OnsiteLast updated
Count valid one-hat-per-person assignments and maximize assigned-hat points with exact counts, unique hat use, and bitmask or backtracking reasoning.
Each person lists the hats they are willing to wear. Assign one acceptable hat to every person, using each hat at most once. Every hat also has a point value. Return both the total number of valid assignments and the maximum total points among valid assignments.
Implement hat_assignments(preferences: int[][], points: int[]) -> int[], returning [number_of_assignments, maximum_points].
Constraints & Assumptions
- Hat IDs are
1throughH, whereH == len(points)andpoints[h-1]is hath's point value. 1 <= H <= 12; there are 0 through 8 people. These small practice bounds allow an exact count without a modulus and keep results within signed 32-bit integers.- Each preference list contains distinct valid hat IDs and may be empty.
- Hat points are nonnegative integers at most 1,000,000. An assignment's score is the sum of points of its assigned hats, counted once per used hat.
- People are distinct, so exchanging two acceptable hats between people creates a different assignment even if the total points stay equal.
- If no complete assignment exists, return
[0, -1]. With no people, the one empty assignment has score zero, so return[1, 0]. - Do not count partial assignments or require every available hat to be used.
Examples
preferences = [[1,2],[2,3]]
points = [5,2,9]
result = [3,14]
The assignments are (1,2), (1,3), and (2,3). Their scores are 7, 14, and 11.
preferences = [[1],[1]]
points = [8]
result = [0,-1]
Explain how the counting state and maximum-score state are combined without counting an assignment twice. Compare bounded backtracking with a bitmask dynamic program under the stated small sizes.
Hints
Hint 1
Separate ways from best score: Two partial assignments may reach the same availability state. Their counts should be added, while their best achievable scores require a maximum rather than addition.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.