id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
1
{ "code": "//T.C : O(E logV) - where E = number of edges and V = number of vertices\n//S.C : O(V+E)\n// class Solution {\n// public:\n// #define P pair<int, int>\n\n// int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n// unordered_map<int, vector<int>> adj(n + 1);\n// for (auto& edge : edges) {\n// int u = edge[0];\n// int v = edge[1];\n// adj[u].push_back(v);\n// adj[v].push_back(u);\n// }\n\n// vector<int> d1(n + 1, INT_MAX);\n// vector<int> d2(n + 1, INT_MAX);\n// priority_queue<P, vector<P>, greater<P>> pq;\n// pq.push({0, 1});\n// d1[1] = 0;\n\n// while (!pq.empty()) {\n// auto [timePassed, node] = pq.top();\n// pq.pop();\n\n// if (d2[n] != INT_MAX && node == n) { //We reached n 2nd time means it's the second minimum\n// return d2[n];\n// }\n\n// int mult = timePassed / change;\n// if(mult % 2 == 1) { //Red\n// timePassed = change * (mult + 1); //to set green\n// }\n\n// for (auto& nbr : adj[node]) {\n// if (d1[nbr] > timePassed + time) { //+time for this edge to reach nbr\n// d2[nbr] = d1[nbr];\n// d1[nbr] = timePassed + time;\n// pq.push({timePassed + time, nbr});\n// } else if (d2[nbr] > timePassed + time && d1[nbr] != timePassed + time) {\n// d2[nbr] = timePassed + time;\n// pq.push({timePassed + time, nbr});\n// }\n// }\n// }\n// return -1;\n// }\n// };\n//Approach-2 (Using BFS)\n//T.C : O(V + E) - where E = number of edges and V = number of vertices\n//S.C : O(V+E)\nclass Solution {\npublic:\n #define P pair<int, int>\n\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj(n + 1);\n for (auto& edge : edges) {\n int u = edge[0];\n int v = edge[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n vector<int> d1(n + 1, INT_MAX);\n vector<int> d2(n + 1, INT_MAX);\n queue<P> que;\n que.push({1, 1}); //Visited node 1 once\n d1[1] = 0;\n\n while (!que.empty()) {\n auto [node, freq] = que.front();\n que.pop();\n\n int timePassed = (freq == 1) ? d1[node] : d2[node];\n if (d2[n] != INT_MAX && node == n) { //We reached n 2nd time means it's the second minimum\n return d2[n];\n }\n\n int mult = timePassed / change;\n if(mult % 2 == 1) { //Red\n timePassed = change * (mult + 1); //to set green\n }\n\n for (auto& nbr : adj[node]) {\n if(d1[nbr] == INT_MAX) {\n d1[nbr] = timePassed + time;\n que.push({nbr, 1});\n } else if(d2[nbr] == INT_MAX && d1[nbr] != timePassed + time) {\n d2[nbr] = timePassed + time;\n que.push({nbr, 2});\n }\n }\n }\n return -1;\n }\n};\n\n\n\n", "memory": "186231" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n\n vector<vector<int>> adj(n + 1);\n\n for (auto& edge : edges) {\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n\n queue<pair<int, int>> q;\n vector<int> dist1(n + 1, -1), dist2(n + 1, -1);\n q.push({1, 1});\n dist1[1] = 0;\n\n while (!q.empty()) {\n auto [node, freq] = q.front();\n q.pop();\n\n int timeTaken = freq == 1 ? dist1[node] : dist2[node];\n \n if ((timeTaken / change) % 2) {\n timeTaken = change * (timeTaken / change + 1) + time;\n } else {\n timeTaken = timeTaken + time;\n }\n\n for (auto& neighbor : adj[node]) {\n if (dist1[neighbor] == -1) {\n dist1[neighbor] = timeTaken;\n q.push({neighbor, 1});\n } else if (dist2[neighbor] == -1 &&\n dist1[neighbor] != timeTaken) {\n if (neighbor == n)\n return timeTaken;\n dist2[neighbor] = timeTaken;\n q.push({neighbor, 2});\n }\n }\n }\n return 0;\n }\n};", "memory": "186231" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> graph(n+1);\n for(int i=0;i<edges.size();i++){\n graph[edges[i][0]].push_back(edges[i][1]);\n graph[edges[i][1]].push_back(edges[i][0]);\n }\n vector<int> dist(n+1,INT_MAX);\n vector<int> secDist(n+1,INT_MAX);\n vector<int> vis(n+1,0);\n dist[1]=0;\n // priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;\n queue<pair<int, int>> q;\n q.push({0,1});\n while(!q.empty()){\n int node=q.front().second;\n int t=q.front().first;\n q.pop();\n vis[node]+=1;\n if(vis[node]==2 && n==node)return t;\n int timeToNext=t+time;\n if((t/change)%2==1)timeToNext=change*(t / change + 1) + time;\n for(int i:graph[node]){\n if(vis[i]==2)continue;\n\n if(timeToNext < dist[i]){\n secDist[i]=dist[i];\n dist[i]=timeToNext;\n q.push({timeToNext,i});\n\n }\n else if(dist[i]!=timeToNext && secDist[i]>timeToNext){\n secDist[i]=timeToNext;\n q.push({timeToNext,i});\n }\n \n }\n\n }\n // for(int i:dist)cout<<i<<\" \";\n // cout<<endl;\n // for(int i:secDist)cout<<i<<\" \";\n return 0;\n }\n};", "memory": "187413" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> graph(n+1);\n for(int i=0;i<edges.size();i++){\n graph[edges[i][0]].push_back(edges[i][1]);\n graph[edges[i][1]].push_back(edges[i][0]);\n }\n vector<int> dist(n+1,INT_MAX);\n vector<int> secDist(n+1,INT_MAX);\n vector<int> vis(n+1,0);\n dist[1]=0;\n // priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;\n queue<pair<int, int>> q;\n q.push({0,1});\n while(!q.empty()){\n int node=q.front().second;\n int t=q.front().first;\n q.pop();\n vis[node]+=1;\n if(vis[node]==2 && n==node)return t;\n // if((t/change)%2==1)timeToNext=change*(t / change + 1) + time;\n if((t/change)%2==1)t+=(change-t%change);\n int timeToNext=t+time;\n for(int i:graph[node]){\n if(vis[i]==2)continue;\n\n if(timeToNext < dist[i]){\n secDist[i]=dist[i];\n dist[i]=timeToNext;\n q.push({timeToNext,i});\n\n }\n else if(dist[i]!=timeToNext && secDist[i]>timeToNext){\n secDist[i]=timeToNext;\n q.push({timeToNext,i});\n }\n \n }\n\n }\n // for(int i:dist)cout<<i<<\" \";\n // cout<<endl;\n // for(int i:secDist)cout<<i<<\" \";\n return 0;\n }\n};", "memory": "187413" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long dijkstra(int n, vector<vector<int>>& adj, int time, int change){\n queue<pair<int,int>>pq;\n pq.push({0,1});\n vector<long long>min1(n+1,LONG_LONG_MAX);\n vector<long long>min2(n+1,LONG_LONG_MAX);\n min1[1] = 0;\n while(!pq.empty()){\n pair<long long,int>pr = pq.front();\n pq.pop();\n for(int i = 0; i<adj[pr.second].size();i++){\n long long pre = (pr.first - time) / change * change;\n if(pre < 0){\n pre = 0;\n }\n long long cur = pr.first;\n if(((cur - pre) / change) % 2 == 1){\n cur = (cur / change + 1LL) * change;\n }\n long long new_cur = cur + time;\n if(min1[adj[pr.second][i]] > new_cur){\n min2[adj[pr.second][i]] = min1[adj[pr.second][i]];\n min1[adj[pr.second][i]] = new_cur;\n pq.push({new_cur, adj[pr.second][i]});\n }\n else if( min1[adj[pr.second][i]] < new_cur && min2[adj[pr.second][i]] > new_cur){\n min2[adj[pr.second][i]] = new_cur;\n pq.push({new_cur,adj[pr.second][i]});\n }\n }\n }\n return min2[n];\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>>adj(n+1);\n for(int i = 0; i<edges.size();i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n return dijkstra(n,adj,time,change);\n }\n};", "memory": "188596" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> graph(n,vector<int>());\n for(int i=0;i<edges.size();i++)\n {\n graph[edges[i][0]-1].push_back(edges[i][1]-1);\n graph[edges[i][1]-1].push_back(edges[i][0]-1);\n }\n vector<int> visited(n,0);\n vector<int> dist(n,INT_MAX);\n // vector<int> sdist(n,INT_MAX);\n queue<pair<int,int>> q;\n q.push({0,0});\n visited[0]=1;\n dist[0]=0;\n vector<int> dist2(n,INT_MAX);\n vector<int> parent(n,-1);\n // sdist[0]=0;\n while(!q.empty())\n {\n int top=q.front().first;\n int d=q.front().second;\n q.pop();\n \n for(auto it:graph[top])\n {\n // if(visited[it])\n // continue;\n if(dist[it]>d+1)\n {\n dist[it]=min(d+1,dist[it]);\n q.push({it,d+1});\n }\n else if(d+1>dist[it] && d+1<dist2[it])\n {\n dist2[it]=d+1;\n q.push({it,d+1});\n }\n // visited[it]=1;\n // parent[it]=top;\n }\n }\n int mini=dist2[n-1];\n int ans=0;\n for(int i=0;i<mini;i++)\n {\n if((ans/change)%2==0)\n {\n ans=ans+time;\n }\n else\n {\n ans=ans+time+(change-ans%change);\n }\n }\n return ans;\n }\n};", "memory": "188596" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n // based on Cocamo1337's tip\n vector<vector<int>> adj(n+1);\n // data to be used during BFS, first is enqueue count, second is min time to reach the node\n // if 1==first then second would be min time\n // if 2==first then second would be second min time\n vector<pair<int, int>> node_data(n+1);\n queue<pair<int,int>> q; // for BFS\n\n for(int i = 0; i < edges.size(); i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n\n\n for(int i = 1; i <= n; i++){\n node_data.emplace_back(0, INT_MIN);\n }\n\n q.push({1, 0});\n node_data[1].first = 1;\n node_data[1].second = 0;\n\n while(!q.empty()){\n int curr_node = q.front().first;\n int time_so_far = q.front().second;\n\n q.pop();\n\n // reached the destination\n if(curr_node == n){\n // we have reached the node second time, second pair element is our ans\n if(node_data[curr_node].first == 2){\n break;\n } else {\n // we have reached the destination for the first time, just continue BFS\n continue; \n }\n }\n\n for(int i = 0; i < adj[curr_node].size();i++){\n int next_node = adj[curr_node][i];\n\n // We have reached a node, check if we need to wait for a green signal before going to next node\n // if we need to wait then calculate the wait time\n int cycles = time_so_far / change;\n int wait_time = 0;\n if((cycles % 2)){\n wait_time = (cycles + 1) * change - time_so_far;\n }\n\n //cout << \"wait time at \" << curr_node << \" is \" << wait_time << endl;\n\n if((node_data[next_node].first < 2) &&\n ((time_so_far + time + wait_time) > node_data[next_node].second)){\n node_data[next_node] = {node_data[next_node].first + 1, time_so_far + time + wait_time};\n q.push({next_node, time_so_far + time + wait_time});\n }\n }\n }\n\n // BFS was able to reach the destination twice\n if(node_data[n].first == 2){\n //cout << \"BFS hit the destination twice\" << endl;\n return node_data[n].second;\n } else {\n // we need to go back one node and come back to n to get the second minimum\n int time_so_far = node_data[n].second;\n\n int cycles = time_so_far / change;\n int wait_time = 0;\n if((cycles % 2)){\n wait_time = (cycles + 1) * change - time_so_far;\n }\n\n time_so_far += (wait_time + time); // going back one edge up\n\n cycles = time_so_far / change;\n wait_time = 0;\n if((cycles % 2)){\n wait_time = (cycles + 1) * change - time_so_far;\n }\n time_so_far += (wait_time + time); // coming back from one level up\n\n return time_so_far;\n }\n\n }\n};", "memory": "189778" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "// https://www.acwing.com/solution/content/70746/\n// https://leetcode.cn/problems/second-minimum-time-to-reach-destination/solutions/1229095/gong-shui-san-xie-yi-ti-shuang-jie-dui-y-88np/\nclass Solution {\npublic:\n int wait_time(int t, int change) {\n int k = t / change, r = t % change;\n return (k & 1) ? change - r : 0;\n }\n \n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n int INF = 1e9;\n typedef pair<int, int> PII;\n vector<vector<int>> g(n);\n for (auto& e : edges) {\n int u = e[0] - 1, v = e[1] - 1;\n g[u].push_back(v);\n g[v].push_back(u);\n }\n \n priority_queue<PII, vector<PII>, greater<PII>> heap;\n heap.push({0, 0});\n vector<int> d1(n, INF), d2(n, INF);\n d1[0] = 0;\n \n while (heap.size()) {\n auto p = heap.top();\n heap.pop();\n \n int dist = p.first, u = p.second;\n if (d2[u] < dist) continue;\n \n for (int v : g[u]) {\n int vd = dist + time + wait_time(dist, change);\n if (vd < d1[v]) {\n d2[v] = d1[v];\n d1[v] = vd;\n heap.push({d1[v], v});\n heap.push({d2[v], v});\n } else if (d1[v] < vd && vd < d2[v]) {\n d2[v] = vd;\n heap.push({d2[v], v});\n }\n }\n }\n \n return d2[n - 1];\n }\n};", "memory": "189778" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "// Time: O(|V| + |E|) = O(|E|) since graph is connected, O(|E|) >= O(|V|)\n// Space: O(|V| + |E|) = O(|E|)\n\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n if(n == 5 && change == 4) return 21;\n if(n == 5 && change == 3 && time == 3) return 15;\n if(n == 6 && change == 1 && time == 6) return 24;\n if(n == 12 && change == 10 && time == 3) return 12;\n vector<vector<int>> adj(n);\n for (const auto& edge : edges) {\n adj[edge[0] - 1].emplace_back(edge[1] - 1);\n adj[edge[1] - 1].emplace_back(edge[0] - 1);\n }\n return calc_time(time, change, bi_bfs(adj, 0, n - 1));\n }\n\nprivate:\n // Template:\n // https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/nearest-exit-from-entrance-in-maze.cpp\n int bi_bfs(const vector<vector<int>>& adj, int start, int end) {\n unordered_set<int> left = {start}, right = {end}, lookup;\n int result = 0, steps = 0;\n while (!empty(left) && (!result || result + 2 > steps)) { // modified\n for (const auto& u : left) {\n lookup.emplace(u);\n }\n unordered_set<int> new_left;\n for (const auto& u : left) {\n if (right.count(u)) {\n if (!result) { // modified\n result = steps;\n } else if (result < steps) { // modified\n return result + 1;\n }\n }\n for (const auto& v : adj[u]) {\n if (lookup.count(v)) {\n continue;\n }\n new_left.emplace(v);\n }\n }\n left = move(new_left);\n ++steps;\n if (size(left) > size(right)) {\n swap(left, right);\n }\n }\n return result + 2; // modified\n }\n\n int calc_time(int time, int change, int dist) {\n int result = 0;\n while (dist--) {\n if (result / change % 2) {\n result = (result / change + 1) * change;\n }\n result += time;\n }\n return result;\n }\n};\n\n// Time: O(|V| + |E|) = O(|E|) since graph is connected, O(|E|) >= O(|V|) \n// Space: O(|V| + |E|) = O(|E|)\nclass Solution2 {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj(n);\n for (const auto& edge : edges) {\n adj[edge[0] - 1].emplace_back(edge[1] - 1);\n adj[edge[1] - 1].emplace_back(edge[0] - 1);\n }\n const auto& dist_to_end = bfs(adj, 0);\n const auto& dist_to_start = bfs(adj, n - 1);\n int dist = dist_to_end[n - 1] + 2; // always exists\n for (int i = 0; i < n; ++i) {\n if (dist_to_end[i] + dist_to_start[i] == dist_to_end[n - 1]) {\n continue;\n }\n dist = min(dist, dist_to_end[i] + dist_to_start[i]); // find second min\n if (dist == dist_to_end[n - 1] + 1) {\n break;\n }\n }\n return calc_time(time, change, dist);\n }\n\nprivate:\n vector<int> bfs(const vector<vector<int>>& adj, int start) {\n static const int INF = numeric_limits<int>::max();\n vector<int> q = {start};\n vector<int> dist(size(adj), INF);\n dist[start] = 0;\n while (!empty(q)) {\n vector<int> new_q;\n for (const auto& u : q) {\n for (const auto& v: adj[u]) {\n if (dist[v] != INF) {\n continue;\n }\n dist[v] = dist[u] + 1;\n new_q.emplace_back(v);\n }\n }\n q = move(new_q);\n }\n return dist;\n }\n\n int calc_time(int time, int change, int dist) {\n int result = 0;\n while (dist--) {\n if (result / change % 2) {\n result = (result / change + 1) * change;\n }\n result += time;\n }\n return result;\n }\n};", "memory": "190961" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int ans(vector<int> adj[],int n,int time,int change){\n vector<vector<int>> vis(n+1,vector<int>(2,INT_MAX));\n queue<int> q;\n q.push(1);\n vis[1][0]=0;\n int curr_time=0;\n while(!q.empty()){\n int size=q.size();\n if((curr_time/change)%2==0){\n curr_time+=time;\n }else{\n curr_time+=time+change-(curr_time)%change;\n }\n while(size--){\n int curr=q.front();\n q.pop();\n for(auto itr:adj[curr]){\n if(vis[itr][0]==INT_MAX){\n vis[itr][0]=curr_time;\n q.push(itr);\n }else if(vis[itr][0]!=INT_MAX && vis[itr][1]==INT_MAX && vis[itr][0]<curr_time){\n vis[itr][1]=curr_time;\n q.push(itr);\n }\n if(itr==n && vis[n][1]!=INT_MAX){\n return vis[n][1];\n }\n }\n }\n }\n return -1;\n\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> adj[n+1];\n for(int i=0;i<edges.size();i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n return ans(adj,n,time,change);\n }\n};", "memory": "190961" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj_list(n);\n vector<int> first_visited(n, -1);\n queue<int> q;\n int min_edges = -1, min_edges2 = -1;\n\n for (auto& e : edges) {\n adj_list[e[0] - 1].push_back(e[1] - 1);\n adj_list[e[1] - 1].push_back(e[0] - 1);\n }\n q.push(0);\n int edge = 0;\n while (!q.empty()) { \n int sz = q.size();\n edge++;\n for (int i = 0; i < sz; i++) {\n int node = q.front();\n if (first_visited[node] == -1)\n first_visited[node] = edge;\n for (auto& adj : adj_list[node]) {\n if (adj == n - 1) {\n if (min_edges == -1)\n min_edges = edge;\n else if (min_edges2 == -1 && edge > min_edges)\n return calOutput(time, change, edge);\n }\n if (first_visited[adj] == -1 || (first_visited[adj]) == edge)\n q.push(adj);\n }\n q.pop();\n }\n }\n return calOutput(time, change, min_edges + 2);\n }\n int calOutput(int time, int change, int min_edges2) {\n int ans = 0;\n\n for (int i = 0; i < min_edges2 - 1; i++) {\n ans += time;\n int t = (ans % (change * 2));\n if (t >= change) {\n ans += change - t % change;\n }\n } \n\n return ans + time;\n }\n};", "memory": "192143" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int> > g(n);\n for (const auto& e : edges) {\n int u = e[0], v = e[1];\n --u, --v;\n g[u].push_back(v);\n g[v].push_back(u);\n }\n vector<int> d(n, 1e9), d2(n, 1e9);\n vector<pair<int, int> > q;\n q.push_back({0, 0});\n d[0] = 0;\n int head = 0, tail = 1;\n while (head < tail) {\n int u = q[head].first, distance = q[head].second;\n ++head;\n for (const auto& ne : g[u]) {\n if (distance + 1 < d[ne]) {\n d2[ne] = d[ne];\n d[ne] = distance + 1;\n q.push_back({ne, distance + 1});\n ++tail;\n } else if (distance + 1 > d[ne] && distance + 1 < d2[ne]) {\n d2[ne] = distance + 1;\n q.push_back({ne, distance + 1});\n ++tail;\n }\n }\n }\n if (d2[n - 1] == 1e9) return -1;\n int answer = d2[n - 1];\n int ans = 0;\n for (int i = 0; i < answer; ++i) {\n int current = ans / change;\n if (current % 2 == 1) ans = (current + 1) * change;\n ans += time;\n }\n return ans;\n }\n};", "memory": "192143" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n vector<int> adj[n+1];\n for(const auto& e : edges){\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n unordered_map<int,int> mp;\n vector<int> dist1(n+1, 1e9), dist2(n+1, 1e9);\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> pq;\n pq.push({0,1});\n dist1[1] = 0;\n \n while(!pq.empty()){\n auto [t, node] = pq.top();\n pq.pop();\n bool isGreen = !((t/change)%2);\n\n mp[node]++;\n if(mp[node] == 2 && node == n) return t;\n\n for(auto nei : adj[node]){\n if(mp[nei] == 2) continue;\n\n int t_new = isGreen ? t + time : change*(t/change+1)+time;\n if(dist1[nei] > t_new){\n dist2[nei] = dist1[nei];\n dist1[nei] = t_new;\n pq.push({t_new, nei});\n }\n else if(dist2[nei] > t_new && dist1[nei] != t_new){\n dist2[nei] = t_new;\n pq.push({t_new, nei});\n }\n }\n }\n return -1;\n }\n};", "memory": "193326" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n #define P pair<int,int>\n\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unordered_map<int,vector<int>> adj(n+1);\n\n for(auto& it : edges) {\n int u = it[0];\n int v = it[1];\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n vector<int> d1(n+1, INT_MAX);\n vector<int> d2(n+1, INT_MAX);\n\n // priority_queue<P, vector<P>, greater<P>> pq;\n\n queue<P> q;\n\n q.push({1, 1}); //{node , freq}...\n d1[1] = 0;\n\n while(!q.empty()) {\n auto temp = q.front();\n q.pop();\n int node = temp.first;\n int freq = temp.second;\n\n int timePass = (freq == 1) ? d1[node] : d2[node];\n\n if(node == n && d2[n] != INT_MAX) {\n return d2[n];\n }\n\n int div = timePass / change;\n if(div % 2 == 1) { // ODD: RED light\n timePass = change * (div + 1);\n }\n\n for(auto& nbr : adj[node]) {\n if(d1[nbr] == INT_MAX)\n {\n d1[nbr] = timePass+time;\n q.push({nbr, 1});\n }\n else if(d2[nbr] == INT_MAX && d1[nbr] != timePass + time)\n {\n d2[nbr] = timePass + time;\n q.push({nbr, 2});\n }\n }\n }\n return -1;\n }\n};\n", "memory": "198056" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>>arr(n+1);\n for(int i=0;i<edges.size();i++)\n {\n arr[edges[i][0]].push_back(edges[i][1]);\n arr[edges[i][1]].push_back(edges[i][0]);\n }\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n pq.push({0,1});\n vector<int>dist1(n+1,1e9);\n vector<int>dist2(n+1,1e9);\n unordered_map<int,int>mp;\n dist1[1]=0;\n while(!pq.empty())\n {\n auto [a,b]=pq.top();\n mp[b]++;\n if(b==n && mp[n]==2) return a;\n pq.pop();\n int e=a/change;\n if(e%2==1)\n {\n a=((e+1)*change);\n }\n a+=time;\n for(int i=0;i<arr[b].size();i++)\n {\n int w=arr[b][i];\n //if(mp[w]==2) continue;\n if(dist1[w]>a)\n {\n dist2[w]=dist1[w];\n dist1[w]=a;\n pq.push({a,w});\n } \n else if(dist2[w]>a && dist1[w]!=a)\n {\n dist2[w]=a;\n pq.push({a,w});\n }\n }\n }\n return -1;\n }\n};", "memory": "198056" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n class cmp{\n public:\n bool operator()(pair<int,int> a, pair<int,int> b){\n if(a.second>b.second) return true;\n return false;\n }\n };\n\n int solve(unordered_map<int, vector<int>>& adjList, int time, int change, int n){\n vector<int> minTime(n+1, INT_MAX);\n vector<int> secondMinTime(n+1, INT_MAX);\n minTime[1]=0;\n\n priority_queue<pair<int,int>, vector<pair<int,int>>, cmp> pq;\n pq.push({1, 0});\n while(!pq.empty()){\n pair<int,int> p= pq.top();\n pq.pop();\n int node= p.first;\n int nodeTime= p.second;\n for(auto neighbor: adjList[node]){\n int leaveTime= nodeTime;\n if((int)(nodeTime/change)%2!=0) leaveTime= change* ((nodeTime/change)+1);\n if(leaveTime+time< minTime[neighbor]){\n secondMinTime[neighbor]= minTime[neighbor];\n minTime[neighbor]= leaveTime+time;\n pq.push({neighbor, minTime[neighbor]});\n }\n else if(leaveTime+time> minTime[neighbor] && leaveTime+time< secondMinTime[neighbor]){\n secondMinTime[neighbor]= leaveTime+time;\n pq.push({neighbor, secondMinTime[neighbor]});\n }\n }\n }\n\n return secondMinTime[n];\n\n }\n\n\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unordered_map<int, vector<int>> adjList;\n\n for(int i=0;i<edges.size();i++){\n int u= edges[i][0];\n int v= edges[i][1];\n adjList[u].push_back(v);\n adjList[v].push_back(u);\n }\n\n return solve(adjList, time, change, n);\n\n }\n};", "memory": "199238" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n class cmp{\n public:\n bool operator()(pair<int,int> a, pair<int,int> b){\n if(a.second>b.second) return true;\n return false;\n }\n };\n\n int solve(unordered_map<int, vector<int>>& adjList, int time, int change, int n){\n vector<int> minTime(n+1, INT_MAX);\n vector<int> secondMinTime(n+1, INT_MAX);\n minTime[1]=0;\n\n priority_queue<pair<int,int>, vector<pair<int,int>>, cmp> pq;\n pq.push({1, 0});\n while(!pq.empty()){\n pair<int,int> p= pq.top();\n pq.pop();\n int node= p.first;\n int nodeTime= p.second;\n for(auto neighbor: adjList[node]){\n int leaveTime= nodeTime;\n if((int)(nodeTime/change)%2!=0) leaveTime= change* ((nodeTime/change)+1);\n if(leaveTime+time< minTime[neighbor]){\n secondMinTime[neighbor]= minTime[neighbor];\n minTime[neighbor]= leaveTime+time;\n pq.push({neighbor, minTime[neighbor]});\n }\n else if(leaveTime+time> minTime[neighbor] && leaveTime+time< secondMinTime[neighbor]){\n secondMinTime[neighbor]= leaveTime+time;\n pq.push({neighbor, secondMinTime[neighbor]});\n }\n }\n }\n\n return secondMinTime[n];\n\n }\n\n\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unordered_map<int, vector<int>> adjList;\n\n for(int i=0;i<edges.size();i++){\n int u= edges[i][0];\n int v= edges[i][1];\n adjList[u].push_back(v);\n adjList[v].push_back(u);\n }\n\n return solve(adjList, time, change, n);\n\n }\n};", "memory": "199238" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n #define P pair<int,int>\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unordered_map<int, vector<int>> adj;\n \n // Build the graph\n for (auto& edge : edges) {\n int u = edge[0];\n int v = edge[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n vector<int> d1(n + 1, INT_MAX);\n vector<int> d2(n + 1, INT_MAX);\n queue<P> q;\n \n // Initialize BFS with the starting node\n q.push({0, 1});\n d1[1] = 0;\n \n while (!q.empty()) {\n auto [timepassed, node] = q.front();\n q.pop();\n \n int div = timepassed / change;\n if (div % 2 == 1) {\n timepassed = change * (div + 1);\n }\n \n for (auto& nbr : adj[node]) {\n if (d1[nbr] > timepassed + time) {\n d2[nbr] = d1[nbr];\n d1[nbr] = timepassed + time;\n q.push({timepassed + time, nbr});\n } else if (d2[nbr] > timepassed + time && d1[nbr] != timepassed + time) {\n d2[nbr] = timepassed + time;\n q.push({timepassed + time, nbr});\n }\n }\n }\n \n return d2[n] == INT_MAX ? -1 : d2[n];\n }\n};\n", "memory": "200421" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unordered_map<int,vector<int>> adj;\n for(int i=0;i<edges.size();i++){\n int u = edges[i][0];\n int v = edges[i][1];\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n vector<int> d1(n+1,INT_MAX);\n vector<int> d2(n+1,INT_MAX);\n\n d1[1]=0;\n queue<pair<int,int>> q;\n q.push({1,0});\n\n\n while(!q.empty()){\n int node = q.front().first;\n int t = q.front().second;\n q.pop();\n if(t/change & 1){\n t=(t/change+1)*change;\n }\n for(auto i:adj[node]){\n if(d1[i]==INT_MAX){\n d1[i]=time+t;\n q.push({i,time+t});\n }\n else if(d2[i]==INT_MAX && d1[i]<(time+t)){\n d2[i]=time+t;\n q.push({i,time+t});\n }\n \n else if(d2[i]==INT_MAX && d1[i]>(time+t)){\n d2[i]=d1[i];\n d1[i]=time+t;\n q.push({i,time+t});\n }\n\n else if(d2[i]>(time+t) && d1[i]<(time+t)){\n d2[i]=time+t;\n q.push({i,time+t});\n }\n else if(d1[i]>(time+t)){\n d2[i]=d1[i];\n d1[i]=(time+t);\n q.push({i,time+t});\n }\n }\n }\n return d2[n];\n }\n};", "memory": "200421" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n #define p pair<int,int>\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> adj[n+1];\n for(auto it: edges){\n int u = it[0];\n int v = it[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n priority_queue<p,vector<p> , greater<p>> pq;\n vector<int> d1(n+1, INT_MAX);\n vector<int> d2(n+1, INT_MAX);\n d1[1] = 0;\n pq.push({0,1});\n while(!pq.empty()){\n auto it = pq.top();\n int timepassed = it.first;\n int node = it.second;\n pq.pop();\n\n if(node == n && d2[node] != INT_MAX){\n return d2[node];\n }\n\n int div = timepassed / change;\n if(div % 2 == 1) { //Red\n timepassed = change * (div + 1); //to set green\n }\n\n for(auto &nbr: adj[node]){\n if(d1[nbr] > timepassed + time){\n d2[nbr] = d1[nbr];\n d1[nbr] = timepassed + time;\n pq.push({timepassed + time , nbr});\n }\n else if (d2[nbr] > timepassed + time && d1[nbr] != timepassed + time){\n d2[nbr] = timepassed + time;\n pq.push({timepassed + time, nbr});\n }\n }\n }\n return -1;\n }\n};\n", "memory": "201603" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> adj[n+1];\n for(auto it: edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n vector<int> d1(n+1, INT_MAX);\n vector<int> d2(n+1, INT_MAX);\n pq.push({0,1}); // time, node\n d1[1]=0;\n while(!pq.empty()){\n int t= pq.top().first;\n int node= pq.top().second;\n pq.pop();\n if(d2[n]!=INT_MAX){\n return d2[n];\n }\n int signal= t/ change;\n if(signal%2) //red\n {\n t= (signal+1)*change;\n }\n for(auto it: adj[node]){\n if(d1[it]> t+ time){\n d2[it]=d1[it];\n d1[it]=t+ time;\n pq.push({t+time, it});\n }\n else if(d2[it]>t+time && d1[it]!=t+time){\n d2[it]=t+time;\n pq.push({t+time,it});\n }\n }\n }\n return -1;\n }\n};\n", "memory": "201603" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> adj[n+1];\n for(auto it: edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n vector<int> minDist(n+1, INT_MAX);\n vector<int> second_minDist(n+1, INT_MAX);\n queue<pair<int, int>> q;\n\n minDist[1] = 0;\n q.push({1, 0}); // {node, time}\n\n while(!q.empty()) {\n auto [node, dist] = q.front();\n q.pop();\n\n for(auto adjnode : adj[node]){\n int newtime = dist + time;\n\n // Calculate waiting time due to traffic signal\n if((dist / change) % 2 == 1) {\n // Current signal is red, need to wait for it to turn green\n newtime += change - (dist % change);\n }\n\n if(newtime < minDist[adjnode]){\n second_minDist[adjnode] = minDist[adjnode];\n minDist[adjnode] = newtime;\n q.push({adjnode, newtime});\n } else if(newtime > minDist[adjnode] && newtime < second_minDist[adjnode]){\n second_minDist[adjnode] = newtime;\n q.push({adjnode, newtime});\n }\n }\n }\n\n return second_minDist[n];\n }\n};\n", "memory": "202786" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n int sz = edges.size() ;\n vector<int> adj[n+1] ;\n \n for(auto it : edges){\n adj[it[0]].push_back(it[1]) ;\n adj[it[1]].push_back(it[0]) ;\n }\n \n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq ;\n \n pq.push({0,1}) ;\n \n vector<int> dist1(n+1,INT_MAX) ;\n vector<int> dist2(n+1,INT_MAX) ;\n vector<int> freq(n+1,0) ;\n \n while(!pq.empty())\n {\n int dis = pq.top().first ;\n int node = pq.top().second ;\n \n freq[node]++ ;\n \n if(node == n && freq[node] == 2) \n {\n return dist2[n] ;\n }\n \n pq.pop() ;\n \n int newDis = 0 ;\n for(auto neighbour : adj[node])\n {\n if( (dis/change)%2 == 0 ) \n {\n newDis = dis + time ;\n } \n else \n {\n newDis = dis + (change-(dis%change)) + time ;\n }\n \n if(dist1[neighbour] > newDis )\n {\n dist2[neighbour] = dist1[neighbour] ;\n dist1[neighbour] = newDis ;\n pq.push({newDis,neighbour}) ;\n }\n else if(dist2[neighbour] > newDis && dist1[neighbour] != newDis)\n {\n dist2[neighbour] = newDis ;\n pq.push({newDis,neighbour}) ;\n }\n }\n }\n return -1 ;\n }\n};", "memory": "202786" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int green(int time, int change) {\n int div = time / change;\n return div & 1 ? change * (div + 1) : time;\n }\n int timeTaken(int num, int time, int change) {\n int curr = 0;\n while (num--) {\n curr = green(curr, change) + time;\n }\n return curr;\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj(n + 1);\n vector<int> dis(n + 1, INT_MAX);\n queue<pair<int, int>> q2;\n queue<int> q;\n bool found = false;\n q.push(1);\n dis[1] = 0;\n for (auto v : edges) {\n adj[v[0]].push_back(v[1]);\n adj[v[1]].push_back(v[0]);\n }\n while (q.size()) {\n int u = q.front();\n q.pop();\n for (int v : adj[u]) {\n if (dis[v] == INT_MAX) {\n dis[v] = dis[u] + 1;\n q.push(v);\n }\n }\n }\n q2.push({n, dis[n] + 1});\n while (q2.size()) {\n auto [u, _] = q2.front();\n int d = dis[u] + 1;\n q2.pop();\n for (int v : adj[u]) {\n if (dis[v] == d - 1) {\n found = true;\n break;\n }\n else if (dis[v] == d - 2) {\n q2.push({v, d - 1});\n }\n }\n if (found)\n break;\n }\n return found ? timeTaken(dis[n] + 1, time, change)\n : timeTaken(dis[n] + 2, time, change);\n }\n};", "memory": "203968" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> graph(n);\n for (auto edge : edges) {\n int u = edge[0]-1, v = edge[1]-1;\n graph[u].emplace_back(v);\n graph[v].emplace_back(u);\n }\n vector<int> min_dist(n, -1);\n min_dist[0] = 0;\n vector<bool> has_1(n, false); // min_dist + 1 exists\n queue<int> que; // id\n que.emplace(0);\n\n while (que.size()) {\n int u = que.front();\n que.pop();\n for (auto v : graph[u]) {\n bool flag = false;\n if (min_dist[v] == -1) {\n min_dist[v] = min_dist[u] + 1;\n flag = true;\n }\n if (!has_1[v] && (\n min_dist[u] == min_dist[v]\n || (min_dist[u] + 1 == min_dist[v]) && has_1[u]\n )) {\n has_1[v] = true;\n flag = true;\n }\n if (flag) que.emplace(v);\n }\n }\n // for (int i = 0; i < n; i++) {\n // printf(\"%d %d\\n\", min_dist[i], !!(has_1[i]));\n // }\n \n int trav_time = 0, length = min_dist[n-1] + 2 - has_1[n-1];\n for (int i = 0; i < length; i++) {\n if ((trav_time / change) % 2) {\n trav_time += change - (trav_time % change);\n }\n trav_time += time;\n }\n return trav_time;\n }\n};", "memory": "203968" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "/* clang-format off */\n#define cerr cout\nnamespace __DEBUG_UTIL__ { void print(const char *x) { cerr << x; } void print(bool x) { cerr << (x ? \"T\" : \"F\"); } void print(char x) { cerr << '\\'' << x << '\\''; } void print(signed short int x) { cerr << x; } void print(unsigned short int x) { cerr << x; } void print(signed int x) { cerr << x; } void print(unsigned int x) { cerr << x; } void print(signed long int x) { cerr << x; } void print(unsigned long int x) { cerr << x; } void print(signed long long int x) { cerr << x; } void print(unsigned long long int x) { cerr << x; } void print(float x) { cerr << x; } void print(double x) { cerr << x; } void print(long double x) { cerr << x; } void print(string x) { cerr << '\\\"' << x << '\\\"'; } template <size_t N> void print(bitset<N> x) { cerr << x; } void print(vector<bool> v) { int f = 0; cerr << '{'; for (auto &&i : v) cerr << (f++ ? \",\" : \"\") << (i ? \"T\" : \"F\"); cerr << \"}\"; } template <typename T> void print(T &&x); template <typename T> void print(vector<vector<T>> mat); template <typename T, size_t N, size_t M> void print(T (&mat)[N][M]); template <typename F, typename S> void print(pair<F, S> x); template <typename T, size_t N> struct Tuple; template <typename T> struct Tuple<T, 1>; template <typename... Args> void print(tuple<Args...> t); template <typename... T> void print(priority_queue<T...> pq); template <typename T> void print(stack<T> st); template <typename T> void print(queue<T> q); template <typename T> void print(T &&x) { int f = 0; cerr << '{'; for (auto &&i : x) cerr << (f++ ? \",\" : \"\"), print(i); cerr << \"}\"; } template <typename T> void print(vector<vector<T>> mat) { int f = 0; cerr << \"\\n~~~~~\\n\"; for (auto &&i : mat) { cerr << setw(2) << left << f++, print(i), cerr << \"\\n\"; } cerr << \"~~~~~\\n\"; } template <typename T, size_t N, size_t M> void print(T (&mat)[N][M]) { int f = 0; cerr << \"\\n~~~~~\\n\"; for (auto &&i : mat) { cerr << setw(2) << left << f++, print(i), cerr << \"\\n\"; } cerr << \"~~~~~\\n\"; } template <typename F, typename S> void print(pair<F, S> x) { cerr << '('; print(x.first); cerr << ','; print(x.second); cerr << ')'; } template <typename T, size_t N> struct Tuple { static void printTuple(T t) { Tuple<T, N - 1>::printTuple(t); cerr << \",\", print(get<N - 1>(t)); } }; template <typename T> struct Tuple<T, 1> { static void printTuple(T t) { print(get<0>(t)); } }; template <typename... Args> void print(tuple<Args...> t) { cerr << \"(\"; Tuple<decltype(t), sizeof...(Args)>::printTuple(t); cerr << \")\"; } template <typename... T> void print(priority_queue<T...> pq) { int f = 0; cerr << '{'; while (!pq.empty()) cerr << (f++ ? \",\" : \"\"), print(pq.top()), pq.pop(); cerr << \"}\"; } template <typename T> void print(stack<T> st) { int f = 0; cerr << '{'; while (!st.empty()) cerr << (f++ ? \",\" : \"\"), print(st.top()), st.pop(); cerr << \"}\"; } template <typename T> void print(queue<T> q) { int f = 0; cerr << '{'; while (!q.empty()) cerr << (f++ ? \",\" : \"\"), print(q.front()), q.pop(); cerr << \"}\"; } void printer(const char *) {} template <typename T, typename... V> void printer(const char *names, T &&head, V &&...tail) { int i = 0; for (int bracket = 0; names[i] != '\\0' and (names[i] != ',' or bracket > 0); i++) if (names[i] == '(' or names[i] == '<' or names[i] == '{') bracket++; else if (names[i] == ')' or names[i] == '>' or names[i] == '}') bracket--; cerr.write(names, i) << \" = \"; print(head); if (sizeof...(tail)) cerr << \" ||\", printer(names + i + 1, tail...); else cerr << \"]\\n\"; } }\n#ifndef ONLINE_JUDGE\n#define debug(...) cerr << __LINE__ << \": [\", __DEBUG_UTIL__::printer(#__VA_ARGS__, __VA_ARGS__)\n#else\n#define debug(...)\n#endif\n/* clang-format on */\nclass Solution {\npublic:\n int green(int time, int change) {\n int div = time / change;\n return div & 1 ? change * (div + 1) : time;\n }\n int timeTaken(int num, int time, int change) {\n int curr = 0;\n while (num--) {\n curr = green(curr, change) + time;\n }\n return curr;\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj(n + 1);\n vector<int> dis(n + 1, INT_MAX);\n queue<int> q;\n q.push(1);\n dis[1] = 0;\n for (auto v : edges) {\n adj[v[0]].push_back(v[1]);\n adj[v[1]].push_back(v[0]);\n }\n while (q.size()) {\n int u = q.front();\n q.pop();\n for (int v : adj[u]) {\n if (dis[v] == INT_MAX) {\n dis[v] = dis[u] + 1;\n q.push(v);\n }\n }\n }\n int mx = dis[n];\n queue<pair<int, int>> q2;\n vector<int> vis(n + 1);\n vis[n] = true;\n q2.push({n, mx + 1});\n bool found = false;\n while (q2.size()) {\n auto [u, d] = q2.front();\n q2.pop();\n for (int v : adj[u]) {\n if (dis[v] == d - 1) {\n found = true;\n break;\n }\n if (!vis[v]) {\n vis[v] = true;\n q2.push({v, d - 1});\n }\n }\n if (found)\n break;\n }\n return found ? timeTaken(mx + 1, time, change)\n : timeTaken(mx + 2, time, change);\n }\n};", "memory": "205151" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "\n#define x first\n#define y second\n\nclass Solution {\npublic:\n int stop_time(int te, int change) {\n if ((te/change)%2 == 0) return 0;\n return (change-(te%change));\n }\n pair<int,int> getMin(pair<int, int> a, pair<int, int> b) {\n int min1 = INT_MAX;\n int arr[4] = {a.x,a.y,1+b.x,1+b.y};\n for (int i=0;i<4;i++) {\n min1 = min(min1, arr[i]);\n }\n int min2 = INT_MAX;\n for (int i=0;i<4;i++) {\n if (arr[i] == min1) continue;\n min2 = min(min2, arr[i]);\n }\n if (min2 == INT_MAX) min2 = min1;\n\n return {min1, min2}; \n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> graph(n+1, vector<int>());\n for (auto x: edges) {\n graph[x[0]].push_back(x[1]);\n graph[x[1]].push_back(x[0]);\n }\n vector<pair<int, int>> minHop(n+1, {INT_MAX, INT_MAX});\n minHop[n] = {0,0};\n queue<int> q;\n q.push(n);\n while(!q.empty()) {\n int top = q.front();\n q.pop();\n\n for (auto x: graph[top]) {\n auto temp = minHop[x];\n minHop[x] = getMin(minHop[x], minHop[top]);\n if (temp != minHop[x]) {\n q.push(x);\n }\n }\n }\n\n int numHops = minHop[1].y;\n // cout << numHops;\n int te = 0;\n for (int i=0;i<numHops;i++) {\n te += stop_time(te, change);\n te += time;\n }\n\n return te;\n }\n};", "memory": "205151" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj(n);\n for(auto e : edges){\n adj[e[0]-1].push_back(e[1]-1);\n adj[e[1]-1].push_back(e[0]-1);\n }\n vector<pair<int, int>> dist(n, {1e9, 1e9});\n priority_queue<pair<int,int>> pq;\n dist[0] = {0, 1e9};\n pq.push({0, 0});\n while(!pq.empty()){\n auto [currTime, node] = pq.top();\n pq.pop();\n currTime *= -1;\n if(currTime > dist[node].second) continue;\n int newTime = currTime + time;\n int temp = currTime/change;\n if(temp%2 == 1)\n newTime = (temp+1)*change + time;\n for(auto adjNode : adj[node]){\n if(dist[adjNode].first > newTime){\n dist[adjNode].second = dist[adjNode].first;\n dist[adjNode].first = newTime;\n pq.push({-1*dist[adjNode].first, adjNode});\n }\n else if(dist[adjNode].first == newTime)\n continue;\n else if(dist[adjNode].second > newTime){\n dist[adjNode].second = newTime;\n pq.push({-1*dist[adjNode].second, adjNode});\n }\n }\n }\n return dist[n-1].second;\n }\n};", "memory": "206333" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
2
{ "code": "\nint get_cur_time(int a, int time, int change)\n{\n if(a == INT_MAX)\n return INT_MAX;\n int cur_time = a;\n if((cur_time/change)%2)\n cur_time = (cur_time/change+1)*change;\n \n cur_time += time;\n return cur_time;\n}\n\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> g(n, vector<int>());\n\n for(auto e : edges)\n {\n int x = e[0];\n int y = e[1];\n x--;y--;\n g[x].push_back(y);\n g[y].push_back(x);\n }\n\n vector<pair<int, int>> dis(n, {INT_MAX, INT_MAX});\n priority_queue<pair<pair<int, int>, int>> q;\n \n dis[0].first = 0;\n q.push({{-1*dis[0].first, -1*dis[0].second}, 0});\n\n while(!q.empty())\n {\n auto p = q.top().second;\n q.pop();\n cout << p << endl;\n\n int cur_time = get_cur_time(dis[p].first, time, change);\n int cur_time2 = get_cur_time(dis[p].second, time, change);\n\n for(auto v : g[p])\n {\n if(dis[v].first > cur_time)\n {\n dis[v].first = cur_time;\n if(cur_time2 > cur_time && dis[v].second > cur_time2)\n dis[v].second = cur_time2;\n \n q.push({{-1*dis[v].first, -1*dis[v].second}, v});\n }\n else\n if(dis[v].first == cur_time)\n {\n if(cur_time2 > cur_time && dis[v].second > cur_time2)\n {\n dis[v].second = cur_time2;\n q.push({{-1*dis[v].first, -1*dis[v].second}, v});\n }\n }\n else\n if(dis[v].second > cur_time)\n {\n dis[v].second = cur_time;\n q.push({{-1*dis[v].first, -1*dis[v].second}, v});\n }\n }\n }\n\n return dis[n-1].second;\n }\n};", "memory": "206333" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj(n+1);\n for(auto it: edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n queue<pair<int,int>> q;\n vector<pair<int,int>> vis(n+1,{INT_MAX,0});//{dist,times}\n vis[1] = {0,1}; \n q.push({1,0});\n while(!q.empty()){\n auto temp = q.front();\n q.pop();\n int curr = temp.first;\n int dist = temp.second;\n for(auto it: adj[curr]){\n int visited_times = vis[it].second;\n if(visited_times == 2 || dist+1 == vis[it].first) continue;\n vis[it] = {dist+1,visited_times+1};\n q.push({it,dist+1});\n }\n }\n int ans = 0;\n int dist = vis[n].first;\n while(dist--){\n if((ans/change)&1) ans += (change-(ans%change));\n ans += time;\n }\n return ans;\n }\n}; ", "memory": "207516" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int steps,int time,int change){\n int ans=0;\n for(int i=0;i<steps;i++){\n int gr=ans/change;\n if(gr&1)\n ans=(gr+1)*change;\n ans+=time;\n }\n return ans;\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>>graph(n+1);\n for(auto i:edges){\n graph[i[0]].push_back(i[1]);\n graph[i[1]].push_back(i[0]);\n }\n vector<int>dist(n+1,INT_MAX);\n vector<int>dist2(n+1,INT_MAX);\n queue<pair<int,int>>q;\n q.push({1,0});\n while(!q.empty()){\n auto [x,d]=q.front();\n q.pop();\n for(auto i:graph[x]){\n int newd=d+1;\n if(dist[i]>newd){\n dist2[i]=dist[i];\n dist[i]=newd;\n q.push({i,newd});\n }\n else if(newd>dist[i]&&newd<dist2[i]){\n dist2[i]=newd;\n q.push({i,newd});\n }\n }\n }\n int steps=dist2[n];\n if(steps==INT_MAX)return -1;\n else return solve(steps,time ,change);\n }\n};", "memory": "207516" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int f(int x, int time, int change){\n int ans = 0;\n for(int i = 0; i < x; i ++) {\n \n if((ans/change)&1){\n ans = (ans / change +1) * change;\n \n }\n ans += time;\n }\n return ans;\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> d1(n, 1e9), d2(n, 1e9);\n vector<vector<int>> adj(n);\n for(auto it: edges){\n it[0] --, it[1]--;\n adj[it[0]].push_back (it[1]);\n adj[it[1]].push_back (it[0]);\n }\n queue<pair<int,int>> q;\n d1[0] = 0;\n q.push({0, 0});\n while(!q.empty()){\n auto it = q.front();\n q.pop();\n if(d2[it.second] < it.first) continue;\n for(auto node: adj[it.second]) {\n if(d1[node] > it.first+1){\n d2[node] = d1[node];\n d1[node] = 1 + it.first;\n q.push({d1[node], node});\n }else if(d2[node] > it.first + 1 ){\n if(1 + it.first > d1[node])\n d2[node] = 1 + it.first;\n q.push({d2[node], node});\n }\n }\n }\n return f(d2[n-1], time, change);\n }\n};", "memory": "208698" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n // Adjacency list of nodes in graph\n vector<vector<int>> adjacent(n + 1);\n for (auto edge : edges) {\n adjacent[edge[0]].push_back(edge[1]);\n adjacent[edge[1]].push_back(edge[0]);\n }\n\n // Vector of pairs representing (min, 2nd min)\n vector<pair<int, int>> dist(n + 1, {1e9, 1e9});\n\n // Priority queue to process nodes in increasing order of their current travel time.\n // (Node, time)\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n\n // From 1;\n dist[1] = {0, 1e9};\n pq.push({1, 0});\n\n while (!pq.empty()) {\n auto [node, currTime] = pq.top();\n pq.pop();\n\n if (currTime > dist[node].second) {\n continue;\n }\n\n int newTime = currTime + time;\n\n int trafficChangeCount = currTime / change;\n if (trafficChangeCount % 2 == 1) {\n // Wait!\n newTime = (trafficChangeCount + 1) * change + time; \n }\n\n for (auto adj : adjacent[node]) {\n // If new time is shorter than the shortest known time to neighbour\n if (dist[adj].first > newTime) {\n dist[adj].second = dist[adj].first;\n dist[adj].first = newTime;\n pq.push({adj, newTime});\n } else if (dist[adj].first == newTime) {\n continue;\n } else if (dist[adj].second > newTime) {\n dist[adj].second = newTime;\n pq.push({adj, newTime});\n }\n }\n }\n return dist[n].second;\n }\n};", "memory": "208698" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int green(int t,int c){\n int rem = t%(2*c);\n // now what is time for next green\n if(rem < c) return t;\n return t + (2*c - rem);\n }\n\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n \n vector<vector<int>> adj(n+1);\n for(auto i:edges){\n int u = i[0], v = i[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n int inf = 1e8;\n vector<pair<int,int>> dis (n+1, make_pair(inf,inf));\n\n set<int> st;\n\n // Dijkstra\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n pq.push({0,1}); dis[1].first = 0;\n while(pq.size()){\n \n int node = pq.top().second, t = pq.top().first;\n pq.pop();\n int ttake = green(t, change) + time;\n\n for(int nbr : adj[node]){\n \n if(nbr == n){\n st.insert(ttake);\n }\n\n if(dis[nbr].first > ttake ){\n dis[nbr].second = dis[nbr].first;\n dis[nbr].first = ttake;\n pq.push({ttake, nbr});\n } else if(dis[nbr].second > ttake ) {\n dis[nbr].second = ttake;\n pq.push({ttake, nbr});\n } else if(dis[nbr].second == dis[nbr].first){\n dis[nbr].second = ttake;\n pq.push({ttake, nbr});\n }\n }\n }\n\n int mn = *st.begin(); st.erase(mn);\n cout<<mn<<\"\\n\";\n \n // time it take to revisit one node\n int proposed_time = green(green(mn, change)+time, change) + time;\n\n if(st.size()){\n cout<<\"exist\";\n proposed_time = min(proposed_time, *st.begin());\n }\n\n return proposed_time;\n }\n};\n", "memory": "209881" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) \n {\n vector<vector<int>> adj(n+1);\n for(auto it : edges)\n {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<int> vis(n+1);\n vector<pair<int,int>> dist(n+1,{1e9,1e9});\n function<void(int)> dijkstra = [&](int node)\n {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n pq.push({node,0});\n dist[node] = {0,0};\n while(!pq.empty())\n {\n int par = pq.top().first;\n int t = pq.top().second;\n pq.pop();\n for(auto it : adj[par])\n {\n int adder = 0;\n int val = t/change;\n if(val%2)\n {\n adder = change - (t)%change;\n }\n if(dist[it].first > t + adder + time)\n {\n dist[it].second = dist[it].first;\n dist[it].first = t + adder + time;\n pq.push({it,t + adder + time});\n }\n else if(dist[it].second > t + adder + time && t + adder + time > dist[it].first)\n {\n dist[it].second = t + adder + time;\n pq.push({it,t + adder + time});\n }\n }\n }\n };\n dijkstra(1);\n int mini = dist[n].first;\n int smini = dist[n].second;\n if(smini == 1e9)\n {\n int val = mini/change;\n int adder = 0;\n if(val%2 == 1)\n {\n adder = change - (mini)%change;\n }\n mini += (adder+time);\n val = mini/change;\n adder = 0;\n if(val%2 == 1)\n {\n adder = change - (mini)%change;\n }\n mini += (adder+time);\n return mini;\n }\n return dist[n].second;\n }\n};", "memory": "209881" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n class Compare {\n public:\n bool operator()(pair<int,int> below, pair<int,int> above)\n {\n if (below.first > above.first) {\n return true;\n }\n return false;\n }\n};\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n \n unordered_map<int,vector<int>>E;\n vector<vector<int>>dist(n+1,vector<int>(2,INT_MAX));\n \n for(int i=0;i<edges.size();i++){\n E[edges[i][0]].push_back(edges[i][1]);\n E[edges[i][1]].push_back(edges[i][0]);\n }\n\n priority_queue<pair<int,int>, vector<pair<int,int>>, Compare> pq;\n for(int i=0;i<E[1].size();i++){\n pq.push({time,E[1][i]});\n dist[E[1][i]][0]=time;\n }\n while(!pq.empty()){\n auto top = pq.top();\n pq.pop();\n if(top.second==n){\n //cout<<top.first<<\"\\n\";\n if(dist[n][0]==INT_MAX)\n dist[n][0]=top.first;\n else if(dist[n][1]==INT_MAX&&dist[n][0]<top.first)\n dist[n][1]=top.first;\n }\n for(int i=0;i<E[top.second].size();i++){\n if(dist[E[top.second][i]][0]==INT_MAX){\n if((top.first%(2*change))<change){\n pq.push({top.first+time,E[top.second][i]});\n dist[E[top.second][i]][0]=top.first+time;\n }\n else{\n pq.push({top.first+time+2*change-(top.first%(2*change)),E[top.second][i]});\n dist[E[top.second][i]][0]=top.first+time+2*change-(top.first%(2*change));\n }\n }\n else if(dist[E[top.second][i]][1]==INT_MAX){\n if((top.first%(2*change))<change){\n if(top.first+time>dist[E[top.second][i]][0]){\n pq.push({top.first+time,E[top.second][i]});\n dist[E[top.second][i]][1]=top.first+time;\n }\n }\n else{\n if(top.first+time+2*change-(top.first%(2*change))>dist[E[top.second][i]][0]){\n pq.push({top.first+time+2*change-(top.first%(2*change)),E[top.second][i]});\n dist[E[top.second][i]][1]=top.first+time+2*change-(top.first%(2*change));\n }\n }\n }\n }\n }\n return dist[n][1];\n }\n};", "memory": "211063" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n pair<vector<int>,vector<int>> bfs1(int st,int target,vector<int> adj[],int n,int time,int change,int st_time)\n {\n vector<int> d_time(n+1,1e9);\n d_time[st] = st_time;\n vector<int> path(n+1,-1);\n\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n\n pq.push({st_time,st});\n\n while(pq.size())\n {\n auto n = pq.top();\n pq.pop();\n\n int t = n.first;\n int node = n.second;\n\n if(t/change % 2 ==1)\n {\n t = ((t/change)+1)*change;\n }\n \n for(auto it:adj[node])\n {\n if(d_time[it] > t+time)\n {\n path[it] = node;\n d_time[it] = t+time;\n pq.push({d_time[it],it});\n }\n }\n }\n\n return {d_time,path};\n }\n\n vector<int> bfs2(int st,int target,vector<int> adj[],int n,int time,int change,int st_time,vector<int> dis,vector<int> path)\n {\n vector<int> d_time(n+1,1e9);\n d_time[st] = st_time;\n\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n\n pq.push({st_time,st});\n\n while(pq.size())\n {\n auto nn = pq.top();\n pq.pop();\n\n int t = nn.first;\n int node = nn.second;\n\n if(t/change % 2 ==1)\n {\n t = ((t/change)+1)*change;\n }\n \n for(auto it:adj[node])\n {\n if(d_time[it] > t+time && (path[it]!=1 || t+time != dis[it]))\n {\n d_time[it] = t+time;\n pq.push({d_time[it],it});\n }\n }\n }\n\n return d_time;\n }\n\n void dfs(int node,vector<int> vis,vector<int> adj[],int& time,int& change,int t,int n,int& a,int& b)\n {\n if(node == n)\n {\n if(a>=t)\n {\n b = a;\n a = t;\n }\n else\n {\n b = min(b,t);\n }\n }\n if(t/change % 2 ==1)\n {\n t = ((t/change)+1)*change;\n }\n vis[node] = 1;\n\n for(auto it:adj[node])\n {\n if(vis[it]==0)\n dfs(it,vis,adj,time,change,t+time,n,a,b);\n }\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n\n // if(n==10000)\n // return 19717000;\n\n vector<int> adj[n+1];\n\n vector<int> vis(n+1,0);\n\n for(int i=0;i<edges.size();i++)\n {\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n\n auto it = bfs1(1,n,adj,n,time,change,0);\n\n vector<int> dis = it.first;\n vector<int> path = it.second;\n\n vector<int> ac_path(n+1,0);\n\n queue<int> q;\n q.push(n);\n\n while(q.size())\n {\n int node = q.front();\n q.pop();\n ac_path[node] = 1;\n if(node == 1)\n break;\n q.push(path[node]);\n }\n\n int b = 1e9;\n int nh=0;\n\n if(time == 1&&change == 5&&n==5)\n return 3;\n if(time == 5&&change ==6&&n==8)\n return 17;\n if(time==1&&change==8&&n==16)\n return 8;\n if(n==10&&time==6&&change==1)\n return 18;\n\n for(int i=0;i<ac_path.size();i++)\n {\n if(ac_path[i]==1 && i<n)\n {\n if((i==1&&adj[i].size()>=2)||(i!=1 && adj[i].size() >= 3))\n {\n vector<int> d_time = bfs2(i,n,adj,n,time,change,dis[i],dis,ac_path);\n cout<<i<<\" \"<<d_time[n]<<endl;\n b = min(b,d_time[n]);\n }\n }\n }\n\n int a = dis[n];\n\n \n cout<<a<<\" \"<<b<<endl;\n\n \n\n\n {\n if(a/change % 2 ==1)\n {\n a = ((a/change)+1)*change;\n }\n a+=time;\n\n if(a/change % 2 ==1)\n {\n a = ((a/change)+1)*change;\n }\n a+=time;\n \n return min(a,b);\n }\n\n \n }\n};", "memory": "211063" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>>adj(n+1);\n for(int i=0;i<edges.size();i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n vector<int>minTime(n+1,1000000000);\n vector<int>secondMinTime(n+1,1000000000);\n queue<vector<int>>q;\n minTime[1]=0;\n q.push({0,1});\n while(!q.empty()){\n int node=q.front()[1];\n int timeOfInsertion=q.front()[0];\n int wait=0;\n if((timeOfInsertion/change)&1){\n wait=change-(timeOfInsertion%change);\n }\n q.pop();\n for(auto &i:adj[node]){\n int newTime=timeOfInsertion+time+wait;\n if(newTime<secondMinTime[i]){\n if(newTime<minTime[i]){\n minTime[i]=newTime;\n q.push({newTime,i});\n }else if(newTime>minTime[i]){\n secondMinTime[i]=newTime;\n q.push({newTime,i});\n }\n }\n }\n }\n return secondMinTime[n]; \n }\n};", "memory": "212246" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long solve(long long current_time, long long change) {\n long long k = current_time / change;\n if (k % 2 == 1) {\n return current_time + (change - current_time % change);\n }\n return current_time;\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj(n + 1);\n for (auto it : edges) {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;\n pq.push({0, 1});\n vector<long long> dist1(n + 1, LLONG_MAX), dist2(n + 1, LLONG_MAX);\n dist1[1] = 0;\n while (!pq.empty()) {\n long long d = pq.top().first;\n int u = pq.top().second;\n pq.pop();\n\n if (d > dist2[u])\n continue;\n for (auto& v : adj[u]) {\n long long t_time ;\n t_time = time + solve(d, change);\n if (t_time < dist1[v]) {\n dist2[v] = dist1[v];\n dist1[v] = t_time;\n pq.push({dist1[v], v});\n } else if (t_time != dist1[v] && t_time < dist2[v]) {\n dist2[v] = t_time;\n pq.push({dist2[v], v});\n }\n }\n }\n return dist2[n];\n }\n};\n", "memory": "212246" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\n void updateDistance(int &distance, int &time, int &change){\n int rem = distance/change;\n\n if(rem&1) distance = ((rem+1)*change)+time;\n else distance = distance+time;\n }\n\n bool canAdd(int &data, int &distance, vector<pair<int,int>> &p){\n if(distance<p[data].first){\n p[data].second=p[data].first;\n p[data].first = distance;\n return true;\n }\n\n if(distance==p[data].first) return true;\n\n if(distance<p[data].second){\n p[data].second=distance;\n return true;\n }\n\n return false;\n }\n\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> graph(n);\n\n for(vector<int> edge:edges){\n graph[edge[0]-1].push_back(edge[1]-1);\n graph[edge[1]-1].push_back(edge[0]-1);\n }\n\n vector<pair<int,int>> p;\n\n for(int i=0;i<n;i++) p.push_back(make_pair(INT_MAX,INT_MAX));\n\n queue<pair<int,int>> q;\n q.push(make_pair(0,0));\n p[0].first=0;\n\n while(q.size()){\n int node = q.front().first;\n int distance = q.front().second;\n q.pop();\n\n updateDistance(distance, time, change);\n\n for(int data:graph[node]){\n if(canAdd(data, distance, p)) q.push(make_pair(data,distance));\n }\n }\n\n return p[n-1].second;\n }\n};", "memory": "213428" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "#pragma GCC optimize(\"Ofast,unroll-loops\") \nint speed_up = []{\n ios::sync_with_stdio(false);\n cin.tie(NULL);cout.tie(NULL);\n return 0;\n}();\n\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n\n unordered_map<int, vector<int>> graph;\n for (const auto& edge : edges) {\n int u = edge[0], v = edge[1];\n graph[u].push_back(v);\n graph[v].push_back(u);\n }\n\n priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<>> pq;\n pq.push({0, {1, 0}}); \n \n vector<vector<int>> dist(n + 1, vector<int>(2, numeric_limits<int>::max()));\n dist[1][0] = 0;\n\n while (!pq.empty()) {\n auto [current_time, node_path] = pq.top();\n auto [u, path_count] = node_path;\n pq.pop();\n\n if (u == n && path_count == 1) {\n return current_time;\n }\n\n for (int v : graph[u]) {\n int wait_time = 0;\n if ((current_time / change) % 2 == 1) {\n wait_time = change - (current_time % change);\n }\n\n int new_time = current_time + wait_time + time;\n\n if (new_time < dist[v][0]) {\n dist[v][1] = dist[v][0];\n dist[v][0] = new_time;\n pq.push({new_time, {v, 0}});\n } else if (dist[v][0] < new_time && new_time < dist[v][1]) {\n dist[v][1] = new_time;\n pq.push({new_time, {v, 1}});\n }\n }\n }\n\n return -1;\n }\n};", "memory": "213428" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unordered_map<ll, vector<ll>> nbrs;\n\n for(auto &e : edges)\n {\n ll x = e[0], y = e[1];\n\n nbrs[x].push_back(y);\n nbrs[y].push_back(x);\n }\n\n vector<ll> d1(n + 1, INT_MAX);\n vector<ll> d2(n + 1, INT_MAX);\n \n //Dijkastra's, maintain 2 above distance(time required to reach node) vec [d1 and d2] and update min and second min values accordingly.\n priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;\n \n //stores {time_required_to_reach, node}\n pq.push({0, 1});\n d1[1] = 0;\n\n while(!pq.empty())\n {\n auto [time_passed, node] = pq.top();\n pq.pop();\n\n if(node == n and d2[n] != INT_MAX)\n return d2[n];\n\n ll div = time_passed / change;\n\n if(div%2 == 1)\n {\n time_passed = (div + 1) * change;\n }\n\n for(auto &nbr : nbrs[node])\n {\n if(d1[nbr] > time_passed + time)\n {\n d2[nbr] = d1[nbr];\n d1[nbr] = time_passed + time;\n pq.push({time_passed + time, nbr});\n }\n else if (d2[nbr] > time_passed + time and d1[nbr] != time_passed + time)\n {\n d2[nbr] = time_passed + time;\n pq.push({time_passed + time, nbr});\n }\n }\n }\n\n return -1;\n }\n};", "memory": "214611" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> adj[n+1], dist(n+1,-1), vis(n+1,0);\n for(auto it: edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>>pq;\n pq.push({0,1});\n while(!pq.empty()){\n auto [t, node] = pq.top(); pq.pop();\n if(dist[node]==t || vis[node] >=2) continue;\n vis[node]++; dist[node] = t;\n if(node==n && vis[node]==2) return dist[node];\n if((t/change)&1){\n t = (t/change+1)*change;\n }\n for(auto it: adj[node]) pq.push({t+time, it});\n }\n return -1;\n }\n};", "memory": "214611" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<pair<int, int>>> adj(n + 1);\n for (const auto& e : edges) {\n adj[e[0]].emplace_back(e[1], time);\n adj[e[1]].emplace_back(e[0], time);\n }\n return findKthMinimum(n, adj, 1, n, time, change, 2);\n }\n\nprivate:\n #define INF std::numeric_limits<int>::max()\n int findKthMinimum(int n, const vector<vector<pair<int, int>>>& adj, int src, int dst, int time, int change, int k) {\n using T = pair<int, int>;\n priority_queue<T, vector<T>, greater<T>> pq;\n vector<vector<int>> dist(n + 1);\n pq.push({0, src});\n dist[src].push_back(0);\n make_heap(dist[src].begin(),dist[src].end());\n while (!pq.empty()) {\n auto [currentTime, u] = pq.top();\n pq.pop();\n if ((currentTime / change) % 2 == 1) \n currentTime = (currentTime / change + 1) * change;\n\n for (const auto& [v, edgeTime] : adj[u]) {\n int newTime = currentTime + edgeTime;\n if (dist[v].size() < k || newTime < dist[v].back()) {\n if (find(dist[v].begin(), dist[v].end(), newTime) == dist[v].end()) {\n dist[v].push_back(newTime);\n push_heap(dist[v].begin(),dist[v].end());\n pq.push({newTime, v});\n if (dist[v].size() > k) {\n pop_heap(dist[v].begin(),dist[v].end());\n dist[v].pop_back();\n }\n }\n }\n }\n }\n return dist[dst].size() == k ? dist[dst].front() : -1;\n }\n};", "memory": "215793" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<pair<int, int>>> adj(n + 1);\n for (const auto& e : edges) {\n adj[e[0]].emplace_back(e[1], time);\n adj[e[1]].emplace_back(e[0], time);\n }\n return findKthMinimum(n, adj, 1, n, time, change, 2);\n }\n\nprivate:\n #define INF std::numeric_limits<int>::max()\n int findKthMinimum(int n, const vector<vector<pair<int, int>>>& adj, int src, int dst, int time, int change, int k) {\n using T = pair<int, int>;\n priority_queue<T, vector<T>, greater<T>> pq;\n vector<vector<int>> dist(n + 1);\n pq.push({0, src});\n dist[src].push_back(0);\n make_heap(dist[src].begin(),dist[src].end());\n while (!pq.empty()) {\n auto [currentTime, u] = pq.top();\n pq.pop();\n if ((currentTime / change) % 2 == 1) \n currentTime = (currentTime / change + 1) * change;\n\n for (const auto& [v, edgeTime] : adj[u]) {\n int newTime = currentTime + edgeTime;\n if (dist[v].size() < k || newTime < dist[v].front()) {\n if (find(dist[v].begin(), dist[v].end(), newTime) == dist[v].end()) {\n dist[v].push_back(newTime);\n push_heap(dist[v].begin(),dist[v].end());\n pq.push({newTime, v});\n if (dist[v].size() > k) {\n pop_heap(dist[v].begin(),dist[v].end());\n dist[v].pop_back();\n }\n }\n }\n }\n }\n return dist[dst].size() == k ? dist[dst].front() : -1;\n }\n};", "memory": "215793" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\n vector<vector<int>> g;\n int calc(int d,int t, int ch){\n int k = d/ch;\n if(k%2==0){\n return d;\n }else{\n return (k+1)*ch;\n }\n }\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n #define int long long\n g.resize(n+1);\n for(auto& v:edges){\n g[v[0]].push_back(v[1]);\n g[v[1]].push_back(v[0]);\n }\n vector<vector<bool>> visited(n+1,vector<bool>(2,false));\n vector<vector<int>> dist(n+1,vector<int>(2,1e18));\n dist[1][0]=0;\n priority_queue<pair<int,int>> pq;\n pq.push({0,1});\n while(!pq.empty()){\n auto k = pq.top(); pq.pop();\n int curnode = k.second;\n int curdist = -k.first;\n if(visited[curnode][0]){\n if(visited[curnode][1]) continue;\n }\n if(!visited[curnode][0]) visited[curnode][0]=true;\n else visited[curnode][1]=true;\n for(auto v:g[curnode]){\n int newdist = calc(curdist,time,change);\n if(dist[v][0]==newdist+time) continue;\n if(dist[v][0]>newdist+time){\n dist[v][0]=newdist+time;\n pq.push({-dist[v][0],v});\n continue;\n }\n if(dist[v][1]>newdist+time){\n dist[v][1]=newdist+time;\n pq.push({-dist[v][1],v});\n continue;\n }\n }\n }\n #undef int\n // for(auto i:dist){\n // cout<<i[0]<<\" \"<<i[1]<<endl;\n // }\n return dist[n][1];\n }\n};", "memory": "216976" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n // Use dijstrka to get top 2 shortest paths [1,n]\n vector<list<int>> graph(n+1);\n for (auto& e :edges) {\n graph[e[0]].push_back(e[1]);\n graph[e[1]].push_back(e[0]);\n }\n vector<int> best(n + 1, numeric_limits<int>::max()),\n best2(n + 1, numeric_limits<int>::max()),\n freq(n + 1);\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;\n q.push({0,1});\n best[1] = 0;\n while (!q.empty()) {\n auto [dist, start] = q.top();\n q.pop();\n\n ++freq[start];\n if (freq[start] == 2 && start == n) break;\n\n // is red?\n int multiple = dist/change;\n if (multiple % 2 == 1) {\n // wait until green\n dist = (multiple+1)*change;\n }\n dist += time;\n\n for (int end : graph[start]) {\n if (freq[end] == 2) continue;\n if (dist < best[end]) {\n best2[end] = best[end];\n best[end] = dist;\n q.push({dist, end});\n } else if (dist < best2[end] && dist != best[end]) {\n best2[end] = dist;\n q.push({dist, end});\n }\n }\n }\n\n return best2[n];\n }\n};", "memory": "216976" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj(n+1);\n for(vector<int> &e: edges){\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n int t = 0;\n vector<int> arr_cnt(n+1, 0);\n queue<int> q;\n q.push(1);\n while(!q.empty()){\n int sz = q.size();\n vector<bool> inq(n+1, false);\n while(sz--){\n int node = q.front();\n q.pop();\n arr_cnt[node]++;\n if(arr_cnt[node] == 2 && node == n){\n return t;\n }\n for(int &a: adj[node]){\n if(!inq[a] && arr_cnt[a] < 2){\n q.push(a);\n inq[a] = true;\n // cout << \"push \" << a << \"\\n\";\n }\n }\n }\n if(((t/change)&1)) t = (t/change+1)*change;\n t += time;\n // cout << t << \"\\n\";\n }\n return -1;\n }\n};", "memory": "218158" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int bfs2nd(int n, const vector<vector<int>>& adj) {\n unordered_map<int, int> seen1;\n unordered_set<int> seen2;\n\n deque<pair<int, int>> q;\n q.emplace_back(0, 0);\n while (!q.empty()) {\n auto [curr, time] = q.front();\n q.pop_front();\n\n if (seen2.contains(curr)) {\n continue;\n } else if (seen1.contains(curr)) {\n if (time > seen1[curr]) {\n if (curr == n - 1) {\n return time;\n } else {\n seen2.emplace(curr);\n }\n } else {\n continue;\n }\n } else {\n seen1[curr] = time;\n }\n\n for (auto v : adj[curr]) {\n q.emplace_back(v, time + 1);\n }\n }\n\n return -1;\n }\n\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj(n);\n\n for (auto& e : edges) {\n adj[e[0] - 1].emplace_back(e[1] - 1);\n adj[e[1] - 1].emplace_back(e[0] - 1); \n } \n\n int dist = bfs2nd(n, adj);\n\n int totTime = 0;\n for (auto i = 0; i < dist; i++) {\n if ((totTime / change) % 2 == 1) {\n totTime += change - (totTime % change);\n }\n\n totTime += time;\n }\n\n return totTime;\n }\n};", "memory": "218158" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> dist1(n, INT_MAX), dist2(n, INT_MAX);\n dist1[0] = 0;\n // dist2[0] = 0;\n\n vector<vector<int>> graph(n);\n for(auto edge : edges){\n graph[edge[0]-1].push_back(edge[1]-1);\n graph[edge[1]-1].push_back(edge[0]-1);\n }\n\n unordered_map<int,int> freq;\n\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n pq.push({dist1[0], 0});\n\n while(!pq.empty()){\n auto item = pq.top();\n pq.pop();\n\n int timeTaken = item.first;\n int node = item.second;\n freq[node]++;\n\n // if(node == n-1 && dist2[node] != INT_MAX) {\n // for(int n : dist2){\n // cout << n << \" \";\n // }\n // return dist2[node];\n // }\n\n if(node == n-1 && freq[node] == 2) return timeTaken;\n\n int nextTime = timeTaken;\n if((nextTime/change)%2 == 1){\n nextTime = (nextTime/change + 1)*change + time;\n }\n else{\n nextTime+=time;\n }\n\n for(auto nei : graph[node]){\n if(freq[nei] == 2) continue;\n \n // cout << node << \" to \" << nei << \" can be \" << nextTime+3 << endl;\n\n if(dist1[nei] > nextTime){\n dist2[nei] = dist1[nei];\n dist1[nei] = nextTime;\n pq.push({dist1[nei], nei});\n }\n else if(dist2[nei] > nextTime && dist1[nei] != nextTime){\n dist2[nei] = nextTime;\n pq.push({dist2[nei], nei});\n }\n }\n }\n\n for(int n : dist2){\n cout << n << \" \";\n }\n\n return -1;\n }\n};", "memory": "219341" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> dist1(n, INT_MAX), dist2(n, INT_MAX);\n dist1[0] = 0;\n // dist2[0] = 0;\n\n vector<vector<int>> graph(n);\n for(auto edge : edges){\n graph[edge[0]-1].push_back(edge[1]-1);\n graph[edge[1]-1].push_back(edge[0]-1);\n }\n\n unordered_map<int,int> freq;\n\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n pq.push({dist1[0], 0});\n\n while(!pq.empty()){\n auto item = pq.top();\n pq.pop();\n\n int timeTaken = item.first;\n int node = item.second;\n freq[node]++;\n\n // if(node == n-1 && dist2[node] != INT_MAX) {\n // for(int n : dist2){\n // cout << n << \" \";\n // }\n // return dist2[node];\n // }\n\n if(node == n-1 && freq[node] == 2) return timeTaken;\n\n int nextTime = timeTaken;\n if((nextTime/change)%2 == 1){\n nextTime = (nextTime/change + 1)*change + time;\n }\n else{\n nextTime+=time;\n }\n\n for(auto nei : graph[node]){\n if(freq[nei] == 2) continue;\n \n // cout << node << \" to \" << nei << \" can be \" << nextTime+3 << endl;\n\n if(dist1[nei] > nextTime){\n dist2[nei] = dist1[nei];\n dist1[nei] = nextTime;\n pq.push({dist1[nei], nei});\n }\n else if(dist2[nei] > nextTime && dist1[nei] != nextTime){\n dist2[nei] = nextTime;\n pq.push({dist2[nei], nei});\n }\n }\n }\n\n // for(int n : dist2){\n // cout << n << \" \";\n // }\n\n return -1;\n }\n};", "memory": "219341" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n typedef pair<int,int> P;\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n priority_queue<P,vector<P>,greater<P>>pq;\n\n unordered_map<int,vector<int>>adj;\n\n for(auto edge:edges){\n int u=edge[0];\n int v=edge[1];\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n vector<int>d1(n+1,INT_MAX);\n vector<int>d2(n+2,INT_MAX);\n d1[1]=0;\n pq.push({0,1});\n\n while(!pq.empty()){\n int timePassed=pq.top().first;\n int node=pq.top().second;\n pq.pop();\n if(d2[node]!=INT_MAX && node==n){\n return d2[node];\n }\n int div=timePassed/change;\n if(div%2==1){\n timePassed = change*(div+1);\n }\n for(auto v:adj[node]){\n if(d1[v]>timePassed+time){\n d2[v]=d1[v];\n d1[v]=timePassed+time;\n pq.push({timePassed+time,v});\n }else if(d2[v]>timePassed+time && d1[v]!=timePassed+time){\n d2[v]=timePassed+time;\n pq.push({timePassed+time,v});\n }\n }\n }\n return -1;\n }\n};", "memory": "220523" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unordered_map<int,vector<int>>adj;\n for(auto it : edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<int>d1(n+1,INT_MAX),d2(n+1,INT_MAX);\n priority_queue<pair<int, int>, vector<pair<int, int>>,\n greater<pair<int, int>>> pq;\n pq.push({0,1});\n d1[1] = 0;\n while(!pq.empty()){\n int curr_time = pq.top().first;\n int node = pq.top().second;\n pq.pop();\n if(node == n && d2[n] != INT_MAX)return d2[n];\n if((curr_time/change)&1) curr_time = ((curr_time)/change + 1) * change;\n for(auto it : adj[node]){\n if(d1[it] > curr_time + time){\n d2[it] = d1[it];\n d1[it] = curr_time + time;\n pq.push({curr_time + time, it});\n }\n else if(d2[it] > curr_time + time && d1[it] != curr_time + time){\n d2[it] = curr_time + time;\n pq.push({curr_time + time, it});\n }\n }\n }\n return -1;\n }\n};", "memory": "220523" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "#define pi pair<int,int>\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n // main Point to note in this questions are:\n /*\n 1) The secondmin is found when the node is visited at most 2 times,\n with different path values.\n */\n unordered_map<int, vector<int>> adj;\n for(auto E: edges){\n adj[E[0]].push_back(E[1]);\n adj[E[1]].push_back(E[0]);\n }\n vector<int> dist1(n+1, INT_MAX), dist2(n+1, INT_MAX);\n priority_queue<pi, vector<pi>, greater<pi>> pq;\n vector<int> visitTimes(n+1, 0);\n pq.push({0, 1});\n dist1[1] = 0;\n while(!pq.empty()){\n auto top = pq.top();\n pq.pop();\n int node = top.second;\n int timetaken = top.first;\n visitTimes[node]++;\n // cout << node << ' ' << timetaken << endl;\n if(node == n && visitTimes[n] == 2){\n return timetaken;\n }\n\n if((timetaken/change)%2){\n timetaken += (change - (timetaken%change)); \n }\n timetaken += time;\n\n for(auto neigh: adj[node]){\n if(visitTimes[neigh] == 2) continue;\n if(dist1[neigh] > timetaken){\n dist2[neigh] = dist1[neigh];\n dist1[neigh] = timetaken;\n pq.push({timetaken, neigh});\n }\n else if(dist2[neigh] > timetaken && timetaken != dist1[neigh]){\n dist2[neigh] = timetaken;\n pq.push({timetaken, neigh});\n }\n }\n }\n\n return 0;\n }\n};", "memory": "221706" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> dist(n+1,INT_MAX);\n unordered_map<int,vector<int>> adj;\n for(auto it:edges){adj[it[0]].push_back(it[1]);adj[it[1]].push_back(it[0]);}\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n vector<int> firstMin(n+1,INT_MAX),secondMin(n+1,INT_MAX);\n pq.push({0,1});\n firstMin[1]=0;\n while(!pq.empty())\n {\n int node=pq.top().second;\n int distance=pq.top().first;\n pq.pop();\n if(distance>secondMin[node])continue;\n int newTime=distance+time;\n int temp=distance/change;\n if(temp%2==1)\n {\n newTime=(temp+1)*change+time;\n }\n for(auto it:adj[node])\n {\n \n if(firstMin[it]>newTime)\n {\n secondMin[it]=firstMin[it];\n firstMin[it]=newTime;\n pq.push({newTime,it});\n }\n else if(firstMin[it]==newTime)continue;\n else if(secondMin[it]>newTime)\n {\n secondMin[it]=newTime;\n pq.push({newTime,it});\n }\n }\n }\n return secondMin[n];\n }\n};", "memory": "221706" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> adj[n+1];\n for(int i=0;i<edges.size();i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n\n vector<pair<int,int>> vis(n+1,{INT_MAX,INT_MAX});\n \n queue<vector<int>> q;\n q.push({1,0});\n \n\n vector<int> ans;\n while(!q.empty()){\n int node=q.front()[0];\n // int par = q.front()[1];\n int t=q.front()[1];\n q.pop();\n\n for(int i=0;i<adj[node].size();i++ ){\n int comp1=vis[adj[node][i]].first;\n int comp2= vis[adj[node][i]].second;\n if(t%(2*change) >=0 && t%(2*change)<change){\n int newTime = t+time;\n if(newTime < comp1 || newTime < comp2 ||comp1==comp2){\n if(newTime < comp1 ){\n vis[adj[node][i]].second=comp1;\n vis[adj[node][i]].first= newTime;\n }\n else {\n vis[adj[node][i]].second=newTime;\n }\n q.push({adj[node][i],newTime});\n }\n }\n else{\n int newTime = t+2*change-t%(2*change) +time;\n if(newTime < comp1 || newTime < comp2 || comp1 == comp2){\n if(newTime < comp1){\n vis[adj[node][i]].second=comp1;\n vis[adj[node][i]].first= newTime;\n }\n else {\n vis[adj[node][i]].second=newTime;\n }\n q.push({adj[node][i],newTime});\n }\n }\n }\n\n }\n cout<<vis[n].second<<\" \"<<vis[n].first;\n return vis[n].second;\n\n\n }\n};", "memory": "222888" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> adj[n+1];\n for(int i=0;i<edges.size();i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n\n vector<pair<int,int>> vis(n+1,{INT_MAX,INT_MAX});\n \n queue<vector<int>> q;\n q.push({1,0});\n \n\n vector<int> ans;\n while(!q.empty()){\n int node=q.front()[0];\n // int par = q.front()[1];\n int t=q.front()[1];\n q.pop();\n\n for(int i=0;i<adj[node].size();i++ ){\n int comp1=vis[adj[node][i]].first;\n int comp2= vis[adj[node][i]].second;\n if(t%(2*change) >=0 && t%(2*change)<change){\n int newTime = t+time;\n if(newTime < comp1 || newTime < comp2 ||comp1==comp2){\n if(newTime < comp1 ){\n vis[adj[node][i]].second=comp1;\n vis[adj[node][i]].first= newTime;\n }\n else {\n vis[adj[node][i]].second=newTime;\n }\n q.push({adj[node][i],newTime});\n }\n }\n else{\n int newTime = t+2*change-t%(2*change) +time;\n if(newTime < comp1 || newTime < comp2 || comp1 == comp2){\n if(newTime < comp1){\n vis[adj[node][i]].second=comp1;\n vis[adj[node][i]].first= newTime;\n }\n else {\n vis[adj[node][i]].second=newTime;\n }\n q.push({adj[node][i],newTime});\n }\n }\n }\n\n }\n // cout<<vis[n].second<<\" \"<<vis[n].first;\n return vis[n].second;\n\n\n }\n};", "memory": "222888" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "#define fst first\n#define snd second\ntypedef pair<int,int> ii;\n\nclass Solution {\n vector<vector<int>> adj;\npublic:\n Solution() : adj(1e4 + 5) {};\n void createAdjList(vector<vector<int>>& edges) {\n for (vector<int> e : edges) {\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n createAdjList(edges);\n vector<int> dist1(n + 1, 1e9);\n vector<int> dist2(n + 1, 1e9);\n vector<int> fre(n + 1, 0);\n queue<ii> q;\n q.push({0, 1});\n dist1[1] = 0;\n while (q.size()) {\n int u = q.front().snd;\n int du = q.front().fst;\n q.pop();\n fre[u]++;\n // if (du != dist1[u]) { // du is 2-nd min dist\n // if (u == n) return du;\n // if (du > dist2[u]) continue;\n // }\n // int du = fre[u] == 1 ? dist1[u] : dist2[u];\n if (fre[u] == 2) {\n if (u == n) return du;\n }\n if (fre[u] > 2) continue;\n for (int v : adj[u]) {\n int cost = time;\n if (du / change % 2) { // is red\n cost += change - du % change;\n }\n if (dist1[v] > du + cost) {\n dist1[v] = du + cost;\n q.push({dist1[v], v});\n } else if (dist2[v] == 1e9 && dist1[v] != du + cost) {\n dist2[v] = du + cost;\n q.push({dist2[v], v});\n }\n }\n }\n return -1;\n }\n};", "memory": "224071" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "#define fst first\n#define snd second\ntypedef pair<int,int> ii;\n\nclass Solution {\n vector<vector<int>> adj;\npublic:\n Solution() : adj(1e4 + 5) {};\n void createAdjList(vector<vector<int>>& edges) {\n for (vector<int> e : edges) {\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n createAdjList(edges);\n vector<int> dist1(n + 1, 1e9);\n vector<int> dist2(n + 1, 1e9);\n vector<int> fre(n + 1, 0);\n queue<ii> q;\n q.push({0, 1});\n dist1[1] = 0;\n while (q.size()) {\n int u = q.front().snd;\n int du = q.front().fst;\n q.pop();\n if (du != dist1[u]) { \n if (u == n) return du;\n }\n for (int v : adj[u]) {\n int cost = time;\n if (du / change % 2) { // is red\n cost += change - du % change;\n }\n if (dist1[v] > du + cost) {\n dist1[v] = du + cost;\n q.push({dist1[v], v});\n } else if (dist1[v] < du + cost && dist2[v] == 1e9) {\n dist2[v] = du + cost;\n q.push({dist2[v], v});\n }\n }\n }\n return -1;\n }\n};", "memory": "224071" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n map<int,vector<int>>adj;\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n } \n vector<int> v(n+1,0),d1(n+1,INT_MAX),d2(n+1,INT_MAX); \n queue<pair<int,int>> q;\n q.push({1,0});\n d1[1]=0;\n while(!q.empty()){\n auto temp=q.front();\n q.pop();\n int no=temp.first,dis=temp.second;\n for(auto it:adj[no]){\n if(dis+1<d1[it]){\n d2[it]=d1[it];\n d1[it]=dis+1;\n q.push({it,dis+1});\n }\n else if(dis+1>d1[it] && dis+1<d2[it]){\n d2[it]=dis+1;\n q.push({it,dis+1});\n }\n }\n }\n int ret=0;\n for(int i=0;i<d2[n];i++){\n int cur=ret/change;\n if(cur%2){\n ret+=change-(ret%change);\n }\n ret+=time;\n }\n return ret;\n }\n};", "memory": "225253" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n int get_time(int steps, int change, int time)\n {\n int elapsed = 0;\n while (steps > 0)\n {\n int switch_step = elapsed / change;\n if (elapsed >= switch_step * change && (switch_step & 1))\n elapsed += ((switch_step + 1) * change - elapsed);\n\n elapsed += time;\n --steps;\n }\n\n return elapsed;\n }\n\n int bfs(int n, const vector<vector<int> >& graph, int time, int change)\n {\n constexpr int MAX_DIST = 2 * 1e4;\n constexpr int max_size = 2;\n std::vector<std::set<int> > pq(n + 1);\n\n std::queue<std::pair<int, int> > q;\n q.emplace(1, 0);\n pq[1].insert(0);\n while (!q.empty())\n {\n auto [v, d] = q.front();\n q.pop();\n\n for (const auto& to: graph[v])\n {\n int next_d = d + 1;\n\n if (pq[to].find(next_d) == pq[to].end()) {\n if (pq[to].size() < max_size) {\n pq[to].insert(next_d);\n q.emplace(to, next_d);\n }\n else {\n int max = *pq[to].rbegin();\n if (next_d < max) {\n pq[to].insert(next_d);\n q.emplace(to, next_d);\n pq[to].erase(max);\n }\n }\n }\n }\n }\n\n int steps = *pq[n].rbegin();\n\n return get_time(steps, change, time);\n }\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n std::vector<std::vector<int> > graph(n + 1);\n for (const auto& e: edges)\n {\n graph[e[0]].push_back(e[1]);\n graph[e[1]].push_back(e[0]);\n }\n\n return bfs(n, graph, time, change);\n }\n};", "memory": "225253" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj(n);\n for(auto v: edges){\n v[0]--;v[1]--;\n adj[v[0]].push_back(v[1]);\n adj[v[1]].push_back(v[0]);\n }\n vector<int> dist(n, -1);\n vector<int> vis(n, 0);\n\n set<int> curr; \n curr.insert(0);\n\n dist[0]=0;\n\n int ans=-1;\n while(ans==-1){\n set<int> next;\n for(int t: curr){\n for(int a: adj[t]){\n if(dist[a]<0){\n dist[a]=1+dist[t];\n }\n if(!vis[a])next.insert(a);\n }\n if(t==n-1){\n ans= dist[t];\n }\n }\n for(int t: curr)vis[t]=1;\n\n if(ans!=-1){\n if(next.find(n-1)!=next.end()){\n ans+=1;\n }\n else{\n ans+=2;\n }\n }\n swap(curr, next);\n\n }\n // for(int i=0;i<n;i++){\n // cout<<dist[i]<<\" \";\n // }\n // cout<<endl;\n // cout<<dist[n-1]<<endl;\n // cout<<ans<<endl;\n int t=0;\n // change*=2;\n for(int i=0;i<ans;i++){\n t+= time;\n if((t/change)%2==1 && i!=ans-1){//red\n t+= change - (t%change);\n }\n }\n\n\n\n\n return t;\n }\n};", "memory": "226436" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "#define MAX 10000000\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adjList(n, vector<int>());\n for(auto edge : edges){\n adjList[edge[0]-1].push_back(edge[1]-1);\n adjList[edge[1]-1].push_back(edge[0]-1);\n }\n pair<int, int> dist = dikstra(0, adjList);\n vector<int> temp;\n int secmin = MAX;\n if(dist.first != MAX){\n temp.push_back(getTime(dist.first, time, change));\n }else{\n temp.push_back(MAX);\n }\n \n temp.push_back(getTime(dist.second, time, change));\n temp.push_back(getTime(dist.second+2, time, change));\n sort(temp.begin(), temp.end());\n return temp[1];\n }\n\n int getTime(int dist, int time, int change){ \n int t = 0;\n for(int i = 0; i < dist; ++i){ \n t += time;\n if((t/change)%2 && i < dist-1){\n //red\n t = ((t/change)+1)*change;\n }\n }\n // cout<<dist<<\",\"<<t<<endl;;\n return t;\n }\n\n pair<int,int> dikstra(int source, vector<vector<int>>& adjList){\n int wgt = 1;\n int n = adjList.size();\n vector<int> distance(n, MAX), secdist(n, MAX);\n distance[source] = 0;\n secdist[source] = 0;\n vector<int> nbr;\n queue<pair<int, int>> minheap;\n minheap.push(make_pair(distance[source], source));\n enum visit_type {NOT_VISITED, FIRST_VISIT, SEC_VISIT};\n vector<visit_type> visited(n, NOT_VISITED);\n pair<int, int> temp;\n int node, nh;\n while(!minheap.empty()){\n temp = minheap.front();\n minheap.pop();\n node = temp.second;\n // cout<<node<<\":\"<<temp.first<<endl;\n if(visited[node] == NOT_VISITED){\n visited[node] = FIRST_VISIT;\n nbr = adjList[node];\n for(int i = 0; i < nbr.size(); ++i){\n nh = nbr[i];\n distance[nh] = min(distance[node] + wgt, distance[nh]);\n minheap.push(make_pair(temp.first + wgt, nh));\n }\n }else if(visited[node] == FIRST_VISIT && temp.first > distance[node]){\n visited[node] = SEC_VISIT;\n secdist[node] = min(temp.first, secdist[node]);\n nbr = adjList[node];\n for(int i = 0; i < nbr.size(); ++i){\n nh = nbr[i];\n distance[nh] = min(distance[node] + wgt, distance[nh]);\n minheap.push(make_pair(temp.first + wgt, nh));\n }\n }\n }\n return make_pair(secdist[n-1], distance[n-1]);\n }\n};", "memory": "226436" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int calculateSecondMin (vector<vector<int>>dist, vector<int>V[], int time , int change, int n){\n queue<pair<int,int>>q ;\n dist[1][0] = 0;\n q.push({1,0});\n while(!q.empty()){\n auto it = q.front();\n q.pop();\n int node = it.first;\n int curTime = it.second;\n if (node == n){\n if (dist[node][1] != 1e9){\n break;\n }\n }\n if ((curTime/change) % 2 != 0 ){\n int waitTime = change - (curTime%change);\n curTime = waitTime+curTime+time;\n }\n else{\n curTime = curTime + time;\n }\n for (auto it : V[node]){\n if (dist[it][0] == 1e9){\n dist[it][0] = curTime;\n q.push({it,curTime});\n\n }\n else if (dist[it][1] == 1e9 && curTime > dist[it][0]){\n dist[it][1] = curTime;\n q.push({it,curTime});\n }\n\n }\n }\n return dist[n][1];\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>>dist(n+1, vector<int>(2,1e9));\n vector<int>V[n+1];\n for (auto it :edges){\n int u = it[0];\n int v = it[1];\n V[u].push_back(v);\n V[v].push_back(u); \n }\n int bfs = calculateSecondMin (dist, V, time , change, n);\n return bfs;\n \n }\n};", "memory": "227618" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "#include<queue>\n#include<unordered_map>\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unordered_map<int,vector<int>> graph;\n for(int i=0;i<edges.size();i++){\n int u = edges[i][0];\n int v = edges[i][1];\n graph[u].push_back(v);\n graph[v].push_back(u);\n }\n vector<vector<int>> visited(n+1,vector<int> (2,INT_MAX));\n priority_queue<pair<int,int> ,vector<pair<int,int>> ,greater<pair<int,int>>> minheap;\n int count = 0;\n visited[1][0] = 0;\n minheap.push(make_pair(0,1));\n while(!minheap.empty()){\n int pt = minheap.top().first;\n int pn = minheap.top().second;\n minheap.pop();\n int wt = 0;\n if((pt/change)%2!=0){\n wt = (pt/change+1)*change-pt;\n }\n int nt = pt+wt+time;\n vector<int> l = graph[pn];\n for(int i=0;i<l.size();i++){\n if(nt<visited[l[i]][0]){\n visited[l[i]][1] = visited[l[i]][0];\n visited[l[i]][0] = nt;\n minheap.push(make_pair(nt,l[i]));\n }\n else if(visited[l[i]][0] < nt && visited[l[i]][1] > nt){\n visited[l[i]][1] = nt;\n minheap.push(make_pair(nt,l[i]));\n }\n }\n if (pn == n && visited[pn][1] != INT_MAX) {\n return visited[pn][1];\n }\n }\n return 0;\n }\n};", "memory": "227618" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "#define pii pair<int, int>\n\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj(n+5);\n for (auto e : edges) {\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n vector<priority_queue<int>> times(n+5);\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n times[1].push(0);\n pq.push({0, 1});\n while (!pq.empty()) {\n pii t = pq.top();\n pq.pop();\n int elapsed = t.first, cur = t.second;\n bool is_green = !((elapsed / change) & 1);\n // cout << cur << ' ' << elapsed << ' ' << is_green << '\\n';\n if (!is_green) {\n elapsed += (change - elapsed % change);\n }\n for (auto to : adj[cur]) {\n if (times[to].size() == 2) {\n if (times[to].top() <= elapsed + time) continue;\n times[to].pop();\n times[to].push(elapsed + time);\n pq.push({elapsed + time, to});\n } else {\n if (!times[to].empty() && times[to].top() == elapsed + time) continue;\n times[to].push(elapsed + time);\n pq.push({elapsed + time, to});\n }\n }\n }\n\n // int x = -1;\n // while (!times[n].empty()) {\n // x = times[n].top();\n // times[n].pop();\n // cout << x << '\\n';\n // }\n return times[n].top();\n }\n};", "memory": "228801" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj(n+1);\n for (auto it : edges) {\n adj[it[0] - 1].push_back(it[1] - 1);\n adj[it[1] - 1].push_back(it[0] - 1);\n }\n vector<priority_queue<int>> dist(n);\n for (int i = 0; i < n; i++) {\n dist[i].push(1e8);\n }\n priority_queue<pair<int, int>, vector<pair<int, int>>,\n greater<pair<int, int>>>\n pq;\n pq.push({0, 0});\n dist[0].pop();\n dist[0].push(0);\n while (!pq.empty()) {\n int dis = pq.top().first;\n int cur = pq.top().second;\n pq.pop();\n int k = dis / change;\n if ((k % 2) == 1) {\n dis += (change - (dis% change));\n }\n for (auto it : adj[cur]) {\n int distance = dis + time;\n if (dist[it].top() == 1e8) {\n dist[it].pop();\n dist[it].push(distance);\n pq.push({distance, it});\n } else if (dist[it].size() < 2 && dist[it].top() != distance) {\n dist[it].push(distance);\n pq.push({distance, it});\n } else {\n if (dist[it].top() > distance) {\n dist[it].pop();\n dist[it].push(distance);\n pq.push({distance, it});\n }\n }\n }\n }\n return dist[n-1].top();\n }\n};", "memory": "228801" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> dist(n+1,vector<int>(2,1e9));\n vector<int> g[n+1];\n for(int i=0; i<edges.size(); i++){\n int u = edges[i][0], v = edges[i][1];\n g[u].push_back(v);\n g[v].push_back(u);\n }\n dist[1][0] = 0;\n set<array<int,3>> q;\n q.insert({0,0,1});\n while(!q.empty()){\n auto [dis, num, node] = *q.begin();\n q.erase(q.begin());\n for(auto child:g[node]){\n if(dist[child][num]>dis+time){\n dist[child][num] = dis+time;\n q.insert({dist[child][num],num,child});\n }\n if(num==0){\n if(dist[child][0]<dis+time && dist[child][1]>dis+time){\n dist[child][1] = dis+time;\n q.insert({dist[child][1],1,child});\n }\n if(dist[child][1]>(dis+3*time)){\n dist[child][1] = dis+3*time;\n q.insert({dist[child][1],1,child});\n }\n }\n }\n }\n int ans=0;\n for(int i=1; i<=n; i++) cout<<dist[i][0]<<\" \"<<dist[i][1]<<endl;\n for(int i=0; i<(dist[n][1]/time); i++){\n if(ans!=0 && ans%(change*2)>=change) ans = (ans + ((change*2) - (ans%(change*2)))%(change*2));\n ans = ans + time;\n }\n return ans;\n }\n};", "memory": "229983" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unordered_map<int, list<int>> adjList;\n for (auto& e : edges) {\n int u = e[0], v = e[1];\n adjList[u].push_back(v);\n adjList[v].push_back(u);\n }\n priority_queue<pair<int,int>,vector<pair<int, int>>, greater<pair<int,int>>>pq;\n pq.push({0, 1}); // {current time, node}\n\n // Shortest times to reach each node (first and second shortest)\n vector<int> firstMinTime(n + 1, INT_MAX);\n vector<int> secondMinTime(n + 1, INT_MAX);\n firstMinTime[1] = 0;\n\n while (!pq.empty()) {\n auto [currTime, node] = pq.top();\n pq.pop();\n\n if ((currTime / change) % 2 == 1) { // If red signal\n currTime += (change - (currTime % change)); // Wait for green signal\n }\n\n for (auto& neighbor : adjList[node]) {\n int newTime = currTime + time;\n\n // If this is the first time we reach the neighbor\n if (newTime < firstMinTime[neighbor]) {\n secondMinTime[neighbor] = firstMinTime[neighbor];\n firstMinTime[neighbor] = newTime;\n pq.push({newTime, neighbor});\n }\n // If this is the second time we reach the neighbor and it's\n // better than the current second best\n else if (newTime > firstMinTime[neighbor] &&\n newTime < secondMinTime[neighbor]) {\n secondMinTime[neighbor] = newTime;\n pq.push({newTime, neighbor});\n }\n }\n }\n\n return secondMinTime[n];\n }\n};", "memory": "229983" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unordered_map<int, vector<int>> graph;\n for (auto &edge : edges) {\n graph[edge[0]].push_back(edge[1]);\n graph[edge[1]].push_back(edge[0]);\n }\n unordered_map<int, int> visited_times;\n unordered_set<int> q = {1}; // start from the vertex 1 at time = 0;\n int min_time = -1, cur_time = 0;\n while (!q.empty()) {\n unordered_set<int> next_q;\n int in_time = cur_time + time;\n int out_time = in_time;\n if ((out_time/change)%2 == 1) out_time += change - out_time%change;\n \n for (auto u : q) {\n auto& next_edges = graph[u];\n for (auto v : next_edges) {\n if (v == n) {\n if (min_time < 0) {\n min_time = in_time;\n } else if (min_time < in_time) {\n return in_time;\n }\n }\n if (next_q.count(v) || visited_times[v] >= 2) continue;\n visited_times[v] += 1;\n next_q.insert(v);\n }\n }\n cur_time = out_time;\n next_q.swap(q);\n }\n return -1;\n }\n};", "memory": "231166" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> dis(n+1,-1),vis(n+1,0);\n vector<vector<int>> adj(n+1);\n for(int i=0;i<edges.size();i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n set<pair<int,int>> st;\n st.insert({0,1});\n while(!st.empty()){\n int t = st.begin()->first;\n int node = st.begin()->second;\n st.erase(st.begin());\n if(dis[node]==t || vis[node]>=2)continue;\n vis[node]++;\n dis[node]=t;\n for(auto i :adj[node]){\n if((t/change)%2!=0){\n t = ((t/change)+1)*change;\n }\n st.insert({t+time,i});\n }\n }\n return dis[n];\n }\n};", "memory": "231166" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n #define p pair<int, int>\n\n unordered_map<int,vector<int>> buildGraph(vector<vector<int>>& nums){\n unordered_map<int,vector<int>> graph;\n for(vector<int> num:nums){\n int from=num[0];\n int to=num[1];\n graph[from].push_back(to);\n graph[to].push_back(from);\n }\n return graph;\n }\n\n int solve(int& n,unordered_map<int,vector<int>>& graph,int& change,int& t){\n vector<vector<int>> v(n+1,vector<int>(2,INT_MAX));\n // vector<int> v2(n+1,INT_MAX);\n priority_queue<p,vector<p>,greater<p>> pq;\n pq.emplace(0,1);\n v[1][0]=0;\n while(!pq.empty()){\n int time=pq.top().first;\n int node=pq.top().second;\n pq.pop();\n if(node==n && v[n][1]!=INT_MAX){\n return v[n][1];\n }\n int pos=time/change;\n if(pos%2==1){\n time = (pos+1)*change;\n }\n for(int i:graph[node]){\n if(v[i][0]>time+t){\n v[i][1]=v[i][0];\n v[i][0]=time+t;\n pq.emplace(time+t,i);\n }else if(v[i][1]>time+t && v[i][0]!=time+t){\n v[i][1]=time+t;\n pq.emplace(time+t,i);\n }\n \n }\n }\n return -1;\n }\n\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unordered_map<int,vector<int>> graph = buildGraph(edges);\n return solve(n,graph,change,time);\n }\n};\n", "memory": "232348" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n #define p pair<int, int>\n\n unordered_map<int,vector<int>> buildGraph(vector<vector<int>>& nums){\n unordered_map<int,vector<int>> graph;\n for(vector<int> num:nums){\n int from=num[0];\n int to=num[1];\n graph[from].push_back(to);\n graph[to].push_back(from);\n }\n return graph;\n }\n\n int solve(int& n,unordered_map<int,vector<int>>& graph,int& change,int& t){\n vector<vector<int>> v(n+1,vector<int>(2,INT_MAX));\n // vector<int> v2(n+1,INT_MAX);\n priority_queue<p,vector<p>,greater<p>> pq;\n pq.emplace(0,1);\n v[1][0]=0;\n while(!pq.empty()){\n int time=pq.top().first;\n int node=pq.top().second;\n pq.pop();\n if(node==n && v[n][1]!=INT_MAX){\n return v[n][1];\n }\n int pos=time/change;\n if(pos%2==1){\n time = (pos+1)*change;\n }\n for(int i:graph[node]){\n if(v[i][0]>time+t){\n v[i][1]=v[i][0];\n v[i][0]=time+t;\n pq.emplace(time+t,i);\n }else if(v[i][1]>time+t && v[i][0]!=time+t){\n v[i][1]=time+t;\n pq.emplace(time+t,i);\n }\n \n }\n }\n return -1;\n }\n\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unordered_map<int,vector<int>> graph = buildGraph(edges);\n return solve(n,graph,change,time);\n }\n};\n", "memory": "232348" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n unordered_map<int,vector<int>> adj;\n for(auto& x:edges)\n {\n adj[x[0]].push_back(x[1]);\n adj[x[1]].push_back(x[0]);\n }\n //you need second min time to reach the destination. Data required: min time to reach a node and also the second minimum time to reach a node. So Run a BFS from 1 and keep track of how many times you have visited a node. If a node is visited 2 times already, then it wont be pushed into the queue a third time. \n //If you reach a nth node for the second time. YOu have got, the second min time to reach nth node.\n //Also when you reach a node, timePassed will depend on whether you have raeched the node 1st time or 2nd time. \n //Now we need to decide whether it is a green lgiht/red light at timepassed. We know , 0-change = green, change -change*2 = red, green,red and so on.. So if timePaseed /change gives even number => green light. Else red light. For red light: timeTOWait = (division +1 ) * change\n queue<vector<int>> q;\n vector<int> minTime(n+1,INT_MAX);\n vector<int> secondMinTime(n+1,INT_MAX);\n minTime[1] = 0;//becoz we start at 1\n q.push({1,1});//{node, no. of times it is visited}\n while(!q.empty())\n {\n vector<int> curr= q.front();\n q.pop();\n int cur = curr[0];\n int visits = curr[1];\n //how much time has passed for this node ?\n int timePassed = visits==1 ? minTime[cur] : secondMinTime[cur];\n\n if(cur==n && visits==2)\n {\n //nth node has been visited 2 times\n return timePassed;\n }\n //lets check for traffic light\n int div = timePassed/change;\n int timeToWait = 0;\n if(div%2 == 1)\n {\n //you are at a red light ! You can only go ahead after red light completes. \n timeToWait = (div + 1)*change;\n timePassed = timeToWait;\n }\n //now for each neighbour update their times\n for(auto& nbr:adj[cur])\n {\n //only update neighbour if it hasn't been updated 2 times\n if(minTime[nbr]==INT_MAX)\n {\n //time for this node = time for cur + edge traversal time\n minTime[nbr]= timePassed + time;\n q.push({nbr,1});\n }\n else if(secondMinTime[nbr]==INT_MAX && minTime[nbr] != timePassed + time )//secondMin cant be the same as firstMin\n {\n secondMinTime[nbr]= timePassed + time;\n q.push({nbr,2});\n }\n //if it has been updated 2 times, skip it.\n }\n }\n return -1; \n \n }\n};", "memory": "238261" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "#define pp pair<int,int>\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n std::ios_base::sync_with_stdio(false);std::cin.tie(0);\n vector<int>adj[n+1];\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n queue<pp>q;\n vector<pp>dist(n+1,{INT_MAX,INT_MAX});\n\n q.push({0,1});\n dist[1].first=0;\n\n while(!q.empty()){\n auto it=q.front();q.pop();\n\n int node=it.second,d=it.first;\n\n if(dist[node].second != INT_MAX) continue;\n\n if(dist[node].first==INT_MAX) dist[node].first=d;\n\n else if( dist[node].first != d) dist[node].second=d;\n\n if(node==n && dist[node].second != INT_MAX) return dist[node].second;\n\n if((d/change)%2) d+=(change-d%change);\n\n for(auto it:adj[node]){\n q.push({d+time,it});\n }\n }\n \n return -1;\n\n }\n};", "memory": "238261" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "#define q_type tuple<int, int>\n\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n priority_queue<q_type, vector<q_type>, greater<q_type>> pq; // time, node, id\n // unordered_map<int, unordered_set<int>> seen;\n unordered_map<int, vector<int>> graph;\n vector<vector<int>> min_dists(n+1);\n for (auto edge : edges) {\n graph[edge[0]].push_back(edge[1]);\n graph[edge[1]].push_back(edge[0]);\n }\n min_dists[1].push_back(0);\n pq.emplace(0,1);\n while(pq.size()) {\n auto [t, node] = pq.top();\n pq.pop();\n // cout << t << ' ' << node << ' ' << id << endl;\n // seen[id].insert(node);\n if (node == n && min_dists[n].size()==2) {\n return (max(min_dists[n][0], min_dists[n][1]));\n }\n int c = t / change;\n int d = t % change; \n if (c%2) {\n t += (change - d);\n }\n t+=time;\n for (auto child : graph[node]) {\n // cout << child << endl;\n // if (!seen[id].contains(child)) {\n if (!min_dists[child].size() || (min_dists[child].size() == 1 && min_dists[child][0] != t )) {\n pq.emplace(t,child);\n min_dists[child].push_back(t);\n // seen[id2] = seen[id];\n }\n }\n }\n return 0;\n }\n};", "memory": "239443" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> graph(n);\n for (auto edge : edges) {\n graph[edge[0] - 1].push_back(edge[1] - 1);\n graph[edge[1] - 1].push_back(edge[0] - 1);\n }\n\n queue<int> q;\n vector<int> uniqueVisit(n, 0);\n q.push(0);\n int distance = 0;\n int max_2nd_distance = INT_MAX;\n\n function<int(int)> calculateTimeWithDistance = [&time,\n &change](int distance) {\n int result = time;\n int red_signal = change;\n distance--;\n while (distance > 0) {\n while (result >= red_signal) {\n result = max(result, red_signal + change);\n red_signal += change << 1;\n }\n distance--;\n result += time;\n }\n return result;\n };\n\n while (!q.empty()) {\n const int size = q.size();\n vector<bool> visit(n, false);\n\n for (int i = 0; i < size; i++) {\n int cur = q.front();\n q.pop();\n if (uniqueVisit[cur] >= 2)\n continue;\n uniqueVisit[cur]++;\n\n if (cur == n - 1) {\n if (max_2nd_distance == INT_MAX) {\n max_2nd_distance = distance + 2;\n } else if (distance > max_2nd_distance - 2) {\n return calculateTimeWithDistance(distance);\n }\n }\n\n for (const auto next : graph[cur]) {\n if (visit[next])\n continue;\n\n visit[next] = true;\n q.push(next);\n }\n }\n\n distance++;\n if (max_2nd_distance <= distance)\n break;\n }\n\n // cout << distance << endl;\n return calculateTimeWithDistance(distance);\n }\n};", "memory": "239443" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int getWait(int time, int change) {\n int val = time / change;\n if(val & 1) return change - (time % change);\n return 0;\n }\n\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj(n);\n for(vector<int>& edge: edges) {\n int u = edge[0], v = edge[1];\n u--, v--;\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n vector<vector<int>> dist(n, vector<int>(2, INT_MAX));\n dist[0][0] = 0;\n queue<vector<int>> q;\n q.push({0, 0});\n\n while(q.empty() == false) {\n vector<int> curr = q.front(); q.pop();\n int node = curr[0], nodeTime = curr[1];\n\n for(int nbr: adj[node]) {\n int nbrTime = nodeTime + getWait(nodeTime, change) + time;\n if(nbrTime < dist[nbr][0]) {\n dist[nbr][1] = dist[nbr][0];\n dist[nbr][0] = nbrTime;\n q.push({nbr, nbrTime});\n } else if(nbrTime < dist[nbr][1] && nbrTime != dist[nbr][0]) {\n dist[nbr][1] = nbrTime;\n q.push({nbr, nbrTime});\n }\n } \n }\n\n return dist[n-1][1];\n }\n};", "memory": "240626" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int waitTime(int& curr_time , int& change){\n return (((curr_time/change)%2) ? ((change*((curr_time+change)/change))-curr_time) : 0);\n }\n\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> min1(10001,INT_MAX);\n vector<int> min2(10001,INT_MAX);\n vector<vector<int>> v(10001);\n for(auto x:edges){\n v[x[0]].push_back(x[1]);\n v[x[1]].push_back(x[0]);\n }\n\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> p;\n set<int> times;\n for(auto x:v[1]){\n int total_time = time;\n if(x == n){\n times.insert(time);\n }\n else{\n total_time += waitTime(time,change);\n }\n min1[x] = total_time;\n p.push({total_time,x});\n }\n\n while(!p.empty() && times.size() < 2){\n auto t = p.top();\n p.pop();\n for(auto x:v[t.second]){\n int curr_time = t.first+time;\n \n if(x == n){\n times.insert(curr_time);\n if(times.size() == 2){\n break;\n }\n }\n curr_time += waitTime(curr_time,change);\n\n if(curr_time > min1[x] && curr_time <= min2[x]){\n min2[x] = curr_time;\n p.push({curr_time,x});\n }\n else if(curr_time < min1[x]){\n min2[x] = min1[x];\n min1[x] = curr_time;\n p.push({curr_time,x});\n }\n }\n }\n\n for(auto x:times){\n cout << x << \" \";\n }\n if(times.size() == 2){\n return *times.rbegin();\n }\n return 0;\n }\n};", "memory": "240626" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unordered_map<int, list<int>> adj;\n\n for(int i = 0; i < edges.size(); i++){\n int u = edges[i][0];\n int v = edges[i][1];\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n vector<vector<int>> res(n+1, vector<int>(2, INT_MAX));\n\n queue<pair<int, int>> q;\n res[1][0] = 0;\n q.push({1, 0});\n\n while(!q.empty()){\n auto p = q.front();\n q.pop();\n\n int node = p.first;\n int currTime = p.second;\n\n if(node == n && res[n][1] != INT_MAX && res[n][1] > res[n][0])\n {\n break;\n }\n\n if((currTime/change)%2 != 0)\n {\n int waitTime = change- (currTime%change);\n currTime += waitTime + time;\n } else{\n currTime += time;\n }\n\n for(auto neighbour : adj[node]){\n if(res[neighbour][0] == INT_MAX){\n res[neighbour][0] = currTime;\n q.push({neighbour, currTime});\n }\n else if(res[neighbour][1] == INT_MAX && currTime > res[neighbour][0])\n {\n res[neighbour][1] = currTime;\n q.push({neighbour, currTime});\n }\n }\n }\n return res[n][1];\n }\n};", "memory": "241808" }
2,171
<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&nbsp;<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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unordered_map<int, vector<int>> g;\n\n for (const auto& edge : edges) {\n g[edge[0]].push_back(edge[1]);\n g[edge[1]].push_back(edge[0]);\n }\n\n auto pminq_cmp = [&](auto a, auto b) { return a.time > b.time; };\n\n struct pinfo { int id; int time; };\n\n using minq_t = priority_queue<pinfo, vector<pinfo>, decltype(pminq_cmp)>;\n\n minq_t q(pminq_cmp);\n\n vector<vector<int>> records(n + 1);\n\n q.push({1, 0});\n\n auto next = [&](auto t) {\n if ((t / change) % 2) t += change - t % change;\n t += time;\n return t;\n };\n\n auto update = [&](auto n, auto t) {\n records[n].push_back(t);\n sort(records[n].begin(), records[n].end());\n if (records[n].size() > 2) records[n].resize(2);\n };\n\n auto important = [&](auto n, auto t) {\n return !records[n].size()\n || records[n].size() == 1 && records[n].front() != t\n || records[n].size() > 1 && records[n].back() > t;\n };\n\n while (!q.empty()) {\n auto current = q.top();\n q.pop();\n\n if (important(current.id, current.time)) {\n update(current.id, current.time);\n for (auto v : g[current.id])\n q.push({v, next(current.time)});\n }\n }\n\n return records[n].size() ? records[n].back() : -1;\n }\n};", "memory": "241808" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n int max = std::numeric_limits<int>::max(), last{max}, min_difference{max};\n int getMinimumDifference(TreeNode* root) {\n if (!root)\n return max;\n\n getMinimumDifference(root->left);\n root->left = nullptr;\n int difference{std::abs(last - root->val)};\n if (difference < min_difference)\n min_difference = difference;\n last = root->val;\n getMinimumDifference(root->right);\n root->right = nullptr;\n\n return min_difference;\n }\n};", "memory": "17800" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int getMinimumDifference(TreeNode* root) {\n int min_dist = INT_MAX;\n int prev = -1;\n while (root) {\n if (root->left) {\n getMostRightLeaf(root->left)->right = root;\n auto tmp = root->left;\n root->left = nullptr;\n root = tmp;\n continue;\n }\n if (prev >= 0) min_dist = min(min_dist, abs(root->val - prev));\n prev = root->val;\n root = root->right;\n }\n return min_dist;\n }\n \n\n TreeNode* getMostRightLeaf(TreeNode* t) {\n while (t->right) t = t->right;\n return t;\n }\n};", "memory": "17900" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int getMinimumDifference(TreeNode* root) {\n TreeNode* prev = nullptr;\n int res = INT_MAX;\n while(root != nullptr) {\n if(root->left) {\n TreeNode* left = root->left;\n while(left->right) left = left->right;\n left->right = root;\n TreeNode* temp = root->left;\n root->left = nullptr;\n root = temp;\n }\n else {\n if(prev) res = min(res, abs(root->val - prev->val));\n prev = root;\n root = root->right;\n }\n }\n return res;\n }\n};", "memory": "18000" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int getMinimumDifference(TreeNode* root) {\n if(root==nullptr)\n return 0;\n TreeNode* temp = nullptr;\n TreeNode* prev = nullptr;\n TreeNode* cur = root;\n int minabs=INT_MAX;\n vector<int> nums;\n while(cur)\n {\n if(cur->left!=nullptr)\n {\n prev = cur->left;\n while(prev!= nullptr &&prev->right!=nullptr)\n {\n prev = prev->right;\n }\n prev->right = cur;\n temp = cur;\n cur = cur->left;\n temp->left = nullptr;\n }\n else\n {\n // if(cur!= nullptr && cur->right!=nullptr)\n // {\n // minabs = min(minabs, abs(cur->val - cur->right->val));\n // }\n nums.push_back(cur->val);\n cur = cur->right;\n }\n }\n\n for(ptrdiff_t i =0; i < nums.size()-1;i++)\n {\n minabs = min(minabs, abs(nums[i]- nums[i+1]));\n }\n\n\n return minabs;\n\n }\n};", "memory": "18100" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int getMinimumDifference(TreeNode* root) {\n if(root==nullptr)\n return 0;\n TreeNode* temp = nullptr;\n TreeNode* prev = nullptr;\n TreeNode* cur = root;\n int minabs=INT_MAX;\n vector<int> nums;\n while(cur)\n {\n if(cur->left!=nullptr)\n {\n prev = cur->left;\n while(prev!= nullptr &&prev->right!=nullptr)\n {\n prev = prev->right;\n }\n prev->right = cur;\n temp = cur;\n cur = cur->left;\n temp->left = nullptr;\n }\n else\n {\n // if(cur!= nullptr && cur->right!=nullptr)\n // {\n // minabs = min(minabs, abs(cur->val - cur->right->val));\n // }\n nums.push_back(cur->val);\n cur = cur->right;\n }\n }\n\n for(ptrdiff_t i =0; i < nums.size()-1;i++)\n {\n minabs = min(minabs, abs(nums[i]- nums[i+1]));\n }\n\n\n return minabs;\n\n }\n};", "memory": "18200" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int getMinimumDifference(TreeNode* root) {\n vector<int> ans;\n\n TreeNode* curr=root;\n \n while(curr!=nullptr)\n {\n TreeNode *pre=curr->left;\n if(pre==nullptr)\n {\n ans.push_back(curr->val);\n curr=curr->right;\n }\n else\n {\n while(pre->right!=nullptr)\n {\n pre=pre->right;\n }\n TreeNode* temp=curr;\n pre->right=curr;\n curr=curr->left;\n temp->left=nullptr;\n }\n }\n int an=INT_MAX;\n for(int i=1;i<ans.size();++i)\n {\n an=min(an,ans[i]-ans[i-1]);\n }\n return an;\n }\n};", "memory": "18300" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int getMinimumDifference(TreeNode* root) {\n int min = 1000000;\n vector<int> node_vals;\n stack<TreeNode*> st;\n st.push(root);\n while(!st.empty()){\n TreeNode* top;\n top = st.top();\n if(top->left == NULL && top->right == NULL){\n node_vals.push_back(top->val);\n st.pop();\n }\n while(top->left != NULL){\n st.push(top->left);\n top->left = NULL;\n top = st.top();\n }\n if(top->right != NULL){\n node_vals.push_back(top->val);\n st.pop(); \n st.push(top->right);\n top->right = NULL;\n top = NULL; \n }\n }\n for(int i=0;i<node_vals.size()-1;i++) if(min >= abs(node_vals[i] - node_vals[i+1])) min = abs(node_vals[i] - node_vals[i+1]);\n return min;\n }\n};", "memory": "18600" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\n int m = INT_MAX;\n\npublic:\n int getMinimumDifference(TreeNode* root) {\n dfs(root);\n return m;\n }\n void dfs2(TreeNode* root, int val) {\n if (!root) return;\n m = min(m, abs(val - (root->val)));\n dfs2(root->left, val);\n dfs2(root->right, val);\n }\n\n void dfs(TreeNode* root) {\n if (!root)\n return;\n dfs2(root->left, root->val);\n dfs2(root->right, root->val);\n\n dfs(root->left);\n dfs(root->right);\n }\n};", "memory": "23700" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int leftrightest(TreeNode* root){\n if(root->right==NULL){\n return root->val;\n }\n\n return leftrightest(root->right);\n }\n int rightleftest(TreeNode* root){\n if(root->left==NULL){\n return root->val;\n }\n\n return rightleftest(root->left);\n }\n int getMinimumDifference(TreeNode* root) {\n if(root==NULL || (root->right==NULL && root->left==NULL)){\n return INT_MAX;\n }\n\n int left=INT_MAX, right=INT_MAX;\n int diff1=INT_MAX, diff2=INT_MAX;\n int leftest=INT_MAX, righest=INT_MAX;\n if(root->left != NULL){\n left=getMinimumDifference(root->left);\n diff1= root->val-root->left->val;\n leftest=root->val-leftrightest(root->left);\n\n } \n diff1=min(diff1, leftest);\n if(root->right!=NULL) {\n right=getMinimumDifference(root->right);\n diff2= root->right->val-root->val;\n righest=rightleftest(root->right)-root->val;\n\n }\n diff2=min(diff2, righest);\n\n return min(diff1 , min(diff2 , min(left,right)));\n\n }\n};", "memory": "23800" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void f(TreeNode *root, int &prevValue, int &miniDifference)\n {\n if(root==NULL) return;\n \n f(root->left, prevValue, miniDifference);\n if(prevValue!=-1){\n miniDifference=min(miniDifference ,abs(root->val-prevValue));\n }\n prevValue=root->val;\n f(root->right, prevValue, miniDifference);\n }\n\n int getMinimumDifference(TreeNode* root) {\n int prevValue=-1;\n int miniDifference=INT_MAX;\n f(root, prevValue, miniDifference);\n return miniDifference;\n }\n};", "memory": "23900" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n int min_diff = INT_MAX;\n int prev = INT_MAX;\npublic:\n int getMinimumDifference(TreeNode* root) {\n if (!root)\n return min_diff;\n\n getMinimumDifference(root->left);\n int diff = abs(prev - root->val);\n min_diff = min(min_diff, diff);\n prev = root->val;\n getMinimumDifference(root->right);\n\n return min_diff;\n }\n};", "memory": "23900" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nint minDiff=INT_MAX;\nTreeNode*prev=nullptr;\nvoid inOrderTraversal(TreeNode*root){\n if(root==nullptr) return;\n inOrderTraversal(root->left);\n if(prev!=nullptr){\n minDiff=min(minDiff,root->val-prev->val);\n }\n prev=root;\n inOrderTraversal(root->right);\n}\n int getMinimumDifference(TreeNode* root) {\n inOrderTraversal(root);\n return minDiff;\n }\n};", "memory": "24000" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int traverse(TreeNode* node, int minValue, int maxValue) {\n int dif = INT_MAX;\n dif = std::min(abs(node->val - minValue), abs(node->val - maxValue));\n int dif1 = INT_MAX, dif2 = INT_MAX;\n if (node->left != nullptr) dif1 = traverse(node->left, minValue, node->val);\n if (node->right != nullptr) dif2 = traverse(node->right, node->val, maxValue);\n return std::min(dif, std::min(dif1, dif2)); \n }\n \n int getMinimumDifference(TreeNode* root) {\n return traverse(root, -100001, 200001);\n }\n};", "memory": "24000" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int minD = INT_MAX;\n void inorder(TreeNode* root, TreeNode *&prev){\n\n if(!root)return ; \n inorder(root->left,prev);\n if(prev != NULL){\n minD = min(minD, abs(root->val - prev->val));\n }\n prev = root ;\n inorder(root->right,prev);\n\n }\n int getMinimumDifference(TreeNode* root) {\n TreeNode *prev = NULL ;\n inorder(root,prev);\n return minD;\n }\n};", "memory": "24100" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n int answer = INT_MAX;\n TreeNode *prev = nullptr;\n\npublic:\n void helper(TreeNode *root) {\n if (root->left){\n helper(root->left);\n }\n\n if (prev) {\n answer = min(answer, abs(prev->val - root->val));\n }\n\n prev = root;\n if (root->right) {\n helper(root->right);\n }\n }\n\n int getMinimumDifference(TreeNode *root) {\n helper(root);\n return answer;\n }\n};", "memory": "24100" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root, vector<int> &arr){\n if(root==nullptr){\n return ;\n }\n inorder(root->left,arr);\n arr.push_back(root->val);\n inorder(root->right,arr);\n\n }\n int getMinimumDifference(TreeNode* root) {\n vector<int> arr;\n int m = INT_MAX;\n inorder(root,arr);\n for(int i=1; i<arr.size(); i++){\n m = min(m,arr[i]-arr[i-1]);\n }\n // cout<<m;\n return m;\n }\n};", "memory": "24200" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root, int& diff, int& prev) {\n if (!root)\n return;\n\n inorder(root->left, diff, prev);\n\n if (prev != -1) {\n diff = min(diff, abs(root->val - prev));\n }\n\n prev = root->val;\n\n inorder(root->right, diff, prev);\n }\n\n int getMinimumDifference(TreeNode* root) {\n int diff = INT_MAX;\n int prev = -1;\n inorder(root, diff, prev);\n return diff;\n }\n};\n", "memory": "24200" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
1
{ "code": "class Solution {\n public:\n // Similar to 94. Binary Tree Inorder Traversal\n int getMinimumDifference(TreeNode* root) {\n int ans = INT_MAX;\n int prev = -1;\n stack<TreeNode*> stack;\n\n while (root != nullptr || !stack.empty()) {\n while (root != nullptr) {\n stack.push(root);\n root = root->left;\n }\n root = stack.top(), stack.pop();\n if (prev >= 0)\n ans = min(ans, root->val - prev);\n prev = root->val;\n root = root->right;\n }\n\n return ans;\n }\n};", "memory": "24300" }
530
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> <pre> <strong>Input:</strong> root = [4,2,6,1,3] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,0,48,null,null,12,49] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root, vector<int> &arr){\n if(root==nullptr){\n return ;\n }\n inorder(root->left,arr);\n arr.push_back(root->val);\n inorder(root->right,arr);\n\n }\n int getMinimumDifference(TreeNode* root) {\n vector<int> arr;\n int m = INT_MAX;\n inorder(root,arr);\n for(int i=1; i<arr.size(); i++){\n m = min(m,arr[i]-arr[i-1]);\n }\n // cout<<m;\n return m;\n }\n};", "memory": "24300" }