Skip to content

Practice Design a Number Container System

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2349
LeetCode Problem

Design a Number Container System

Problem Statement

NumberContainers starts empty. change(index,number) stores a number at an index, replacing any previous one. find(number) returns its smallest current index or -1 if absent. Practice approach: Track index-to-number assignments and a min-heap of candidate indices for each number. On find, discard heap entries whose current assignment differs. Alternatively use ordered index sets with eager removals. Replacement must stop the old number from claiming that index. Test changing the smallest matching index and assigning the same value again.

Source: Design a Number Container System on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["NumberContainers","change","change","find","change","find","find"]
[[],[4,7],[2,7],[7],[2,9],[7],[9]]
Output
[null,null,null,2,null,4,2]
Explanation

Replacing index 2 moves the smallest index for number 7 to 4.

Constraints

1 <= index, number <= 10^9
At most 10^5 total calls.

Related LeetCode Problems

Practice more problems similar to Design a Number Container System.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.