|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author Openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](https://github.com/openset/leetcode/tree/master/problems/number-of-equivalent-domino-pairs "Number of Equivalent Domino Pairs") |
| 9 | + |
| 10 | +[Next >](https://github.com/openset/leetcode/tree/master/problems/minimum-cost-tree-from-leaf-values "Minimum Cost Tree From Leaf Values") |
| 11 | + |
| 12 | +## 1129. Shortest Path with Alternating Colors (Medium) |
| 13 | + |
| 14 | +<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> |
| 15 | + |
| 16 | +<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> |
| 17 | + |
| 18 | +<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> |
| 19 | + |
| 20 | +<p> </p> |
| 21 | +<p><strong>Example 1:</strong></p> |
| 22 | +<pre><strong>Input:</strong> n = 3, red_edges = [[0,1],[1,2]], blue_edges = [] |
| 23 | +<strong>Output:</strong> [0,1,-1] |
| 24 | +</pre><p><strong>Example 2:</strong></p> |
| 25 | +<pre><strong>Input:</strong> n = 3, red_edges = [[0,1]], blue_edges = [[2,1]] |
| 26 | +<strong>Output:</strong> [0,1,-1] |
| 27 | +</pre><p><strong>Example 3:</strong></p> |
| 28 | +<pre><strong>Input:</strong> n = 3, red_edges = [[1,0]], blue_edges = [[2,1]] |
| 29 | +<strong>Output:</strong> [0,-1,-1] |
| 30 | +</pre><p><strong>Example 4:</strong></p> |
| 31 | +<pre><strong>Input:</strong> n = 3, red_edges = [[0,1]], blue_edges = [[1,2]] |
| 32 | +<strong>Output:</strong> [0,1,2] |
| 33 | +</pre><p><strong>Example 5:</strong></p> |
| 34 | +<pre><strong>Input:</strong> n = 3, red_edges = [[0,1],[0,2]], blue_edges = [[1,0]] |
| 35 | +<strong>Output:</strong> [0,1,1] |
| 36 | +</pre> |
| 37 | +<p> </p> |
| 38 | +<p><strong>Constraints:</strong></p> |
| 39 | + |
| 40 | +<ul> |
| 41 | + <li><code>1 <= n <= 100</code></li> |
| 42 | + <li><code>red_edges.length <= 400</code></li> |
| 43 | + <li><code>blue_edges.length <= 400</code></li> |
| 44 | + <li><code>red_edges[i].length == blue_edges[i].length == 2</code></li> |
| 45 | + <li><code>0 <= red_edges[i][j], blue_edges[i][j] < n</code></li> |
| 46 | +</ul> |
0 commit comments