BackmediumArraysFlipkart

Minimum Unique Material Identifiers Solution

Problem Statement

Given a 2D array rooms where each sub-array represents the available materials for a 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
[[1, 2, 3], [4, 5, 6], [1, 2, 3]]
Output
3

Explanation: Step-by-step: We have three rooms with materials [1, 2, 3], [4, 5, 6], and [1, 2, 3]. We need to assign unique IDs to each room. The unique IDs are [1, 2, 3, 4, 5, 6]. Since there are 6 unique IDs, we need to assign 3 new IDs to the third room, making the total minimum number of new or existing material IDs to assign to each room 3.

Example 2
Input
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output
9

Explanation: Step-by-step: We have three rooms with materials [1, 2, 3], [4, 5, 6], and [7, 8, 9]. We need to assign unique IDs to each room. The unique IDs are [1, 2, 3, 4, 5, 6, 7, 8, 9]. Since there are 9 unique IDs, we don't need to assign any new IDs to the third room, making the total minimum number of new or existing material IDs to assign to each room 9.

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 Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Minimum Unique Material Identifiers — Problem Statement & Solution Guide

ArraysMediumNEW OR EXISTING ID
TimeO(n*m)
|
SpaceO(n*m)

Problem Description

Given a 2D array rooms where each sub-array represents the available materials for a 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.

Examples

Example 1

Input

[[1, 2, 3], [4, 5, 6], [1, 2, 3]]

Output

3

Explanation: Step-by-step: We have three rooms with materials [1, 2, 3], [4, 5, 6], and [1, 2, 3]. We need to assign unique IDs to each room. The unique IDs are [1, 2, 3, 4, 5, 6]. Since there are 6 unique IDs, we need to assign 3 new IDs to the third room, making the total minimum number of new or existing material IDs to assign to each room 3.

Example 2

Input

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Output

9

Explanation: Step-by-step: We have three rooms with materials [1, 2, 3], [4, 5, 6], and [7, 8, 9]. We need to assign unique IDs to each room. The unique IDs are [1, 2, 3, 4, 5, 6, 7, 8, 9]. Since there are 9 unique IDs, we don't need to assign any new IDs to the third room, making the total minimum number of new or existing material IDs to assign to each room 9.

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

Optimal Approach & Strategy

The optimal approach is to use a hash map to keep track of the next available ID for each material type across all rooms. This allows us to efficiently assign new IDs without having to check for existing IDs in each room.

Brute Force Approach

One brute-force approach is to iterate through all the rooms and materials, and for each pair, check if the material has already been assigned to the room. If it has, assign a new ID. This approach results in a time complexity of O(n^2) where n is the number of materials.

Verified Code Solutions

JavaScript Solution
Time: O(n*m)
function minUniqueMaterialIds(rooms) { let map = new Map(); let id = 1; for (let room of rooms) { let roomMap = new Map(); for (let material of room) { if (!roomMap.has(material)) { roomMap.set(material, id); id++; } } for (let [material, roomId] of roomMap) { if (!map.has(material)) { map.set(material, roomId); } } } let uniqueIds = [...map.values()]; return uniqueIds.length; }

Asked in Top Tech Interviews

Flipkart

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.