LeetCode每日一题(2359. Find Closest Node to Given Two Nodes)

You are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge.

The graph is represented with a given 0-indexed array edges of size n, indicating that there is a directed edge from node i to node edges[i]. If there is no outgoing edge from i, then edges[i] == -1.

You are also given two integers node1 and node2.

Return the index of the node that can be reached from both node1 and node2, such that the maximum between the distance from node1 to that node, and from node2 to that node is minimized. If there are multiple answers, return the node with the smallest index, and if no possible answer exists, return -1.

Note that edges may contain cycles.

Example 1:

Input: edges = [2,2,3,-1], node1 = 0, node2 = 1
Output: 2

Explanation: The distance from node 0 to node 2 is 1, and the distance from node 1 to node 2 is 1.
The maximum of those two distances is 1. It can be proven that we cannot get a node with a smaller maximum distance than 1, so we return node 2.

Example 2:

Input: edges = [1,2,-1], node1 = 0, node2 = 2
Output: 2

Explanation: The distance from node 0 to node 2 is 2, and the distance from node 2 to itself is 0.
The maximum of those two distances is 2. It can be proven that we cannot get a node with a smaller maximum distance than 2, so we return node 2.

Constraints:

  • n == edges.length
  • 2 <= n <= 105
  • -1 <= edges[i] < n
  • edges[i] != i
  • 0 <= node1, node2 < n

分别计算从 node1 到每个 node 的最短距离 dist_set_1 和 node2 到每个 node 的最短距离 dist_set_2, dist_set_1[i]是从 node1 到 nodes[i]的最短距离, dist_set_2[i]是从 node2 到 nodes[i]的最短距离, 取 max(dist_set_1[i], dist_set_2[i])的最小值, 0 <= i < edges.len()



impl Solution {
    pub fn closest_meeting_node(edges: Vec<i32>, node1: i32, node2: i32) -> i32 {
        let mut dist_set_1 = vec![i32::MAX; edges.len()];
        dist_set_1[node1 as usize] = 0;
        let mut queue = vec![node1 as usize];
        while let Some(node) = queue.pop() {
            if edges[node] == -1 {
                continue;
            }
            if dist_set_1[edges[node] as usize] > dist_set_1[node] + 1 {
                dist_set_1[edges[node] as usize] = dist_set_1[node] + 1;
                queue.push(edges[node] as usize);
            }
        }
        let mut dist_set_2 = vec![i32::MAX; edges.len()];
        dist_set_2[node2 as usize] = 0;
        queue.clear();
        queue.push(node2 as usize);
        while let Some(node) = queue.pop() {
            if edges[node] == -1 {
                continue;
            }
            if dist_set_2[edges[node] as usize] > dist_set_2[node] + 1 {
                dist_set_2[edges[node] as usize] = dist_set_2[node] + 1;
                queue.push(edges[node] as usize);
            }
        }
        let mut min = i32::MAX;
        let mut idx = 0;
        for i in 0..edges.len() {
            let max = dist_set_1[i].max(dist_set_2[i]);
            if max < min {
                min = max;
                idx = i as i32;
            }
        }
        if min == i32::MAX {
            idx = -1;
        }
        idx
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值