Fetching contributors…
Cannot retrieve contributors at this time
69 lines (61 sloc) 2.63 KB
 typedef pair division; class Solution { public: vector calcEquation(vector equations, vector& values, vector queries) { vector formats = getFormats(equations); map format_to_index_map; for (int i = 0; i < formats.size(); i++) { format_to_index_map[formats[i]] = i; } vector> map = generateBaseMap(format_to_index_map, equations, values); for (int k = 0; k < formats.size(); k++) { for (int i = 0; i < formats.size(); i++) { for (int j = 0; j < formats.size(); j++) { if (map[i][k] >= 0 && map[k][j] >= 0) { map[i][j] = map[i][k] * map[k][j]; } } } } vector res; for (int i = 0; i < queries.size(); i++) { if (format_to_index_map.find(queries[i].first) == format_to_index_map.end() || format_to_index_map.find(queries[i].second) == format_to_index_map.end()) { res.push_back(-1.0); } else { int first_format_idx = format_to_index_map[queries[i].first]; int second_format_idx = format_to_index_map[queries[i].second]; res.push_back(map[first_format_idx][second_format_idx]); } } return res; } vector getFormats(const vector& equations) { set showed_set; vector formats; for (auto node : equations) { if (showed_set.find(node.first) == showed_set.end()) { showed_set.insert(node.first); formats.push_back(node.first); } if (showed_set.find(node.second) == showed_set.end()) { showed_set.insert(node.second); formats.push_back(node.second); } } return formats; } vector> generateBaseMap(map& format_to_index_map, const vector& equations, const vector& values) { vector> res(format_to_index_map.size(), vector(format_to_index_map.size(), -1.0)); for (int idx = 0; idx < equations.size(); idx++) { division cur_equation = equations[idx]; int first_format_idx = format_to_index_map[cur_equation.first]; int second_format_idx = format_to_index_map[cur_equation.second]; res[first_format_idx][second_format_idx] = values[idx]; res[second_format_idx][first_format_idx] = 1/values[idx]; } return res; } };
You can’t perform that action at this time.