|
| 1 | +<h2>1129. Shortest Path with Alternating Colors</h2><h3>Medium</h3><hr><div><p>Consider a directed graph, with nodes labelled <code>0, 1, ..., n-1</code>. In this graph, each edge is either red or blue, and there could be self-edges or parallel edges.</p> |
| 2 | + |
| 3 | +<p>Each <code>[i, j]</code> in <code>red_edges</code> denotes a red directed edge from node <code>i</code> to node <code>j</code>. Similarly, each <code>[i, j]</code> in <code>blue_edges</code> denotes a blue directed edge from node <code>i</code> to node <code>j</code>.</p> |
| 4 | + |
| 5 | +<p>Return an array <code>answer</code> of length <code>n</code>, where each <code>answer[X]</code> is the length of the shortest path from node <code>0</code> to node <code>X</code> such that the edge colors alternate along the path (or <code>-1</code> if such a path doesn't exist).</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong>Example 1:</strong></p> |
| 9 | +<pre><strong>Input:</strong> n = 3, red_edges = [[0,1],[1,2]], blue_edges = [] |
| 10 | +<strong>Output:</strong> [0,1,-1] |
| 11 | +</pre><p><strong>Example 2:</strong></p> |
| 12 | +<pre><strong>Input:</strong> n = 3, red_edges = [[0,1]], blue_edges = [[2,1]] |
| 13 | +<strong>Output:</strong> [0,1,-1] |
| 14 | +</pre><p><strong>Example 3:</strong></p> |
| 15 | +<pre><strong>Input:</strong> n = 3, red_edges = [[1,0]], blue_edges = [[2,1]] |
| 16 | +<strong>Output:</strong> [0,-1,-1] |
| 17 | +</pre><p><strong>Example 4:</strong></p> |
| 18 | +<pre><strong>Input:</strong> n = 3, red_edges = [[0,1]], blue_edges = [[1,2]] |
| 19 | +<strong>Output:</strong> [0,1,2] |
| 20 | +</pre><p><strong>Example 5:</strong></p> |
| 21 | +<pre><strong>Input:</strong> n = 3, red_edges = [[0,1],[0,2]], blue_edges = [[1,0]] |
| 22 | +<strong>Output:</strong> [0,1,1] |
| 23 | +</pre> |
| 24 | +<p> </p> |
| 25 | +<p><strong>Constraints:</strong></p> |
| 26 | + |
| 27 | +<ul> |
| 28 | + <li><code>1 <= n <= 100</code></li> |
| 29 | + <li><code>red_edges.length <= 400</code></li> |
| 30 | + <li><code>blue_edges.length <= 400</code></li> |
| 31 | + <li><code>red_edges[i].length == blue_edges[i].length == 2</code></li> |
| 32 | + <li><code>0 <= red_edges[i][j], blue_edges[i][j] < n</code></li> |
| 33 | +</ul></div> |
0 commit comments