BackmediumArrays Atlassian

Renovating Homes with Limited Materials Solution

Problem Statement

Given a list of rooms and the materials available for each room, determine the minimum number of new or existing material IDs to assign to each room so that no two rooms with the same type of material have the same material ID.

Example 1
Input
{"Kitchen": {"paint": 1, "flooring": 1}, "Living Room": {"chair": 1, "paint": 1}}
Output
{"Kitchen": {"paint": 1, "flooring": 1}, "Living Room": {"chair": 1, "paint": 2}}

Explanation: Step-by-step: Given the input, we first create a dictionary to store the count of each material type. Then, we iterate over each room and its materials. If a material type is already in the count dictionary, we assign a new ID to it. Finally, we return the updated dictionary.

Example 2
Input
{"Bedroom": {"bed": 1, "paint": 1}, "Bathroom": {"bed": 1, "paint": 1}}
Output
{"Bedroom": {"bed": 1, "paint": 1}, "Bathroom": {"bed": 2, "paint": 2}}

Explanation: Step-by-step: Given the input, we first create a dictionary to store the count of each material type. Then, we iterate over each room and its materials. If a material type is already in the count dictionary, we assign a new ID to it. Finally, we return the updated dictionary.

Constraints

  • 1 ≤ rooms.length ≤ 10^4
  • 1 ≤ materialIds.length < 10^5
  • Each room has at least one material
  • Each material can be assigned to any number of rooms
  • Material IDs start at 1 and increase by 1
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free