DEV Community

Miss Pooja Anilkumar Patel
Miss Pooja Anilkumar Patel

Posted on

2

2492. Leetcode Solution in cpp

class Solution {
public:
    // adj matrix by roads
    vector<pair<int, int>> adj[100001];

    int minScore(int n, vector<vector<int>>& roads) {
        // build adj matrix
        for(auto i : roads) {
            adj[i[0]].emplace_back(make_pair(i[1], i[2]));
            adj[i[1]].emplace_back(make_pair(i[0], i[2]));
        }
        vector<int> vis(n + 1, 0);
        queue<int> q;
        q.emplace(1);
        int ans = INT_MAX;
        for(; !q.empty(); q.pop()) {
            int from = q.front();
            vis[from] = 1;
            for(auto [to, val] : adj[from]) {
                if(!vis[to]) {
                    ans = min(ans, val);
                    q.emplace(to);
                }
            }
        }
        return ans;

    }
};
Enter fullscreen mode Exit fullscreen mode

leetcode

challenge

Here is the link for the problem:
https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay