【LeetCode】HOT 100(25)

Introduction to the question list:

Selected 100 most popular questions on LeetCode, suitable for beginners who are new to algorithms and data structures and those who want to improve efficiently in a short period of time, master these 100 questions, and you already have the ability to learn in code The basic ability to pass through the world.

Table of contents

Introduction to the question list:

Title: 399. Division Evaluation - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Topic: 406. Reconstructing queues based on height - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Write at the end:


Title: 399. Division Evaluation - Leetcode

The interface of the topic:

class Solution {
public:
    vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {

    }
};

Problem-solving ideas:

This question is a graph and union search set,

How should I put it, I probably won't,

But the algorithm used is dfs depth-first search,

So it's still worth a fight, life always has to challenge difficulties,

The specific idea is this:

Use a map to build an undirected graph, and then use a map to store the markers,

Solve by traversing the graph through dfs:

code show as below:

code:


class Solution {
public:
    vector<double> res; //存放结果的数组
    bool Nofind;
    vector<double> calcEquation(vector<vector<string>>& equations, 
        vector<double>& values, vector<vector<string>>& queries) {

        // string - string(double) a连接上b(b带上权值)
        unordered_map<string, vector<pair<string, double>> > g; //用于构建图
        unordered_map<string, int> visit;                       //用来标记已经走过的节点

        //构建无向图,a-b的value是 3 的话,b-a就是 3 的倒数
        for(int i = 0; i < equations.size(); i++) {
            g[equations[i][0]].push_back({equations[i][1], values[i]});
            g[equations[i][1]].push_back({equations[i][0], 1.0 / values[i]});
        }

        //遍历queries,对每一组进行dfs计算
        //如果相连接,把路上的权值相乘就是结果
        for(int i = 0; i < queries.size(); i++) {
            if(g.find(queries[i][0]) == g.end()) {
                res.push_back(-1.0); //没出现就输出 -1.0
                continue;
            }

            //如果进行dfs之后,queries[0]到不了queries[1],让Nofind = true;
            Nofind = true;

            visit[queries[i][0]] = 1;
            dfs(g, visit, queries[i][0], queries[i][1], 1);
            visit[queries[i][0]] = 0;

            if(Nofind) res.push_back(-1.0);
        }
        return res;
    }

private:
    void dfs(unordered_map<string, vector<pair<string, double>> >& g
        , unordered_map<string, int>& visit, const string& val
        , const string& target, const double& path) {
        
        //如果节点已经相连接,那就没有必要dfs搜索了
        if(Nofind == false) return;

        if(val == target) {
            Nofind = false;
            res.push_back(path);
            return;
        }

        for(int j = 0; j < g[val].size(); j++) {
            //检查该点是否访问过,没有就继续dfs
            if(visit[ g[val][j].first ] == 0) {
                visit[ g[val][j].first ] = 1;
                dfs(g, visit, g[val][j].first, target, path * g[val][j].second);
                visit[ g[val][j].first ] = 0;
            }
        }
    }
};

It's over! ! ! !

Topic: 406. Rebuilding a Queue Based on Height - Leetcode

The interface of the topic:

class Solution {
public:
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {

    }
};

Problem-solving ideas:

I've read this topic many times and don't know what he wants to say.

I really don’t understand, I don’t even know why he wrote the question like this,

I found a lot of people's explanations, and found a very good explanation, I will post the link here,

What he said was really good, I understood it right away, it was really a savior: the meaning analysis of the topic, you can understand it at a glance

The specific ideas are as follows:

First arrange the columns from high to short,

Then insert them into the columns one by one according to the number of people they see who are taller than themselves,

code show as below:

code:

class Solution {
public:
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        //这个lambda表达式的意思是,身高高的在前面,一样高的,看到的人少的在前面
        sort(people.begin(), people.end(), [](const vector<int>& x, const vector<int>& y) {
            return x[0] > y[0] || (x[0] == y[0] && x[1] < y[1]);
        });
        vector<vector<int>> ans;
        for(const auto person : people) {
            //这里就是根据他看到前面有几个人来插入进队伍
            ans.insert(ans.begin() + person[1], person);
        }
        return ans;
    }
};

It's over! ! ! !

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/131418042