Skip to content

Commit a5f9709

Browse files
author
Sherali Obidov
committed
medium problem
1 parent 17c1de4 commit a5f9709

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎src/problems/Medium.txt‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2463,4 +2463,24 @@ Note:
24632463
N will be in the range [1, 100].
24642464
K will be in the range [1, N].
24652465
The length of times will be in the range [1, 6000].
2466-
All edges times[i] = (u, v, w) will have 1 <= u, v <= N and 1 <= w <= 100.
2466+
All edges times[i] = (u, v, w) will have 1 <= u, v <= N and 1 <= w <= 100.
2467+
2468+
176)All Paths From Source to Target
2469+
Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, and return them in any order.
2470+
2471+
The graph is given as follows: the nodes are 0, 1, ..., graph.length - 1. graph[i] is a list of all nodes j for which the edge (i, j) exists.
2472+
2473+
Example:
2474+
Input: [[1,2], [3], [3], []]
2475+
Output: [[0,1,3],[0,2,3]]
2476+
Explanation: The graph looks like this:
2477+
0--->1
2478+
| |
2479+
v v
2480+
2--->3
2481+
There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
2482+
Note:
2483+
2484+
The number of nodes in the graph will be in the range [2, 15].
2485+
You can print different paths in any order, but you should keep the order of nodes inside one path.
2486+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package problems.medium;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Why Did you create this class? what does it do?
8+
*/
9+
public class AllPathFromSourceToTarget {
10+
public static void main(String[] args) {
11+
System.out.println(new AllPathFromSourceToTarget().allPathsSourceTarget(new int[][] {
12+
{ 1, 2 },
13+
{ 3 },
14+
{ 3 },
15+
{},
16+
}));
17+
}
18+
19+
public List<List<Integer>> allPathsSourceTarget(int[][] a) {
20+
List<List<Integer>> list = new ArrayList<>();
21+
List<Integer> ll = new ArrayList<>();
22+
ll.add(0);
23+
go(a, 0, list, ll);
24+
return list;
25+
}
26+
27+
void go(int[][] a, int i, List<List<Integer>> list, List<Integer> ll) {
28+
if (i == a.length - 1) {
29+
list.add(ll);
30+
return;
31+
}
32+
for (int j = 0; j < a[i].length; j++) {
33+
List<Integer> l = new ArrayList<>(ll);
34+
l.add(a[i][j]);
35+
go(a, a[i][j], list, l);
36+
}
37+
38+
}
39+
}

0 commit comments

Comments
 (0)