Leetcode Problem 2349. Design a Number Container System

2349. Design a Number Container System

Leetcode Solutions

Using HashMap and TreeSet

  1. Initialize a HashMap indexToNumber to map indices to numbers.
  2. Initialize a TreeMap numberToIndices to map numbers to a TreeSet of indices.
  3. For the change method: a. If the index already has a number, remove the index from the TreeSet of the old number. b. Update the indexToNumber map with the new number for the given index. c. Add the index to the TreeSet of the new number in numberToIndices.
  4. For the find method: a. Check if the number exists in numberToIndices. b. If it does, return the first (smallest) index in the TreeSet. c. If not, return -1.
UML Thumbnail

Using HashMap and PriorityQueue

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...