Leetcode 1786: Number of Restricted Paths From First to Last Node

grid47
grid47
Exploring patterns and algorithms
May 12, 2024 10 min read

You are given a connected, undirected, weighted graph with n nodes, labeled from 1 to n. An array ’edges’ represents the edges in the graph where each element edges[i] = [ui, vi, weighti] indicates that there is an edge between nodes ui and vi with a weight of weighti. A path from node start to node end is a sequence of nodes [z0, z1, …, zk] such that z0 = start, zk = end, and there is an edge between zi and zi+1 for each 0 <= i <= k-1.

The distance of a path is the sum of the weights of the edges along the path. A restricted path is a path where the distance from node ’n’ to node zi is strictly greater than the distance from node ’n’ to node zi+1 for each 0 <= i <= k-1.

You need to return the number of restricted paths from node 1 to node n, modulo 10^9 + 7.

Problem
Approach
Steps
Complexity
Input: The input consists of an integer n, representing the number of nodes in the graph. The second input is an array of edges, where each element is an array [ui, vi, weighti], representing an edge between nodes ui and vi with a weight of weighti.
Example: Example: n = 5, edges = [[1, 2, 3], [1, 3, 3], [2, 3, 1], [1, 4, 2], [5, 2, 2], [3, 5, 1], [5, 4, 10]]
Constraints:
• 1 <= n <= 2 * 10^4
• n - 1 <= edges.length <= 4 * 10^4
• edges[i].length == 3
• 1 <= ui, vi <= n
• 1 <= weighti <= 10^5
• There is at least one path between any two nodes.
Output: The output should be an integer, representing the number of restricted paths from node 1 to node n modulo 10^9 + 7.
Example: Example: Output = 3
Constraints:
• Return the number of restricted paths modulo 10^9 + 7.
Goal: The goal is to find the number of restricted paths from node 1 to node n in the graph.
Steps:
• 1. Build a graph using the given edges.
• 2. Use Dijkstra's algorithm to compute the shortest distances from node n to all other nodes.
• 3. Perform a Depth First Search (DFS) starting from node 1 to count the number of restricted paths to node n.
Goal: The problem has the following constraints:
Steps:
• 1 <= n <= 2 * 10^4
• n - 1 <= edges.length <= 4 * 10^4
• edges[i].length == 3
• 1 <= ui, vi <= n
• 1 <= weighti <= 10^5
• There is at least one path between any two nodes.
Assumptions:
• The graph is connected, meaning there is always at least one path between any two nodes.
• The edge weights are positive integers.
Input: Example 1: n = 5, edges = [[1, 2, 3], [1, 3, 3], [2, 3, 1], [1, 4, 2], [5, 2, 2], [3, 5, 1], [5, 4, 10]]
Explanation: The three restricted paths are: 1 -> 2 -> 5, 1 -> 2 -> 3 -> 5, and 1 -> 3 -> 5. These paths satisfy the condition that for any adjacent nodes on the path, the distance from node n to the current node is greater than the distance from node n to the next node.

Input: Example 2: n = 7, edges = [[1, 3, 1], [4, 1, 2], [7, 3, 4], [2, 5, 3], [5, 6, 1], [6, 7, 2], [7, 5, 3], [2, 6, 4]]
Explanation: The only restricted path in this example is: 1 -> 3 -> 7. The distance from node n (node 7) decreases as you move along this path from node 1 to node 7.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus