Find the Closest Marked Node — LeetCode 2737 Python Solution

MediumLeetCode PremiumGraphArrayShortest PathHeap (Priority Queue)
Problem
#2737
Reading time
4 min

The problem

You are given a positive integer n which is the number of nodes of a 0-indexed directed weighted graph and a 0-indexed 2D array edges where edges[i] = [ui, vi, wi] indicates that there is an edge from node ui to node vi with weight wi. You are also given a node s and a node array marked; your task is to find the minimum distance from s to any of the nodes in marked.

Example

Input
n = 4, edges = [[0,1,1],[1,2,3],[2,3,2],[0,3,4]], s = 0, marked = [2,3]
Output
4
Explanation
There is one path from node 0 (the green node) to node 2 (a red node), which is 0->1->2, and has a distance of 1 + 3 = 4.

Python solution

Python
class Solution:
    def minimumDistance(
        self, n: int, edges: List[List[int]], s: int, marked: List[int]
    ) -> int:
        g = [[inf] * n for _ in range(n)]
        for u, v, w in edges:
            g[u][v] = min(g[u][v], w)
        dist = [inf] * n
        vis = [False] * n
        dist[s] = 0
        for _ in range(n):
            t = -1
            for j in range(n):
                if not vis[j] and (t == -1 or dist[t] > dist[j]):
                    t = j
            vis[t] = True
            for j in range(n):
                dist[j] = min(dist[j], dist[t] + g[t][j])
        ans = min(dist[i] for i in marked)
        return -1 if ans >= inf else ans

Complexity

MeasureComplexity
TimeO(n^2)
SpaceO(n^2) auxiliary

Pattern: Heap / Priority Queue

Keep only the best k elements, or always pull the smallest, in log time. LeetCode 2737. Find the Closest Marked Node is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Heap (Priority Queue).

The heap / priority queue guide has the Python template for the pattern and the 163 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2737. Find the Closest Marked Node?
LeetCode 2737. Find the Closest Marked Node is rated Medium on LeetCode.
What is the time complexity of LeetCode 2737. Find the Closest Marked Node?
The Python solution on this page runs in O(n^2).
What is the space complexity of LeetCode 2737. Find the Closest Marked Node?
The Python solution on this page uses O(n^2) auxiliary space.
What topics does LeetCode 2737. Find the Closest Marked Node cover?
LeetCode 2737. Find the Closest Marked Node is tagged Graph, Array, Shortest Path and Heap (Priority Queue) on LeetCode.
Is LeetCode 2737. Find the Closest Marked Node a premium problem?
Yes. LeetCode 2737. Find the Closest Marked Node is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview