Last active
February 8, 2025 10:45
-
-
Save Tan12d/66b4efe93a8eb54e4305e6aae004e2c1 to your computer and use it in GitHub Desktop.
Leetcode 2349 | Design a Number Container System
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Method-1 | |
class NumberContainers { | |
HashMap<Integer, Integer> idx_num; | |
HashMap<Integer, PriorityQueue<Integer>> num_indices; | |
public NumberContainers() | |
{ | |
idx_num = new HashMap<Integer, Integer>(); | |
num_indices = new HashMap<>(); | |
} | |
public void change(int index, int number) | |
{ | |
if(idx_num.containsKey(index)) | |
{ | |
int old_value = idx_num.get(index); | |
if(old_value == number) return; | |
num_indices.get(old_value).remove(index); | |
} | |
idx_num.put(index, number); | |
num_indices.computeIfAbsent(number, k -> new PriorityQueue<>()).add(index); | |
} | |
public int find(int number) | |
{ | |
if(!num_indices.containsKey(number)) return -1; | |
return num_indices.get(number).peek() == null ? -1 : num_indices.get(number).peek(); | |
} | |
} | |
// Method-2 | |
class NumberContainers { | |
HashMap<Integer, Integer> idx_num; | |
HashMap<Integer, TreeSet<Integer>> num_indices; | |
public NumberContainers() | |
{ | |
idx_num = new HashMap<Integer, Integer>(); | |
num_indices = new HashMap<>(); | |
} | |
public void change(int index, int number) | |
{ | |
if(idx_num.containsKey(index)) | |
{ | |
int old_value = idx_num.get(index); | |
num_indices.get(old_value).remove(index); | |
if(num_indices.get(old_value).isEmpty()) | |
{ | |
num_indices.remove(old_value); | |
} | |
} | |
idx_num.put(index, number); | |
num_indices.computeIfAbsent(number, k -> new TreeSet<>()).add(index); | |
} | |
public int find(int number) | |
{ | |
if(!num_indices.containsKey(number)) return -1; | |
return num_indices.get(number).first(); | |
} | |
} | |
// Method-3 | |
class NumberContainers { | |
HashMap<Integer, Integer> idx_num; | |
HashMap<Integer, PriorityQueue<Integer>> num_indices; | |
public NumberContainers() | |
{ | |
idx_num = new HashMap<Integer, Integer>(); | |
num_indices = new HashMap<>(); | |
} | |
public void change(int index, int number) | |
{ | |
if(idx_num.containsKey(index)) | |
{ | |
int old_value = idx_num.get(index); | |
if(old_value == number) return; | |
num_indices.get(old_value).remove(index); | |
} | |
idx_num.put(index, number); | |
num_indices.computeIfAbsent(number, k -> new PriorityQueue<>()).add(index); | |
} | |
public int find(int number) | |
{ | |
PriorityQueue<Integer> res = num_indices.getOrDefault(number, new PriorityQueue<>()); | |
return res.isEmpty() ? -1 : res.peek(); | |
} | |
} | |
// Method-4 | |
class NumberContainers { | |
HashMap<Integer, Integer> idx_num; | |
HashMap<Integer, PriorityQueue<Integer>> num_indices; | |
public NumberContainers() | |
{ | |
idx_num = new HashMap<Integer, Integer>(); | |
num_indices = new HashMap<>(); | |
} | |
public void change(int index, int number) | |
{ | |
if(idx_num.containsKey(index)) | |
{ | |
int old_value = idx_num.get(index); | |
if(old_value == number) return; | |
num_indices.get(old_value).remove(index); | |
} | |
idx_num.put(index, number); | |
num_indices.computeIfAbsent(number, k -> new PriorityQueue<>()).add(index); | |
} | |
public int find(int number) | |
{ | |
if(!num_indices.containsKey(number)) return -1; | |
return num_indices.get(number).peek() == null ? -1 : num_indices.get(number).peek(); | |
} | |
} | |
// Method-5 | |
class NumberContainers { | |
HashMap<Integer, Integer> idx_num; | |
HashMap<Integer, TreeSet<Integer>> num_indices; | |
public NumberContainers() | |
{ | |
idx_num = new HashMap<Integer, Integer>(); | |
num_indices = new HashMap<>(); | |
} | |
public void change(int index, int number) | |
{ | |
if(idx_num.containsKey(index)) | |
{ | |
int old_value = idx_num.get(index); | |
if(old_value == number) return; | |
num_indices.get(old_value).remove(index); | |
if(num_indices.get(old_value).isEmpty()) | |
{ | |
num_indices.remove(old_value); | |
} | |
} | |
idx_num.put(index, number); | |
num_indices.computeIfAbsent(number, k -> new TreeSet<>()).add(index); | |
} | |
public int find(int number) | |
{ | |
if(!num_indices.containsKey(number)) return -1; | |
return num_indices.get(number).first(); | |
} | |
} | |
// Method-6 (Time Limit exceeded) | |
class NumberContainers { | |
HashMap<Integer, Integer> idx_num; | |
HashMap<Integer, PriorityQueue<Integer>> num_indices; | |
public NumberContainers() | |
{ | |
idx_num = new HashMap<Integer, Integer>(); | |
num_indices = new HashMap<>(); | |
} | |
public void change(int index, int number) | |
{ | |
if(idx_num.containsKey(index)) | |
{ | |
int old_value = idx_num.get(index); | |
num_indices.get(old_value).remove(index); | |
if(num_indices.get(old_value).isEmpty()) | |
{ | |
num_indices.remove(old_value); | |
} | |
} | |
idx_num.put(index, number); | |
num_indices.computeIfAbsent(number, k -> new PriorityQueue<>()).add(index); | |
} | |
public int find(int number) | |
{ | |
if(!num_indices.containsKey(number)) return -1; | |
return num_indices.get(number).peek(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Important Links & Files
Leetcode Question Link
Leetcode Solution Link
LinkedIn Post