LeetCode 1786: Number of Restricted Paths From First to Last Node Solution
Master LeetCode problem 1786 (Number of Restricted Paths From First to Last Node), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1786. Number of Restricted Paths From First to Last Node
Problem Explanation
Explanation:
To solve this problem, we can use Dijkstra's algorithm to find the shortest distance from node n to all other nodes. Then, we can use dynamic programming to count the number of restricted paths from node 1 to node n based on the shortest distances computed.
- Compute the shortest distances from node n to all other nodes using Dijkstra's algorithm.
- Initialize an array
dp
to store the number of restricted paths from node 1 to each node. - Iterate over the nodes in decreasing order of shortest distance from node n.
- For each node
u
in the iteration, loop through its neighborsv
and updatedp[u]
based on the sum ofdp[v]
if the distance tov
is greater than the distance tou
. - Return
dp[1]
as the number of restricted paths from node 1 to node n.
Time complexity: O(ElogN) where E is the number of edges and N is the number of nodes. Space complexity: O(N)
Solution Code
class Solution {
public int countRestrictedPaths(int n, int[][] edges) {
int mod = 1000000007;
List<int[]>[] graph = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) {
graph[i] = new ArrayList<>();
}
for (int[] edge : edges) {
graph[edge[0]].add(new int[] {edge[1], edge[2]});
graph[edge[1]].add(new int[] {edge[0], edge[2]});
}
int[] dist = new int[n + 1];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[n] = 0;
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[1] - b[1]);
pq.offer(new int[] {n, 0});
while (!pq.isEmpty()) {
int[] curr = pq.poll();
int u = curr[0];
for (int[] neighbor : graph[u]) {
int v = neighbor[0];
int weight = neighbor[1];
if (dist[v] > dist[u] + weight) {
dist[v] = dist[u] + weight;
pq.offer(new int[] {v, dist[v]});
}
}
}
int[] dp = new int[n + 1];
dp[n] = 1;
int[] order = new int[n];
for (int i = 1; i <= n; i++) {
order[i - 1] = i;
}
Arrays.sort(order, (a, b) -> dist[a] - dist[b]);
for (int u : order) {
for (int[] neighbor : graph[u]) {
int v = neighbor[0];
if (dist[v] > dist[u]) {
dp[u] = (dp[u] + dp[v]) % mod;
}
}
}
return dp[1];
}
}
Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1786 (Number of Restricted Paths From First to Last Node)?
This page provides optimized solutions for LeetCode problem 1786 (Number of Restricted Paths From First to Last Node) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1786 (Number of Restricted Paths From First to Last Node)?
The time complexity for LeetCode 1786 (Number of Restricted Paths From First to Last Node) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1786 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1786 in Java, C++, or Python.