【LeetCode】399。評価部門(中)(JAVA)
件名アドレス:https://leetcode.com/problems/evaluate-division/
タイトル説明:
変数ペア方程式の配列と実数値の配列が与えられます。ここで、equations [i] = [A_i、B_i]とvalues [i]は方程式A_i / B_i = values [i]を表します。各A_iまたはB_iは、単一の変数を表す文字列です。
また、いくつかのクエリが与えられます。ここで、querys [j] = [C_j、D_j]は、C_j / D_j =?の答えを見つける必要があるj番目のクエリを表します。
すべてのクエリに対する回答を返します。単一の回答を判別できない場合は、-1.0を返します。
注:入力は常に有効です。クエリを評価してもゼロ除算にはならず、矛盾はないと思われるかもしれません。
例1:
Input: equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
Output: [6.00000,0.50000,-1.00000,1.00000,-1.00000]
Explanation:
Given: a / b = 2.0, b / c = 3.0
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
例2:
Input: equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
Output: [3.75000,0.40000,5.00000,0.20000]
例3:
Input: equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
Output: [0.50000,2.00000,-1.00000,-1.00000]
制約:
- 1 <=方程式。長さ<= 20
- 方程式[i] .length == 2
- 1 <= A_i.length、B_i.length <= 5
- values.length ==方程式.length
- 0.0 <値[i] <= 20.0
- 1 <= querys.length <= 20
- クエリ[i] .length == 2
- 1 <= C_j.length、D_j.length <= 5
- A_i、B_i、C_j、D_jは、小文字の英字と数字で構成されます。
一般的なアイデア
既知の条件として、方程式の変数ペア配列と値の実数値配列を提供します。ここで、equations [i] = [A_i、B_i]とvalues [i]は一緒に方程式A_i / B_i = values [iを表します]。各A_iまたはB_iは、単一の変数を表す文字列です。
クエリの配列で表される質問もいくつかあります。ここで、querys [j] = [C_j、D_j]はj番目の質問を表します。既知の条件に基づく回答として、C_j / D_j =?の結果を見つけてください。
すべての質問に対する回答を返します。不明な回答がある場合は、この回答を-1.0に置き換えてください。
注:入力は常に有効です。除算演算には除数0がなく、矛盾する結果が発生しないと想定できます。
問題解決方法
暴力的な解決策
- 有向グラフを作成し、それをmapKeyのkv形式で保存し、mapValueを使用して、表示されるk / vの計算結果値を保存します。
- 新しい値を検索しますx / y、x、yはmapKeyに含まれている必要があります。結果が得られた場合は、mapValueに再格納します。
class Solution {
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
Map<String, List<String>> mapKey = new HashMap<>();
Map<String, Double> mapValue = new HashMap<>();
for (int i = 0; i < values.length; i++) {
String first = equations.get(i).get(0);
String second = equations.get(i).get(0);
putMap(mapKey, first, second);
putMap(mapKey, second, first);
mapValue.put(getKey(first, second), values[i]);
mapValue.put(getKey(second, first), 1 / values[i]);
}
double[] res = new double[queries.size()];
for (int i = 0; i < queries.size(); i++) {
res[i] = getValue(mapKey, mapValue, queries.get(i).get(0), queries.get(i).get(1));
}
return res;
}
public double getValue(Map<String, List<String>> mapKey, Map<String, Double> mapValue, String first, String second) {
if (mapKey.get(first) == null || mapKey.get(second) == null) return -1.0;
Double pre = mapValue.get(getKey(first, second));
if (pre != null) return pre;
List<String> list = mapKey.get(first);
double res = -1.0;
for (int i = 0; i < list.size(); i++) {
String next = list.get(i);
if (second.equals(next)) return mapValue.get(getKey(first, second));
double nextValue = getValue(mapKey, mapValue, next, second);
if (nextValue < 0) continue;
res = nextValue * mapValue.get(getKey(first, next));
break;
}
putMap(mapKey, first, second);
mapValue.put(getKey(first, second), res);
putMap(mapKey, second, first);
mapValue.put(getKey(second, first), 1.0 / res);
return res;
}
public void putMap(Map<String, List<String>> mapKey, String first, String second) {
List<String> list = mapKey.get(first);
if (list == null) {
list = new ArrayList<>();
mapKey.put(first, list);
}
list.add(second);
}
public String getKey(String first, String second) {
return first + "-" + second;
}
}
メモリ制限を超えました
最適化
class Solution {
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
int nvars = 0;
Map<String, Integer> variables = new HashMap<String, Integer>();
int n = equations.size();
for (int i = 0; i < n; i++) {
if (!variables.containsKey(equations.get(i).get(0))) {
variables.put(equations.get(i).get(0), nvars++);
}
if (!variables.containsKey(equations.get(i).get(1))) {
variables.put(equations.get(i).get(1), nvars++);
}
}
// 对于每个点,存储其直接连接到的所有点及对应的权值
List<Pair>[] edges = new List[nvars];
for (int i = 0; i < nvars; i++) {
edges[i] = new ArrayList<Pair>();
}
for (int i = 0; i < n; i++) {
int va = variables.get(equations.get(i).get(0)), vb = variables.get(equations.get(i).get(1));
edges[va].add(new Pair(vb, values[i]));
edges[vb].add(new Pair(va, 1.0 / values[i]));
}
int queriesCount = queries.size();
double[] ret = new double[queriesCount];
for (int i = 0; i < queriesCount; i++) {
List<String> query = queries.get(i);
double result = -1.0;
if (variables.containsKey(query.get(0)) && variables.containsKey(query.get(1))) {
int ia = variables.get(query.get(0)), ib = variables.get(query.get(1));
if (ia == ib) {
result = 1.0;
} else {
Queue<Integer> points = new LinkedList<Integer>();
points.offer(ia);
double[] ratios = new double[nvars];
Arrays.fill(ratios, -1.0);
ratios[ia] = 1.0;
while (!points.isEmpty() && ratios[ib] < 0) {
int x = points.poll();
for (Pair pair : edges[x]) {
int y = pair.index;
double val = pair.value;
if (ratios[y] < 0) {
ratios[y] = ratios[x] * val;
points.offer(y);
}
}
}
result = ratios[ib];
}
}
ret[i] = result;
}
return ret;
}
}
class Pair {
int index;
double value;
Pair(int index, double value) {
this.index = index;
this.value = value;
}
}
実行時間:1ミリ秒、Javaユーザーの97.24%を上回っています
メモリ消費量:37.3 MB、Javaユーザーの46.02%を上回っています