Skip to content

Commit 5dc82ad

Browse files
committed
feat: update solutions to lc problem: No.0399.Evaluate Division
1 parent 6d21515 commit 5dc82ad

File tree

6 files changed

+180
-322
lines changed

6 files changed

+180
-322
lines changed

solution/0300-0399/0399.Evaluate Division/README.md

Lines changed: 60 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -131,42 +131,27 @@ d[find(a)] = distance
131131
```python
132132
class Solution:
133133
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
134-
n = len(equations)
135-
p = list(range(n << 1))
136-
w = [1.0] * (n << 1)
137-
134+
w = collections.defaultdict(lambda: 1)
135+
p = collections.defaultdict()
136+
for a, b in equations:
137+
p[a] = a
138+
p[b] = b
139+
138140
def find(x):
139141
if p[x] != x:
140142
origin = p[x]
141143
p[x] = find(p[x])
142144
w[x] *= w[origin]
143145
return p[x]
144146

145-
mp = {}
146-
idx = 0
147147
for i, e in enumerate(equations):
148-
a, b = e[0], e[1]
149-
if a not in mp:
150-
mp[a] = idx
151-
idx += 1
152-
if b not in mp:
153-
mp[b] = idx
154-
idx += 1
155-
pa, pb = find(mp[a]), find(mp[b])
148+
pa, pb = find(e[0]), find(e[1])
156149
if pa == pb:
157150
continue
158151
p[pa] = pb
159-
w[pa] = w[mp[b]] * values[i] / w[mp[a]]
160-
161-
res = []
162-
for q in queries:
163-
c, d = q[0], q[1]
164-
if c not in mp or d not in mp:
165-
res.append(-1.0)
166-
else:
167-
pa, pb = find(mp[c]), find(mp[d])
168-
res.append(w[mp[c]] / w[mp[d]] if pa == pb else -1.0)
169-
return res
152+
w[pa] = w[e[1]] * values[i] / w[e[0]]
153+
154+
return [-1 if c not in p or d not in p or find(c) != find(d) else w[c] / w[d] for c, d in queries]
170155
```
171156

172157
### **Java**
@@ -175,57 +160,45 @@ class Solution:
175160

176161
```java
177162
class Solution {
178-
private int[] p;
179-
private double[] w;
163+
private Map<String, String> p;
164+
private Map<String, Double> w;
180165

181166
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
182167
int n = equations.size();
183-
p = new int[n << 1];
184-
w = new double[n << 1];
185-
for (int i = 0; i < p.length; ++i) {
186-
p[i] = i;
187-
w[i] = 1.0;
168+
p = new HashMap<>(n << 1);
169+
w = new HashMap<>(n << 1);
170+
for (List<String> e : equations) {
171+
p.put(e.get(0), e.get(0));
172+
p.put(e.get(1), e.get(1));
173+
w.put(e.get(0), 1.0);
174+
w.put(e.get(1), 1.0);
188175
}
189-
Map<String, Integer> mp = new HashMap<>(n << 1);
190-
int idx = 0;
191176
for (int i = 0; i < n; ++i) {
192177
List<String> e = equations.get(i);
193178
String a = e.get(0), b = e.get(1);
194-
if (!mp.containsKey(a)) {
195-
mp.put(a, idx++);
196-
}
197-
if (!mp.containsKey(b)) {
198-
mp.put(b, idx++);
199-
}
200-
int pa = find(mp.get(a)), pb = find(mp.get(b));
201-
if (pa == pb) {
179+
String pa = find(a), pb = find(b);
180+
if (Objects.equals(pa, pb)) {
202181
continue;
203182
}
204-
p[pa] = pb;
205-
w[pa] = w[mp.get(b)] * values[i] / w[mp.get(a)];
183+
p.put(pa, pb);
184+
w.put(pa, w.get(b) * values[i] / w.get(a));
206185
}
207186
int m = queries.size();
208187
double[] res = new double[m];
209188
for (int i = 0; i < m; ++i) {
210189
String c = queries.get(i).get(0), d = queries.get(i).get(1);
211-
Integer id1 = mp.get(c), id2 = mp.get(d);
212-
if (id1 == null || id2 == null) {
213-
res[i] = -1.0;
214-
} else {
215-
int pa = find(id1), pb = find(id2);
216-
res[i] = pa == pb ? w[id1] / w[id2] : -1.0;
217-
}
190+
res[i] = !p.containsKey(c) || !p.containsKey(d) || !Objects.equals(find(c), find(d)) ? - 1.0 : w.get(c) / w.get(d);
218191
}
219192
return res;
220193
}
221194

222-
private int find(int x) {
223-
if (p[x] != x) {
224-
int origin = p[x];
225-
p[x] = find(p[x]);
226-
w[x] *= w[origin];
195+
private String find(String x) {
196+
if (!Objects.equals(p.get(x), x)) {
197+
String origin = p.get(x);
198+
p.put(x, find(p.get(x)));
199+
w.put(x, w.get(x) * w.get(origin));
227200
}
228-
return p[x];
201+
return p.get(x);
229202
}
230203
}
231204
```
@@ -235,48 +208,41 @@ class Solution {
235208
```cpp
236209
class Solution {
237210
public:
238-
vector<int> p;
239-
vector<double> w;
211+
unordered_map<string, string> p;
212+
unordered_map<string, double> w;
240213

241214
vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
242215
int n = equations.size();
243-
for (int i = 0; i < (n << 1); ++i)
216+
for (auto e : equations)
244217
{
245-
p.push_back(i);
246-
w.push_back(1.0);
218+
p[e[0]] = e[0];
219+
p[e[1]] = e[1];
220+
w[e[0]] = 1.0;
221+
w[e[1]] = 1.0;
247222
}
248-
unordered_map<string, int> mp;
249-
int idx = 0;
250223
for (int i = 0; i < n; ++i)
251224
{
252-
auto e = equations[i];
225+
vector<string> e = equations[i];
253226
string a = e[0], b = e[1];
254-
if (mp.find(a) == mp.end()) mp[a] = idx++;
255-
if (mp.find(b) == mp.end()) mp[b] = idx++;
256-
int pa = find(mp[a]), pb = find(mp[b]);
227+
string pa = find(a), pb = find(b);
257228
if (pa == pb) continue;
258229
p[pa] = pb;
259-
w[pa] = w[mp[b]] * values[i] / w[mp[a]];
230+
w[pa] = w[b] * values[i] / w[a];
260231
}
261232
int m = queries.size();
262-
vector<double> res;
233+
vector<double> res(m);
263234
for (int i = 0; i < m; ++i)
264235
{
265236
string c = queries[i][0], d = queries[i][1];
266-
if (mp.find(c) == mp.end() || mp.find(d) == mp.end()) res.push_back(-1.0);
267-
else
268-
{
269-
int pa = find(mp[c]), pb = find(mp[d]);
270-
res.push_back(pa == pb ? w[mp[c]] / w[mp[d]] : -1.0);
271-
}
237+
res[i] = p.find(c) == p.end() || p.find(d) == p.end() || find(c) != find(d) ? -1.0 : w[c] / w[d];
272238
}
273239
return res;
274240
}
275241

276-
int find(int x) {
242+
string find(string x) {
277243
if (p[x] != x)
278244
{
279-
int origin = p[x];
245+
string origin = p[x];
280246
p[x] = find(p[x]);
281247
w[x] *= w[origin];
282248
}
@@ -288,54 +254,40 @@ public:
288254
### **Go**
289255

290256
```go
291-
var p []int
292-
var w []float64
257+
var p map[string]string
258+
var w map[string]float64
293259

294260
func calcEquation(equations [][]string, values []float64, queries [][]string) []float64 {
295-
n := len(equations)
296-
p = make([]int, (n<<1)+10)
297-
w = make([]float64, (n<<1)+10)
298-
for i := 0; i < (n<<1)+10; i++ {
299-
p[i] = i
300-
w[i] = 1.0
261+
p = make(map[string]string)
262+
w = make(map[string]float64)
263+
for _, e := range equations {
264+
p[e[0]] = e[0]
265+
p[e[1]] = e[1]
266+
w[e[0]] = 1.0
267+
w[e[1]] = 1.0
301268
}
302-
mp := make(map[string]int)
303-
idx := 1
304269
for i, e := range equations {
305270
a, b := e[0], e[1]
306-
if mp[a] == 0 {
307-
mp[a] = idx
308-
idx++
309-
}
310-
if mp[b] == 0 {
311-
mp[b] = idx
312-
idx++
313-
}
314-
pa, pb := find(mp[a]), find(mp[b])
271+
pa, pb := find(a), find(b)
315272
if pa == pb {
316273
continue
317274
}
318275
p[pa] = pb
319-
w[pa] = w[mp[b]] * values[i] / w[mp[a]]
276+
w[pa] = w[b] * values[i] / w[a]
320277
}
321278
var res []float64
322-
for _, q := range queries {
323-
c, d := q[0], q[1]
324-
if mp[c] == 0 || mp[d] == 0 {
279+
for _, e := range queries {
280+
c, d := e[0], e[1]
281+
if p[c] == "" || p[d] == "" || find(c) != find(d) {
325282
res = append(res, -1.0)
326283
} else {
327-
pa, pb := find(mp[c]), find(mp[d])
328-
if pa == pb {
329-
res = append(res, w[mp[c]]/w[mp[d]])
330-
} else {
331-
res = append(res, -1.0)
332-
}
284+
res = append(res, w[c]/w[d])
333285
}
334286
}
335287
return res
336288
}
337289

338-
func find(x int) int {
290+
func find(x string) string {
339291
if p[x] != x {
340292
origin := p[x]
341293
p[x] = find(p[x])

0 commit comments

Comments
 (0)