Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.2349 (#3312)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme authored Jul 23, 2024
1 parent bf00301 commit d5d3ad8
Show file tree
Hide file tree
Showing 10 changed files with 1,743 additions and 139 deletions.
624 changes: 579 additions & 45 deletions solution/2300-2399/2349.Design a Number Container System/README.md

Large diffs are not rendered by default.

624 changes: 579 additions & 45 deletions solution/2300-2399/2349.Design a Number Container System/README_EN.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
class NumberContainers {
public:
map<int, int> mp;
map<int, set<int>> t;

NumberContainers() {
}

void change(int index, int number) {
auto it = mp.find(index);
if (it != mp.end()) {
t[it->second].erase(index);
it->second = number;
} else
mp[index] = number;
t[number].insert(index);
if (d.contains(index)) {
int oldNumber = d[index];
g[oldNumber].erase(index);
if (g[oldNumber].empty()) {
g.erase(oldNumber);
}
}
d[index] = number;
g[number].insert(index);
}

int find(int number) {
auto it = t.find(number);
return it == t.end() || it->second.empty() ? -1 : *it->second.begin();
return g.contains(number) ? *g[number].begin() : -1;
}

private:
unordered_map<int, int> d;
unordered_map<int, set<int>> g;
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
type NumberContainers struct {
mp map[int]int
t map[int]*redblacktree.Tree
d map[int]int
g map[int]*redblacktree.Tree
}

func Constructor() NumberContainers {
return NumberContainers{map[int]int{}, map[int]*redblacktree.Tree{}}
}

func (this *NumberContainers) Change(index int, number int) {
if num, ok := this.mp[index]; ok {
this.t[num].Remove(index)
if oldNumber, ok := this.d[index]; ok {
this.g[oldNumber].Remove(index)
}
this.mp[index] = number
if this.t[number] == nil {
this.t[number] = redblacktree.NewWithIntComparator()
this.d[index] = number
if _, ok := this.g[number]; !ok {
this.g[number] = redblacktree.NewWithIntComparator()
}
this.t[number].Put(index, nil)
this.g[number].Put(index, nil)
}

func (this *NumberContainers) Find(number int) int {
s, ok := this.t[number]
if !ok || s.Size() == 0 {
return -1
if ids, ok := this.g[number]; ok && ids.Size() > 0 {
return ids.Left().Key.(int)
}
return s.Left().Key.(int)
return -1
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
class NumberContainers {
private Map<Integer, Integer> mp = new HashMap<>();
private Map<Integer, TreeSet<Integer>> t = new HashMap<>();
private Map<Integer, Integer> d = new HashMap<>();
private Map<Integer, TreeSet<Integer>> g = new HashMap<>();

public NumberContainers() {
}

public void change(int index, int number) {
if (mp.containsKey(index)) {
int v = mp.get(index);
t.get(v).remove(index);
if (t.get(v).isEmpty()) {
t.remove(v);
}
if (d.containsKey(index)) {
int oldNumber = d.get(index);
g.get(oldNumber).remove(index);
}
mp.put(index, number);
t.computeIfAbsent(number, k -> new TreeSet<>()).add(index);
d.put(index, number);
g.computeIfAbsent(number, k -> new TreeSet<>()).add(index);
}

public int find(int number) {
return t.containsKey(number) ? t.get(number).first() : -1;
var ids = g.get(number);
return ids == null || ids.isEmpty() ? -1 : ids.first();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@


class NumberContainers:

def __init__(self):
self.mp = {}
self.t = defaultdict(SortedSet)
self.d = {}
self.g = defaultdict(SortedSet)

def change(self, index: int, number: int) -> None:
if index in self.mp:
v = self.mp[index]
self.t[v].remove(index)
self.mp[index] = number
self.t[number].add(index)
if index in self.d:
old_number = self.d[index]
self.g[old_number].remove(index)
self.d[index] = number
self.g[number].add(index)

def find(self, number: int) -> int:
s = self.t[number]
return s[0] if s else -1
ids = self.g[number]
return ids[0] if ids else -1


# Your NumberContainers object will be instantiated and called as such:
Expand Down
Loading

0 comments on commit d5d3ad8

Please sign in to comment.