Skip to content

Commit f0fa969

Browse files
committed
Modified 2 solutions
1 parent b0e07ae commit f0fa969

File tree

2 files changed

+58
-80
lines changed

2 files changed

+58
-80
lines changed
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
class Solution {
2-
public int numEquivDominoPairs(int[][] dominoes) {
3-
Map<String, Integer> map = new HashMap <>();
4-
for (int i = 0; i < dominoes.length; i++) {
5-
String key = Math.min(dominoes[i][0], dominoes[i][1]) + "|" + Math.max(dominoes[i][0], dominoes[i][1]);
6-
map.put(key, map.getOrDefault(key, 0) + 1);
2+
public int numEquivDominoPairs(int[][] dominoes) {
3+
int count = 0;
4+
Map<String, Integer> map = new HashMap<>();
5+
for (int i = 0; i < dominoes.length; i++) {
6+
String straightKey = dominoes[i][0] + "/" + dominoes[i][1];
7+
String revKey = dominoes[i][1] + "/" + dominoes[i][0];
8+
if (map.containsKey(straightKey)) {
9+
count += map.get(straightKey);
10+
}
11+
else if (!straightKey.equals(revKey)) {
12+
if (map.containsKey(revKey)) {
13+
count += map.get(revKey);
714
}
8-
9-
int count = 0;
10-
for (int value : map.values()) {
11-
count += (value * (value - 1)) / 2;
12-
}
13-
14-
return count;
15+
}
16+
map.put(straightKey, map.getOrDefault(straightKey, 0) + 1);
17+
if (dominoes[i][0] != dominoes[i][1]) {
18+
map.put(revKey, map.getOrDefault(revKey, 0) + 1);
19+
}
1520
}
21+
return count;
22+
}
1623
}

Medium/Evaluate Division.java

Lines changed: 39 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,44 @@
11
class Solution {
2-
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
3-
double[] ans = new double[queries.size()];
4-
5-
Map<String, Set<String>> map = new HashMap<>();
6-
Map<String, Double> resMap = new HashMap<>();
7-
8-
for (int i = 0; i < equations.size(); i++) {
9-
String operand1 = equations.get(i).get(0);
10-
String operand2 = equations.get(i).get(1);
11-
12-
map.computeIfAbsent(operand1, k -> new HashSet<>()).add(operand2);
13-
map.computeIfAbsent(operand2, k -> new HashSet<>()).add(operand1);
14-
15-
resMap.put(operand1 + "|" + operand2, values[i]);
16-
}
17-
18-
for (int i = 0; i < queries.size(); i++) {
19-
String operand1 = queries.get(i).get(0);
20-
String operand2 = queries.get(i).get(1);
21-
22-
if (!map.containsKey(operand1) || !map.containsKey(operand2)) {
23-
ans[i] = -1.0;
24-
}
25-
else if (operand1.equals(operand2)) {
26-
ans[i] = 1.0;
27-
}
28-
else if (map.get(operand1).contains(operand2)) {
29-
ans[i] = getSimpleDivisionVal(resMap, operand1, operand2);
30-
}
31-
else if (operand2.contains(operand1)) {
32-
ans[i] = getSimpleDivisionVal(resMap, operand2, operand1);
33-
}
34-
else {
35-
ans[i] = dfs(map, resMap, operand1, operand2, 1.0, new HashSet<>());
36-
}
37-
}
38-
39-
return ans;
2+
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
3+
Map<String, List<String>> map = new HashMap<>();
4+
Map<String, List<Double>> valueMap = new HashMap<>();
5+
for (int i = 0; i < equations.size(); i++) {
6+
List<String> equation = equations.get(i);
7+
map.computeIfAbsent(equation.get(0), k -> new ArrayList<>()).add(equation.get(1));
8+
map.computeIfAbsent(equation.get(1), k -> new ArrayList<>()).add(equation.get(0));
9+
valueMap.computeIfAbsent(equation.get(0), k -> new ArrayList<>()).add(values[i]);
10+
valueMap.computeIfAbsent(equation.get(1), k -> new ArrayList<>()).add(1 / values[i]);
4011
}
41-
42-
private double dfs (Map<String, Set<String>> map, Map<String, Double> resMap, String operand1, String operand2,
43-
double base, Set<String> visited) {
44-
if (visited.contains(operand1)) {
45-
return -1.0;
46-
}
47-
if (map.get(operand1).contains(operand2)) {
48-
return base * getSimpleDivisionVal(resMap, operand1, operand2);
49-
}
50-
51-
visited.add(operand1);
52-
53-
double ans = -1.0;
54-
Iterator<String> children = map.get(operand1).iterator();
55-
56-
while (children.hasNext()) {
57-
String child = children.next();
58-
if (!visited.contains(child)) {
59-
ans = Math.max(ans, dfs(map, resMap, child, operand2, base * getSimpleDivisionVal(resMap, operand1, child), visited));
60-
}
61-
}
62-
63-
return ans;
12+
double[] ans = new double[queries.size()];
13+
for (int i = 0; i < queries.size(); i++) {
14+
List<String> query = queries.get(i);
15+
ans[i] = dfs(map, valueMap, query.get(0), query.get(1), new HashSet<>(), 1.0);
16+
ans[i] = ans[i] == 0.0 ? -1.0 : ans[i];
6417
}
65-
66-
private double getSimpleDivisionVal (Map<String, Double> resMap, String operand1, String operand2) {
67-
if (resMap.containsKey(operand1 + "|" + operand2)) {
68-
return resMap.get(operand1 + "|" + operand2);
69-
}
70-
71-
return 1 / resMap.get(operand2 + "|" + operand1);
18+
return ans;
19+
}
20+
21+
private double dfs(Map<String, List<String>> map, Map<String, List<Double>> valueMap, String a, String b, Set<String> set, double curr) {
22+
if (set.contains(a)) {
23+
return 0.0;
7224
}
25+
if (!map.containsKey(a)) {
26+
return 0.0;
27+
}
28+
if (a.equals(b)) {
29+
return curr;
30+
}
31+
set.add(a);
32+
List<String> children = map.get(a);
33+
List<Double> valueList = valueMap.get(a);
34+
double temp = 0.0;
35+
for (int i = 0; i < children.size(); i++) {
36+
temp = dfs(map, valueMap, children.get(i), b, set, curr * valueList.get(i));
37+
if (temp != 0.0) {
38+
break;
39+
}
40+
}
41+
set.remove(a);
42+
return temp;
43+
}
7344
}

0 commit comments

Comments
 (0)