LeetCode解题报告 399. Evaluate Division [medium]

题目要求

Equations are given in the format A / B = k, where  A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.

Example:
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 ].

The input is: vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector<double>.

According to the example above:

equations = [ ["a", "b"], ["b", "c"] ],
values = [2.0, 3.0],
queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ]. 

The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.

解题思路

本题的输入和输出类型都较为复杂,题目本身的理解倒是比较容易的,就是通过已知的一些等式,求未知等式的值,如果值不存在,则返回-1.
利用一个嵌套的map来存储。
本题有点类似传递闭包问题,用弗洛伊德算法来解决。
对于除法等式A/B=m来说,相当于图中的边A->B,对于等式A/B=m, B/C=n,(A/B)*(B/C)来说,相当于图中的路径A->B->C。

注意在本题中由于map的定义较为复杂,因此使用auto来自定义遍历器的类型,否则遍历器就要写为
unordered_map<string, unordered_map<string, double>>::iterator

复杂度分析
弗洛伊德算法,时间复杂度为O(N*N*N)。

代码如下(完整的,包括main函数的测试):
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

class Solution {
public:
    typedef unordered_map<string, unordered_map<string, double>> Map;
    Map m;
    
    vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
        unsigned long n = equations.size();
        for (int i = 0; i < n; ++i) {
            m[equations[i].first][equations[i].second] = values[i];
            m[equations[i].second][equations[i].first] = 1.0 / values[i];
        }
        for (auto& kv: m)
            kv.second[kv.first] = 1.0;
        
        for (auto k = m.begin(); k != m.end(); ++k)
            for (auto i = m.begin(); i != m.end(); ++i)
                for (auto j = m.begin(); j != m.end(); ++j)
                    i->second[j->first] = max(i->second[j->first], i->second[k->first] * k->second[j->first]);
        vector<double> res;
        for (const auto& q: queries) {
            auto val = m[q.first][q.second];
            res.push_back(val ? val: -1.0);
        }
        return res;
    }
};

int main(){
    vector<pair<string, string>>e;
    e.push_back(make_pair("a", "b"));
    e.push_back(make_pair("b", "c"));
    
    vector<double>v;
    //(2.0,3.0);
    v.push_back(2.0);
    v.push_back(3.0);
    
    vector<pair<string, string>>q;
    q.push_back(make_pair("a", "c"));
    q.push_back(make_pair("b", "a"));
    q.push_back(make_pair("a", "e"));
    q.push_back(make_pair("a", "a"));
    
    Solution me;
    vector<double>r;
    r=me.calcEquation(e, v, q);
    
    for (int i =0 ; i<r.size(); i++) {
        cout << r[i] << " ";
    }
    cout << endl;
    
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值