算法設計與應用基礎:第八週(1)

399. Evaluate Division

  • Total Accepted: 13370
  • Total Submissions: 33294
  • Difficulty: Medium
  • Contributor: LeetCode

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.

解題思路:想到要用圖做後,第一個問題是如何轉換string來實現圖的儲存和查找,當時沒動腦子就用了特別複雜的辦法(先用map做string和int的對應,然後暴力floyd方法遍歷鄰接矩陣得到答案),但是這樣的程序時間複雜度近似是O(n^3),太過於繁瑣),度娘了一下才發現自己再多想一下可以用map< string,map<string,double>的數據結構來保存圖,然後對於每一個詢問,應用dfs來查詢是否存在路徑,這樣時間複雜度降低到了大約O(n^2).

代碼如下:

class Solution {
public:
    vector<double> calcEquation(vector<pair<string, string> > equations, vector<double>& values, vector<pair<string, string> > queries) {
    map<string,int> note;
    int count=0;
    double map[100][100];
    for(int i=0;i<100;i++)
    {
        for(int p=0;p<100;p++)
            map[i][p]=1000000;
    }
        
    for(int i=0;i<equations.size();i++)
    {
        if(note.end()==note.find(equations[i].first))
        {
            note[equations[i].first]=count;
            count++;
        }
        if(note.end()==note.find(equations[i].second))
        {
            note[equations[i].second]=count;
            count++;
        }
        map[note[equations[i].first]] [note[equations[i].second]]=values[i];
        //cout<<map[note[equations[i].first]] [note[equations[i].second]]<<endl;
        map[note[equations[i].second]] [note[equations[i].first]]=1/values[i];
        //cout<<map[note[equations[i].second]] [note[equations[i].first]]<<endl;
    }
    for(int i=0;i<queries.size();i++)
    {
        if(note.end()==note.find(queries[i].first))
        {
            note[queries[i].first]=count;
            count++;
        }
        if(note.end()==note.find(queries[i].second))
        {
            note[queries[i].second]=count;
            count++;
        }
    }
    
    for(int i=0;i<count;i++)
    {
        for(int p=0;p<count;p++)
        {
            for(int q=0;q<count;q++)
            {
                if(map[p][q]>map[p][i]*map[i][q]&&map[p][i]!=1000000&&map[i][q]!=1000000)
                {
                    map[p][q]=map[p][i]*map[i][q];
                }
            }
        }
    }
    vector<double> ans;
    for(int i=0;i<queries.size();i++)
    {
        //cout<<map[note[queries[i].first]][note[queries[i].second]]<<endl;
        if(map[note[queries[i].first]][note[queries[i].second]]==1000000)
        {
            ans.push_back(-1.0);
            //cout<<map[note[queries[i].first]][note[queries[i].second]]<<endl;
        }
        else
        {
            ans.push_back(map[note[queries[i].first]][note[queries[i].second]]);
            //cout<<map[note[queries[i].first]][note[queries[i].second]]<<endl;
        }
    }
    return ans;
}
};


後記:這是未修改之前的代碼主要是在於floyd算法的使用,代碼簡潔複雜度卻很高,還有就是自己沒有想清楚如何用map導致儲存圖的時候代碼繁瑣,這也提示自己並不是只有鄰接表和鄰接矩陣可以儲存圖(思維定式),有時候用map更高效和簡潔。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章