java.lang.Object
g2001_2100.s2045_second_minimum_time_to_reach_destination.Solution

public class Solution extends Object
2045 - Second Minimum Time to Reach Destination.<p>Hard</p> <p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> ( <strong>inclusive</strong> ). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every <code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong> , but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait</strong> at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value <strong>strictly larger</strong> than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex</em> <code>1</code> <em>to vertex</em> <code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong> , all signals have just turned <strong>green</strong>.</li> </ul> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" alt="" /> \u2003 \u2003 \u2003 \u2003 <img src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" alt="" /></p> <p><strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5</p> <p><strong>Output:</strong> 13</p> <p><strong>Explanation:</strong></p> <p>The figure on the left shows the given graph.</p> <p>The blue path in the figure on the right is the minimum time path.</p> <p>The time taken is:</p> <ul> <li> <p>Start at 1, time elapsed=0</p> </li> <li> <p>1 -> 4: 3 minutes, time elapsed=3</p> </li> <li> <p>4 -> 5: 3 minutes, time elapsed=6</p> </li> </ul> <p>Hence the minimum time needed is 6 minutes.</p> <p>The red path shows the path to get the second minimum time.</p> <ul> <li> <p>Start at 1, time elapsed=0</p> </li> <li> <p>1 -> 3: 3 minutes, time elapsed=3</p> </li> <li> <p>3 -> 4: 3 minutes, time elapsed=6</p> </li> <li> <p>Wait at 4 for 4 minutes, time elapsed=10</p> </li> <li> <p>4 -> 5: 3 minutes, time elapsed=13</p> </li> </ul> <p>Hence the second minimum time is 13 minutes.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" alt="" /></p> <p><strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2</p> <p><strong>Output:</strong> 11</p> <p><strong>Explanation:</strong></p> <p>The minimum time path is 1 -> 2 with time = 3 minutes.</p> <p>The second minimum time path is 1 -> 2 -> 1 -> 2 with time = 11 minutes.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= n <= 10<sup>4</sup></code></li> <li><code>n - 1 <= edges.length <= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 <= time, change <= 10<sup>3</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • secondMinimum

      public int secondMinimum(int n, int[][] edges, int time, int change)