Understand Number Container System Problem

Problem Name: Number Container System
Problem Description:

Number Container System

Design a system that stores numbers in "containers" (indexed slots) and supports two main operations:

1.change(index, number):
Set the container at the specified index to the given number. If a container already has a number, replace it with the new number.

  1. find(number):
    Return the smallest index that currently contains the specified number. If no container holds that number, return -1.

How It Works

  • Initialization:
    When the system is created, all containers are empty.
  • Updating a Container:
    When you call change(index, number), the system stores number at that index. If the container already held a number, it is replaced.
  • Searching for a Number:
    When you call find(number), the system searches for all containers containing that number and returns the smallest index among them. If none is found, it returns -1.

Example Test Cases

Example 1

Input:

["NumberContainers", "change", "change", "find", "change", "find"] [[], [2, 10], [5, 10], [10], [2, 20], [10]]

Expected Output:

[null, null, null, 2, null, 5]

Step-by-Step Explanation:

  1. Initialization:
    • Create a new NumberContainers instance.
  2. Operation: change(2, 10)
    • Set the container at index 2 to 10.
  3. Operation: change(5, 10)
    • Set the container at index 5 to 10.
  4. Operation: find(10)
    • Check all containers for the number 10.
    • Containers 2 and 5 contain 10.
    • The smallest index among these is 2.
    • Output: 2
  5. Operation: change(2, 20)
    • Update the container at index 2 to 20.
    • Now, container 2 no longer holds 10.
  6. Operation: find(10)
    • Check all containers for the number 10.
    • Only container 5 now holds 10.
    • Output: 5

Alternative Example 2

Input:

["NumberContainers", "change", "find", "change", "change", "find", "change", "find", "find"] [[], [3, 15], [15], [7, 15], [3, 20], [15], [7, 30], [15], [20]]

Expected Output:

[null, null, 3, null, null, 7, null, -1, 3]

Step-by-Step Explanation:

  1. Initialization:
    • Create a new NumberContainers instance.
  2. Operation: change(3, 15)
    • Set the container at index 3 to 15.
  3. Operation: find(15)
    • Check all containers for the number 15.
    • Container 3 contains 15.
    • Output: 3
  4. Operation: change(7, 15)
    • Set the container at index 7 to 15.
    • Now, containers 3 and 7 both have 15.
  5. Operation: change(3, 20)
    • Update the container at index 3 to 20.
    • Container 3 no longer holds 15; only container 7 holds 15.
  6. Operation: find(15)
    • Check all containers for the number 15.
    • Only container 7 contains 15.
    • Output: 7
  7. Operation: change(7, 30)
    • Update the container at index 7 to 30.
    • Container 7 no longer holds 15.
  8. Operation: find(15)
    • Check all containers for the number 15.
    • No container holds 15.
    • Output: -1
  9. Operation: find(20)
    • Check all containers for the number 20.
    • Container 3 holds 20.
    • Output: 3

Constraints

  • 1 <= index, number <= 10^9
  • There will be at most 10^5 calls combined to the change and find operations.

Category:No additional categories provided
Programming Language:
  • Java
Reference Link:

https://leetcode.com/problems/design-a-number-container-system/description

Online IDE

Scroll down for output
Java
Output:

Loading component...

Loading component...

Tracking code (View only. In case you want to track the code, click this button):
Main Function:

Main Function is not defined.

Helper Function:

INPUT : ["NumberContainers", "change", "change", "find", "change", "find"] [[], [2, 10], [5, 10], [10], [2, 20], [10]]

Output: [null, null, null, 2, null, 5]

public static void change(int index, int number) {

if (indexMap.containsKey(index)) {

int oldNumber = indexMap.get(index);

if (numToIndices.containsKey(oldNumber)) {

numToIndices.get(oldNumber).remove(index);

}//If End

}//If End

indexMap.put(index, number);

numToIndices.putIfAbsent(number, new TreeSet<>());

numToIndices.get(number).add(index);

}//function end

Utility Functions and Global variables:

public static int find(int number) {

if (!numToIndices.containsKey(number) || numToIndices.get(number).isEmpty()) {

return -1;

}

return numToIndices.get(number).first();

}//function end