Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d5d3ad8

Browse files
authoredJul 23, 2024··
feat: add solutions to lc problem: No.2349 (#3312)
1 parent bf00301 commit d5d3ad8

File tree

10 files changed

+1743
-139
lines changed

10 files changed

+1743
-139
lines changed
 

‎solution/2300-2399/2349.Design a Number Container System/README.md

Lines changed: 579 additions & 45 deletions
Large diffs are not rendered by default.

‎solution/2300-2399/2349.Design a Number Container System/README_EN.md

Lines changed: 579 additions & 45 deletions
Large diffs are not rendered by default.

‎solution/2300-2399/2349.Design a Number Container System/Solution.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
class NumberContainers {
22
public:
3-
map<int, int> mp;
4-
map<int, set<int>> t;
5-
63
NumberContainers() {
74
}
85

96
void change(int index, int number) {
10-
auto it = mp.find(index);
11-
if (it != mp.end()) {
12-
t[it->second].erase(index);
13-
it->second = number;
14-
} else
15-
mp[index] = number;
16-
t[number].insert(index);
7+
if (d.contains(index)) {
8+
int oldNumber = d[index];
9+
g[oldNumber].erase(index);
10+
if (g[oldNumber].empty()) {
11+
g.erase(oldNumber);
12+
}
13+
}
14+
d[index] = number;
15+
g[number].insert(index);
1716
}
1817

1918
int find(int number) {
20-
auto it = t.find(number);
21-
return it == t.end() || it->second.empty() ? -1 : *it->second.begin();
19+
return g.contains(number) ? *g[number].begin() : -1;
2220
}
21+
22+
private:
23+
unordered_map<int, int> d;
24+
unordered_map<int, set<int>> g;
2325
};
2426

2527
/**

‎solution/2300-2399/2349.Design a Number Container System/Solution.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
type NumberContainers struct {
2-
mp map[int]int
3-
t map[int]*redblacktree.Tree
2+
d map[int]int
3+
g map[int]*redblacktree.Tree
44
}
55

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

1010
func (this *NumberContainers) Change(index int, number int) {
11-
if num, ok := this.mp[index]; ok {
12-
this.t[num].Remove(index)
11+
if oldNumber, ok := this.d[index]; ok {
12+
this.g[oldNumber].Remove(index)
1313
}
14-
this.mp[index] = number
15-
if this.t[number] == nil {
16-
this.t[number] = redblacktree.NewWithIntComparator()
14+
this.d[index] = number
15+
if _, ok := this.g[number]; !ok {
16+
this.g[number] = redblacktree.NewWithIntComparator()
1717
}
18-
this.t[number].Put(index, nil)
18+
this.g[number].Put(index, nil)
1919
}
2020

2121
func (this *NumberContainers) Find(number int) int {
22-
s, ok := this.t[number]
23-
if !ok || s.Size() == 0 {
24-
return -1
22+
if ids, ok := this.g[number]; ok && ids.Size() > 0 {
23+
return ids.Left().Key.(int)
2524
}
26-
return s.Left().Key.(int)
25+
return -1
2726
}
2827

2928
/**

‎solution/2300-2399/2349.Design a Number Container System/Solution.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
class NumberContainers {
2-
private Map<Integer, Integer> mp = new HashMap<>();
3-
private Map<Integer, TreeSet<Integer>> t = new HashMap<>();
2+
private Map<Integer, Integer> d = new HashMap<>();
3+
private Map<Integer, TreeSet<Integer>> g = new HashMap<>();
44

55
public NumberContainers() {
66
}
77

88
public void change(int index, int number) {
9-
if (mp.containsKey(index)) {
10-
int v = mp.get(index);
11-
t.get(v).remove(index);
12-
if (t.get(v).isEmpty()) {
13-
t.remove(v);
14-
}
9+
if (d.containsKey(index)) {
10+
int oldNumber = d.get(index);
11+
g.get(oldNumber).remove(index);
1512
}
16-
mp.put(index, number);
17-
t.computeIfAbsent(number, k -> new TreeSet<>()).add(index);
13+
d.put(index, number);
14+
g.computeIfAbsent(number, k -> new TreeSet<>()).add(index);
1815
}
1916

2017
public int find(int number) {
21-
return t.containsKey(number) ? t.get(number).first() : -1;
18+
var ids = g.get(number);
19+
return ids == null || ids.isEmpty() ? -1 : ids.first();
2220
}
2321
}
2422

‎solution/2300-2399/2349.Design a Number Container System/Solution.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22

33

44
class NumberContainers:
5+
56
def __init__(self):
6-
self.mp = {}
7-
self.t = defaultdict(SortedSet)
7+
self.d = {}
8+
self.g = defaultdict(SortedSet)
89

910
def change(self, index: int, number: int) -> None:
10-
if index in self.mp:
11-
v = self.mp[index]
12-
self.t[v].remove(index)
13-
self.mp[index] = number
14-
self.t[number].add(index)
11+
if index in self.d:
12+
old_number = self.d[index]
13+
self.g[old_number].remove(index)
14+
self.d[index] = number
15+
self.g[number].add(index)
1516

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

2021

2122
# Your NumberContainers object will be instantiated and called as such:

‎solution/2300-2399/2349.Design a Number Container System/Solution.ts

Lines changed: 521 additions & 0 deletions
Large diffs are not rendered by default.

‎solution/2800-2899/2849.Determine if a Cell Is Reachable at a Given Time/README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ tags:
5959

6060
<!-- solution:start -->
6161

62-
### 方法一
62+
### 方法一:分情况讨论
63+
64+
如果起点和终点相同,那么只有当 $t \neq 1$ 时,才能在给定时间到达终点。
65+
66+
否则,我们可以计算出起点和终点的横纵坐标之差,然后取最大值,如果最大值小于等于给定时间,那么就可以在给定时间到达终点。
67+
68+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
6369

6470
<!-- tabs:start -->
6571

@@ -143,8 +149,9 @@ function isReachableAtTime(sx: number, sy: number, fx: number, fy: number, t: nu
143149
```cs
144150
public class Solution {
145151
public bool IsReachableAtTime(int sx, int sy, int fx, int fy, int t) {
146-
if (sx == fx && sy == fy)
152+
if (sx == fx && sy == fy) {
147153
return t != 1;
154+
}
148155
return Math.Max(Math.Abs(sx - fx), Math.Abs(sy - fy)) <= t;
149156
}
150157
}

‎solution/2800-2899/2849.Determine if a Cell Is Reachable at a Given Time/README_EN.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### Solution 1
60+
### Solution 1: Case Discussion
61+
62+
If the starting point and the destination are the same, then we can only reach the destination within the given time if $t \neq 1$.
63+
64+
Otherwise, we can calculate the difference in the x and y coordinates between the starting point and the destination, and then take the maximum value. If the maximum value is less than or equal to the given time, then we can reach the destination within the given time.
65+
66+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
6167

6268
<!-- tabs:start -->
6369

@@ -141,8 +147,9 @@ function isReachableAtTime(sx: number, sy: number, fx: number, fy: number, t: nu
141147
```cs
142148
public class Solution {
143149
public bool IsReachableAtTime(int sx, int sy, int fx, int fy, int t) {
144-
if (sx == fx && sy == fy)
150+
if (sx == fx && sy == fy) {
145151
return t != 1;
152+
}
146153
return Math.Max(Math.Abs(sx - fx), Math.Abs(sy - fy)) <= t;
147154
}
148155
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
public class Solution {
22
public bool IsReachableAtTime(int sx, int sy, int fx, int fy, int t) {
3-
if (sx == fx && sy == fy)
3+
if (sx == fx && sy == fy) {
44
return t != 1;
5+
}
56
return Math.Max(Math.Abs(sx - fx), Math.Abs(sy - fy)) <= t;
67
}
78
}

0 commit comments

Comments
 (0)
Please sign in to comment.