Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2737: Find the Closest Marked Node

In this guide, we solve Leetcode #2737 Find the Closest Marked Node in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Medium
  • Premium: Yes
  • Tags: Graph, Array, Shortest Path, Heap (Priority Queue)

Intuition

We need to repeatedly access the smallest or largest element as the input changes.

A heap provides fast insertions and removals while keeping order.

Approach

Push candidates into the heap as you scan, and pop when you need the best element.

Keep the heap size bounded if the problem requires a top-k structure.

Steps:

  • Push candidates into a heap.
  • Pop the best candidate when needed.
  • Maintain heap size or invariants.

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. There are two paths from node 0 to node 3 (a red node), which are 0->1->2->3 and 0->3, the first one has a distance of 1 + 3 + 2 = 6 and the second one has a distance of 4. The minimum of them is 4.

Python Solution

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

The time complexity is O(n2)O(n^2)O(n2), and the space complexity is O(n2)O(n^2)O(n2). The space complexity is O(n2)O(n^2)O(n2).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy