id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) \n {\n vector<int>ans(n,-1);\n vector<pair<int,int>>v[n];\n for(auto it:edges){\n v[it[0]].push_back({it[1],it[2]});\n v[it[1]].push_back({it[0],it[2]});\n }\n\n set<pair<int,int>>s;\n s.insert({0,0});\n\n while(s.size())\n {\n auto it=*s.begin();\n s.erase(it);\n if(it.first>=disappear[it.second])\n {\n continue;\n }\n if(ans[it.second]!=-1)continue;\n ans[it.second]=it.first;\n \n for(auto itr:v[it.second])\n {\n if(ans[itr.first]==-1&&disappear[itr.first]>it.first+itr.second)\n {\n s.insert({it.first+itr.second,itr.first});\n }\n }\n }\n\n return ans;\n\n\n\n\n }\n};", "memory": "240351" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "typedef pair<int, int> intPair;\n\nclass Solution {\nprivate:\n int startIndex = 0;\n int startDist = 0;\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n unordered_map<int, vector<intPair>> graph;\n vector<int> distance (n, INT_MAX);\n vector<bool> visited(n, false);\n priority_queue<intPair, vector<intPair>, greater<intPair>> minHeap;\n\n for (auto edge: edges) {\n graph[edge[0]].push_back({edge[1], edge[2]});\n graph[edge[1]].push_back({edge[0], edge[2]});\n }\n\n minHeap.push({startDist, startIndex});\n\n while (!minHeap.empty()) {\n int currentNode = minHeap.top().second;\n int currentNodeDist = minHeap.top().first;\n minHeap.pop();\n\n if (currentNodeDist >= disappear[currentNode] || visited[currentNode]) {\n continue;\n }\n\n distance[currentNode] = currentNodeDist;\n visited[currentNode] = true;\n\n for (auto adjacent: graph[currentNode]) {\n int adjacentNode = adjacent.first;\n int adjacentNodeDist = adjacent.second;\n\n if (!visited[adjacentNode]) {\n minHeap.push({currentNodeDist + adjacentNodeDist, adjacentNode});\n }\n }\n }\n\n for (int index = 0; index < n; index++) {\n distance[index] = distance[index] == INT_MAX\n ? -1\n : distance[index];\n }\n\n return distance;\n }\n};", "memory": "240351" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n map<int,list<pair<int,int>>> mp;\n int m=edges.size();\n for(int i=0;i<m;i++){\n mp[edges[i][0]].push_back({edges[i][1],edges[i][2]});\n mp[edges[i][1]].push_back({edges[i][0],edges[i][2]});\n } \n\n vector<int> ans(n,-1);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n pq.push({0,0});\n\n\n while(!pq.empty()){\n int cost=pq.top().first;\n int node=pq.top().second;\n pq.pop();\n if(ans[node]==-1){\n ans[node]=cost;\n \n }\n else{\n continue;\n }\n if(cost<disappear[node]){\n ans[node]=min(ans[node],cost);\n }\n // if((cost<ans[node] || ans[node]==-1) && cost<disappear[node]){\n // ans[node]=cost;\n // }\n\n if(cost>=disappear[node]){\n continue;\n }\n\n for(auto i:mp[node]){\n int j=i.first;\n int price=i.second;\n\n if((ans[j]==-1 || ans[j]<cost+price) && (cost+price)<disappear[j]){\n pq.push({cost+price,j});\n // ans[j]=10e7;\n \n }\n }\n }\n\n return ans;\n }\n};", "memory": "241903" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> dis,vis;\n unordered_map<int,vector<pair<int,int>>> gr;\n\n void djs(int src, vector<int>& disappear){\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n pq.push({0,src});\n while(!pq.empty()){\n pair<int,int> best = pq.top();\n pq.pop();\n int d = best.first;\n int node = best.second;\n if(vis[node] || d>= disappear[node])\n continue;\n dis[node]=d;\n vis[node]=1;\n for(auto ne : gr[node]){\n if(vis[ne.first]==0)\n pq.push({d+ne.second,ne.first});\n }\n }\n }\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n for(auto edge : edges){\n int u= edge[0],v=edge[1],w=edge[2];\n gr[u].push_back({v,w});\n gr[v].push_back({u,w});\n }\n dis = vector<int>(n,-1), vis=vector<int>(n,0);\n djs(0,disappear);\n return dis;\n }\n};", "memory": "241903" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "#pragma GCC optimize(\"03\")\nclass Solution {\npublic:\n Solution(){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr); cout.tie(nullptr);\n }\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n edges.shrink_to_fit(); \n disappear.shrink_to_fit();\n vector<pair<int, int>> adj[50000];\n \n int szEdges = edges.size();\n {\n int cnt[50000] = {0};\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n cnt[u]++; cnt[v]++;\n }\n for (int i = 0; i < n; i++) \n adj[i].reserve(cnt[i]);\n }\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n int p = edges[i][2];\n adj[u].push_back({p, v});\n adj[v].push_back({p, u});\n }\n for (int i = 0; i < n; i++) \n sort(adj[i].begin(), adj[i].end());\n \n int dp[50000]; dp[0] = 0;\n for (int i = 1; i < n; i++) dp[i] = INT_MAX;\n\n priority_queue<pair<int, int>> heap;\n heap.push({0, 0});\n while (!heap.empty())\n {\n int u = heap.top().second;\n int cur = -heap.top().first;\n heap.pop();\n\n if (cur > dp[u]) continue;\n if (dp[u] >= disappear[u]) continue;\n\n int l = adj[u].size();\n for (int i = 0; i < l; i++)\n {\n int v = adj[u][i].second;\n int p = adj[u][i].first;\n int update = dp[u] + p;\n if (update >= dp[v]) continue;\n dp[v] = update;\n heap.push({-dp[v], v});\n }\n }\n\n vector<int> ans(n);\n for (int i = 0; i < n; i++) \n {\n bool ch = dp[i] == INT_MAX || dp[i] >= disappear[i];\n ans[i] = ch ? -1 : dp[i];\n }\n return ans;\n }\n};", "memory": "243454" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>&edges, vector<int>&dist) {\n map<int,vector<pair<int,int>>>mp;\n for(int i=0;i<edges.size();i++)\n {\n mp[edges[i][0]].push_back({edges[i][1],edges[i][2]});\n mp[edges[i][1]].push_back({edges[i][0],edges[i][2]});\n }\n vector<int>dis(n,1e8);\n set<pair<int,int>>st;\n dis[0]=0;\n set<int>ss;\n st.insert({0,0});\n while(st.size())\n {\n auto&it = *st.begin();\n int node = it.second;\n int cost = it.first;\n st.erase(it);\n if(ss.find(node)!=ss.end())\n {\n continue;\n }\n ss.insert(node);\n for(auto&ik:mp[node])\n {\n int nn = ik.first;\n int nc = ik.second;\n if(dis[nn]>nc+cost&&nc+cost<dist[nn])\n {\n dis[nn] = nc+cost;\n st.insert({dis[nn],nn});\n }\n }\n }\n vector<int>ans;\n for(int i=0;i<n;i++)\n {\n if(dis[i]>=1e8)\n {\n ans.push_back(-1);\n }\n else\n {\n ans.push_back(dis[i]);\n }\n }\n return ans;\n\n }\n};", "memory": "243454" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplate<typename T> istream & operator>>(istream & in, vector<T> & lst)\n{ for (auto & e : lst) in >> e; return in; }\n#define endl '\\n'\n#define F first\n#define pb push_back\n#define pf push_front\n#define pob pop_back\n#define pof pop_front\n#define S second\n#define MP make_pair\n// typedef long long ll;\ntypedef long double ld;\ntypedef pair<ll, ll> ii;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<ii> vii;\ntypedef set<ll> si;\ntypedef map<string, ll> msi;\n#define all(v) v.begin(),v.end()\n#define REP(i, a, b) \\\nfor (ll i = ll(a); i <= ll(b); i++)\n#define RREP(i, a, b) \\\nfor (ll i = ll(a); i >= ll(b); i--)\n#define ohyes cout<<\"YES\\n\"\n#define ohno cout<<\"NO\\n\"\n#define getvec(v,a,b) \\\nfor (ll i = a; i <=b; i++) \\\n cin >> v[i];\n#define printvec(v,a,b) {for (ll i = a; i <=b; i++){cout << v[i] << \" \";}cout<<'\\n';}\n#define get2dvec(v,a,b,c,d) \\\nfor (ll i = a; i <=b; i++) \\\n for (ll j = c; j <=d; j++) \\\n cin >> v[i][j];\n#define print2dvec(v,a,b,c,d) for (ll i = a; i <=b; i++){for (ll j = c; j <=d; j++){cout << v[i][j]<<\" \";}cout<<'\\n';}\n#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n// ll mod = 998244353;\n// ll mod = 1e9 + 7;\nll _t,cs;\n// cout<<\"Case \"<<cs-_t<<\": \"<<ans<<'\\n';\n// memset(dp,-1,sizeof(dp));\nclass Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<ll,ll>>adj[n];\n for(auto x:edges){\n adj[x[0]].push_back({x[2],x[1]});\n adj[x[1]].push_back({x[2],x[0]});\n }\n vector<int>dis(n,1e5+1);\n dis[0]=0;\n priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>>pq;\n pq.push({0,0});\n vi mark(n);\n while(!pq.empty()){\n pair<ll,ll>top=pq.top();\n pq.pop();\n ll dinh=top.second;\n if(mark[dinh])continue;\n for(auto x:adj[dinh]){\n ll dk=x.second;\n ll w=x.first;\n if(dis[dk]>dis[dinh]+w && dis[dinh]+w<disappear[dk]){\n dis[dk]=dis[dinh]+w;\n pq.push({dis[dk],dk});\n }\n }\n mark[dinh]=1;\n }\n for(auto &i:dis){\n if(i==1e5+1)i=-1;\n }\n return dis; \n }\n};", "memory": "245005" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "#define l long long\nclass Solution {\npublic:\n\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n \n vector<pair<l,l>>adj[n];\n for(auto it:edges){\n adj[it[0]].push_back({(l)it[1], (l)it[2]});\n adj[it[1]].push_back({(l)it[0], (l)it[2]});\n }\n\n // queue<pair<l,l>, vector<pair<l,l>>, greater<pair<l,l>>>pq;\n queue<pair<l, l>>pq;\n pq.push({0ll, 0ll}); //node, time\n vector<l>vis(n, 0);\n vector<int>time(n, INT_MAX);\n time[0] = 0;\n vis[0] = 1;\n while(!pq.empty()){\n int nd = pq.front().first;\n l tm = pq.front().second;\n pq.pop();\n\n if(tm>=disappear[nd]||time[nd]<tm)continue;\n // time[nd] = tm;\n for(auto it:adj[nd]){\n if(!vis[it.first] && tm+it.second<disappear[it.first]){\n vis[it.first] = 1;\n time[it.first] = tm+it.second;\n pq.push({it.first, time[it.first]});\n }\n else if(tm+it.second<disappear[it.first]&&tm+it.second<time[it.first]){\n time[it.first] = tm+it.second;\n pq.push({it.first, time[it.first]});\n }\n }\n }\n for(int i=1; i<n; i++){\n if(time[i]==INT_MAX)time[i] = -1;\n }\n return time;\n }\n};", "memory": "245005" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n int m = edges.size();\n\n unordered_map<int, list<pair<int, int>>> adj;\n\n for(int i = 0; i < m; i++){\n int u = edges[i][0];\n int v = edges[i][1];\n int w = edges[i][2];\n\n adj[u].push_back(make_pair(v, w));\n adj[v].push_back(make_pair(u, w));\n }\n\n set<pair<int, int>> s;\n vector<int> times(n, INT_MAX);\n\n times[0] = 0;\n s.insert(make_pair(0, 0));\n\n while(!s.empty()){\n auto top = *(s.begin());\n int time = top.first;\n int topNode = top.second;\n\n s.erase(s.begin());\n\n for(auto nbr : adj[topNode]){\n if(time + nbr.second < times[nbr.first] && disappear[nbr.first] > time + nbr.second){\n auto record = s.find(make_pair(times[nbr.first], nbr.first));\n\n if(record != s.end()) s.erase(record);\n\n times[nbr.first] = time + nbr.second;\n s.insert(make_pair(times[nbr.first], nbr.first));\n }\n }\n }\n\n for(int i = 0; i < n; i++){\n if(times[i] == INT_MAX) times[i] = -1;\n }\n\n return times;\n }\n};", "memory": "246556" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pair<int,int>>> adj(n);\n for(auto it:edges){\n int u=it[0];\n int v=it[1];\n int wt=it[2];\n bool e=false;\n for(auto &it:adj[u]){\n if(it.second==v){\n if(wt<it.first){\n it.first=wt;\n \n }\n e=true;\n break;\n }\n\n }\n if(!e)adj[u].push_back({wt,v});\n e=false;\n for(auto &it:adj[v]){\n if(it.second==u){\n if(wt<it.first){\n it.first=wt;\n \n \n }\n e=true;\n break;\n }\n }\n if(!e)adj[v].push_back({wt,u});\n }\n set<pair<int,int>> st;\n vector<int> visited(n);\n visited[0]=0;\n vector<int> timereach(n,1e9);\n timereach[0]=0;\n st.insert({0,0});\n vector<int> ans(n,-1);\n set<int> disappeared;\n ans[0]=0;\n while(!st.empty()){\n auto t=*st.begin();\n int time=t.first;\n int node=t.second;\n st.erase(*st.begin());\n if(disappeared.find(node)!=disappeared.end()) continue;\n disappeared.insert(node);\n if(time>disappear[node]) continue;\n \n \n for(auto it:adj[node]){\n int v=it.second;\n int wt=it.first;\n if(timereach[node]+wt<timereach[v] && timereach[node]+wt<disappear[v]){\n timereach[v]=timereach[node]+wt;\n st.insert({timereach[v],v});\n ans[v]=timereach[v];\n }\n \n }\n\n }\n //vector<int> ans(n,-1);\n // for(int i=0;i<n;i++){\n // if(disappear[i]<timereach[i]) ans[i]=-1;\n // }\n return ans;\n\n }\n};", "memory": "246556" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "typedef pair<int,int> pii;\nclass Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pii>> graph(n);\n for(auto edge : edges){\n graph[edge[0]].push_back({edge[1],edge[2]});\n graph[edge[1]].push_back({edge[0],edge[2]});\n }\n vector<int> dis(n,INT_MAX);\n // priority_queue<pii,vector<pii>,greater<pii>> pq;\n set<pii> st;\n // pq.push({0,0});\n // node, distance\n st.insert({0,0});\n dis[0]=0;\n while(!st.empty()){\n // auto top = pq.top();\n // pq.pop();\n auto top = *(st.begin());\n st.erase(top);\n int node = top.first ,wt=top.second;\n for(auto it : graph[node]){\n int nd = it.first, edwt=it.second;\n if(wt+edwt<dis[nd] && wt+edwt<disappear[nd]){\n if(st.find({nd,dis[nd]})!=st.end()){\n st.erase({nd,dis[nd]});\n }\n dis[nd]=wt+edwt;\n // pq.push({nd,dis[nd]});\n st.insert({nd,dis[nd]});\n }\n\n }\n\n }\n for(int &x : dis){\n if(x==INT_MAX) x=-1;\n }\n return dis;\n }\n};", "memory": "248108" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& dis) {\n vector<pair<int,int>> adj[n];\n for(int i = 0;i<edges.size();i++){\n int u = edges[i][0] , v = edges[i][1] , w = edges[i][2];\n adj[u].push_back({v , w});\n adj[v].push_back({u , w});\n }\n vector<int>dist(n , INT_MAX);\n priority_queue<pair<int,int> , vector<pair<int,int>> , greater<pair<int,int>>> pq;\n dist[0] = 0;\n pq.push({dist[0] , 0});\n\n while(!pq.empty()){\n int srcx = pq.top().second , distx = pq.top().first;\n pq.pop();\n if(distx > dist[srcx]) continue;\n for(auto itr : adj[srcx]){\n int currDist = distx + itr.second;\n if(currDist < dist[itr.first] && currDist < dis[itr.first]){\n dist[itr.first] = currDist;\n pq.push({dist[itr.first] , itr.first});\n }\n }\n }\n for(int i = 0;i<n;i++){\n if(dist[i] == INT_MAX) dist[i] = -1;\n }\n return dist;\n }\n};", "memory": "249659" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>> adj[n];\n for(auto &i: edges){\n int u=i[0];\n int v=i[1];\n int wt=i[2];\n\n adj[u].push_back({v, wt});\n adj[v].push_back({u, wt}); \n }\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n vector<int> dist(n, 1e9);\n dist[0]=0;\n pq.push({0, 0});\n while(!pq.empty()){\n auto it=pq.top();\n pq.pop();\n int u=it.second;\n int wt=it.first;\n if(wt>dist[u]){\n continue;\n }\n for(auto &i: adj[u]){\n int v=i.first;\n int dis=i.second;\n\n if(wt+dis<disappear[v] && wt+dis<dist[v]){\n dist[v]=wt+dis;\n pq.push({dist[v], v});\n }\n\n }\n }\n for(auto &i: dist){\n if(i==1e9){\n i=-1;\n }\n }\n return dist;\n \n }\n};", "memory": "251210" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& v, vector<int>& d) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;\n vector<pair<int,int>>adj[n];\n for(int i=0;i<v.size();i++) {\n int a=v[i][0],b=v[i][1],t=v[i][2];\n if(a==b)\n continue;\n adj[a].push_back({b,t});\n adj[b].push_back({a,t});\n }\n q.push({0,0});\n vector<int>dist(n,INT_MAX);\n vector<bool>vis(n,0);\n dist[0]=0;\n while(!q.empty()) {\n pair<int,int>p=q.top();\n q.pop();\n int t=p.first,s=p.second;\n if(vis[s]==1)\n continue;\n if(t>=d[s])\n continue;\n vis[s]=1;\n for(auto i:adj[s]) {\n if(t+i.second<dist[i.first] && t+i.second<=d[i.first]) {\n dist[i.first]=t+i.second;\n q.push({t+i.second,i.first});\n }\n }\n }\n vector<int>ans;\n for(int i=0;i<n;i++) {\n if(dist[i]<d[i]) \n ans.push_back(dist[i]);\n else\n ans.push_back(-1);\n }\n return ans;\n }\n};", "memory": "254313" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& v, vector<int>& d) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;\n vector<pair<int,int>>adj[n];\n for(int i=0;i<v.size();i++) {\n int a=v[i][0],b=v[i][1],t=v[i][2];\n if(a==b)\n continue;\n adj[a].push_back({b,t});\n adj[b].push_back({a,t});\n }\n q.push({0,0});\n vector<int>dist(n,INT_MAX);\n vector<bool>vis(n,0);\n dist[0]=0;\n while(!q.empty()) {\n pair<int,int>p=q.top();\n q.pop();\n int t=p.first,s=p.second;\n if(vis[s]==1)\n continue;\n if(t>=d[s])\n continue;\n vis[s]=1;\n for(auto i:adj[s]) {\n if(t+i.second<dist[i.first] && t+i.second<=d[i.first]) {\n dist[i.first]=t+i.second;\n q.push({t+i.second,i.first});\n }\n }\n }\n vector<int>ans;\n for(int i=0;i<n;i++) {\n if(dist[i]<d[i]) \n ans.push_back(dist[i]);\n else\n ans.push_back(-1);\n }\n return ans;\n }\n};", "memory": "254313" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n \n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n\n pq.push({0,0});\n\n vector<vector<pair<int,int>>>adjList(n);\n\n for(auto & edge: edges){\n adjList[edge[0]].push_back({edge[1],edge[2]});\n adjList[edge[1]].push_back({edge[0],edge[2]});\n\n }\n\n vector<int>dis(n,INT_MAX);\n dis[0]=0;\n \n\n while(!pq.empty()){\n auto curr=pq.top();\n pq.pop();\n int node=curr.second;\n int dist=curr.first;\n \n if(dist>dis[node])continue;\n\n\n for(auto & next:adjList[node]){\n int nextNode=next.first;\n int weight=next.second;\n if(dist>disappear[nextNode]){\n \n continue;\n }\n \n \n \n if(dist+weight >=disappear[nextNode] ){\n\n continue;\n }\n if( dist+weight<dis[nextNode]){\n \n dis[nextNode]=dist+weight;\n pq.push({dis[nextNode],nextNode});\n }\n }\n }\n\n\n for(auto & d: dis){\n if(d==INT_MAX)d=-1;\n }\n\n return dis;\n \n }\n};", "memory": "255864" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pair<int, int>>> graph(n);\n for(const auto& edge : edges){\n int u = edge[0], v = edge[1], w = edge[2];\n graph[u].push_back({v, w});\n graph[v].push_back({u, w});\n }\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> min_heap;\n min_heap.push({0, 0});\n \n vector<int> min_time(n, INT_MAX);\n min_time[0] = 0;\n while(!min_heap.empty()){\n auto [t, cur] = min_heap.top();\n min_heap.pop();\n if(t > min_time[cur]){\n continue;\n }\n for(const auto& [next, weight] : graph[cur]){\n int time = t + weight;\n if(time < min_time[next] && time < disappear[next]){\n min_time[next] = time;\n min_heap.push({min_time[next], next});\n }\n }\n }\n\n for(int i = 0; i < min_time.size(); ++i){\n if(min_time[i] == INT_MAX){\n min_time[i] = -1;\n }\n }\n\n return min_time;\n }\n};", "memory": "255864" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pair<int, int>>> adj(n);\n\n for(int i=0;i<edges.size();i++){\n int u = edges[i][0];\n int v = edges[i][1];\n int w = edges[i][2];\n adj[u].push_back({v, w});\n adj[v].push_back({u, w});\n }\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n vector<int>ans(n,-1);\n vector<int>dist(n,-1);\n pq.push({0,0});\n dist[0] = 0;\n while(!pq.empty()){\n int time = pq.top().first;\n int node = pq.top().second;\n pq.pop();\n if(time>dist[node])continue;\n\n if (time >= disappear[node]) {\n continue;\n }\n if (ans[node] == -1) {\n ans[node] = time; \n }\n \n for(auto it: adj[node]){\n int u = it.first;\n int wt = it.second;\n if(dist[u]==-1 || time + wt < dist[u]){\n dist[u] = time+ wt;\n pq.push({time+wt,u});\n }\n\n }\n \n\n }\n return ans;\n\n }\n};", "memory": "257415" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution\n{\nprivate:\n void dijkstra(int src, vector<vector<pair<int, int>>> &adj, vector<int> &dist, int n,vector<int>&disappear)\n {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n pq.push({0, src}); // Distance to source is 0\n dist[src] = 0;\n\n while (!pq.empty())\n {\n int u = pq.top().second;\n int d = pq.top().first;\n pq.pop();\n\n if (d > dist[u])\n continue;\n\n for (auto &edge : adj[u])\n {\n int v = edge.first;\n int weight = edge.second;\n\n if (dist[u] + weight < dist[v] && d+weight < disappear[v])\n {\n dist[v] = dist[u] + weight;\n pq.push({dist[v], v});\n }\n }\n }\n }\n\npublic:\n vector<int> minimumTime(int n, vector<vector<int>> &edges, vector<int> &disappear)\n {\n vector<vector<pair<int, int>>> adj(n);\n vector<int> dist_from_zero(n, 0);\n vector<int> dist(n, INT_MAX);\n\n for (auto &it : edges)\n {\n int u = it[0];\n int v = it[1];\n int t = it[2];\n\n if (u == 0)\n dist_from_zero[v] = t;\n\n adj[u].push_back({v, t});\n adj[v].push_back({u, t});\n }\n\n dijkstra(0, adj, dist, n,disappear);\n\n vector<int> ans(n,0);\n for (int i = 0; i < n; i++)\n {\n if (dist[i] <= disappear[i])\n ans[i] = dist[i];\n else\n ans[i] = -1;\n }\n\n return ans;\n }\n};\n", "memory": "257415" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class cmp\n{\n public:\n bool operator()(pair<int,int>&p,pair<int,int>&q)\n {\n return p.first>q.first;\n }\n};\n\nclass Solution \n{\npublic:\n vector<int> minimumTime(int n,vector<vector<int>>&e,vector<int>&d) \n {\n vector<vector<pair<int,int>>>v(n);\n for(int i=0;i<e.size();i++)\n {\n int x=e[i][0];\n int y=e[i][1];\n int dis=e[i][2];\n v[x].push_back({y,dis});\n v[y].push_back({x,dis});\n }\n vector<int>t(n,1e7);\n priority_queue<pair<int,int>,vector<pair<int,int>>,cmp>q;\n q.push({0,0});\n t[0]=0;\n vector<bool>vis(n,false);\n while(q.size())\n {\n pair<int,int>temp=q.top();\n q.pop();\n int srcdis=temp.first;\n int x=temp.second;\n if(vis[x])\n {\n continue;\n }\n vis[x]=true;\n if(x!=0 && srcdis>=d[x])\n {\n continue;\n }\n for(int i=0;i<v[x].size();i++)\n {\n int y=v[x][i].first;\n int dis=v[x][i].second;\n if(srcdis+dis<t[y])\n {\n t[y]=srcdis+dis;\n q.push({t[y],y});\n }\n }\n // cout<<x<<\"_\"<<srcdis<<endl;\n }\n vector<int>ans;\n for(int i=0;i<n;i++)\n {\n if(t[i]<d[i])\n {\n ans.push_back(t[i]);\n }\n else\n {\n ans.push_back(-1);\n }\n }\n return ans;\n }\n};", "memory": "258966" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<array<int,2>>> edge;\n vector<int> ShortestPath(vector<int> &disappear)\n {\n int n=disappear.size();\n vector<int> vdis(n,-1);\n priority_queue<array<int,2>,vector<array<int,2>>,greater<array<int,2>>> pq;\n pq.push({0,0});\n while(!pq.empty())\n {\n auto [t,cur]=pq.top();\n pq.pop();\n if(vdis[cur]>=0) continue;\n if(t>=disappear[cur]) continue;\n vdis[cur]=t;\n for(auto [nxt,dis]:edge[cur])\n {\n if(vdis[nxt]<0)\n {\n pq.push({t+dis,nxt});\n }\n }\n }\n return vdis;\n }\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n edge.assign(n,vector<array<int,2>>());\n for(vector<int> &e:edges)\n {\n edge[e[0]].push_back({e[1],e[2]});\n edge[e[1]].push_back({e[0],e[2]});\n }\n return ShortestPath(disappear);\n }\n};", "memory": "258966" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n #define ll long long\n #define ff first \n #define ss second\n vector<int> minimumTime(int n, vector<vector<int>>& edge, vector<int>& dis) {\n int m = edge.size() ;\n vector<vector<pair<int , int>>> mp(n) ;\n\n for(int i = 0 ; i < m ; i++){\n int u = edge[i][0] ;\n int v = edge[i][1] ;\n int w = edge[i][2] ;\n mp[u].push_back({v,w}) ;\n mp[v].push_back({u,w}) ;\n }\n\n // for(int i = 0 ; i < n; i++){\n // int m = mp[i].size() ;\n // cout << i << \" : \" ;\n // for(int j = 0 ; j < m ; j++){\n // cout << mp[i][j].ff << \" \" << mp[i][j].ss << endl ;\n // }\n // cout << endl << endl ;\n // }\n\n priority_queue<pair<ll ,ll> , vector<pair<ll,ll>> , greater<pair<int , int>>> pq ;\n pq.push({0,0}) ;\n vector<int> reachtime(n , 1e9) ;\n vector<int> vis(n , -1) ;\n vector<int> visited(n , 0) ;\n // vis[0] = 0 ;\n reachtime[0] = 0 ;\n while(!pq.empty()){\n int node = pq.top().ss ;\n ll weight = pq.top().ff ;\n pq.pop() ;\n if(visited[node]== 1){\n continue ;\n }\n visited[node] = 1; \n // cout << node << \" \" << weight << \" \" ;\n if(weight < dis[node]){\n // cout << \"2\" << \" \" ;\n if(vis[node] == -1) vis[node] = weight ;\n // cout << vis[node] << endl ;\n }\n else{\n continue ;\n }\n\n for(auto it : mp[node]){\n if(visited[it.ff] == 0 && weight+it.ss < reachtime[it.ff]){\n reachtime[it.ff] = weight+it.ss ;\n pq.push({weight+it.ss , it.ff}) ;\n }\n }\n\n }\n vis[0] = 0 ;\n // for(auto& it : vis){\n // if(it == 1e9){ \n // it = -1 ;\n // }\n // }\n return vis ;\n }\n};", "memory": "260518" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<long long int> dist;\n vector<int> temp;\n void dijkstra(vector<vector<pair<int, int>>>& adj) {\n priority_queue<pair<long long int, int>,\n vector<pair<long long int, int>>,\n greater<pair<long long int, int>>>\n pq;\n pq.push({0, 0});\n while (!pq.empty()) {\n auto [d, node] = pq.top();\n pq.pop();\n if (d >= temp[node]||d>dist[node])\n continue;\n for (auto& it : adj[node]) {\n int nnode = it.first;\n long long int nd = it.second;\n if (dist[node] + nd < dist[nnode]) {\n dist[nnode] = dist[node] + nd;\n pq.push({dist[nnode], nnode});\n }\n }\n }\n }\n vector<int> minimumTime(int n, vector<vector<int>>& edges,\n vector<int>& disappear) {\n vector<vector<pair<int, int>>> adj(n);\n temp=disappear;\n for (auto& it : edges) {\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n dist.resize(n, LLONG_MAX);\n dist[0] = 0;\n dijkstra(adj);\n vector<int> ans(n, -1);\n for (int i = 0; i < n; i++) {\n if (dist[i] < disappear[i]) {\n ans[i] = dist[i];\n }\n }\n return ans;\n }\n};\n", "memory": "260518" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int,int>> adj[n];\n if(n==0) return {};\n for(int i=0;i<edges.size();i++){\n adj[edges[i][0]].push_back({edges[i][1],edges[i][2]});\n adj[edges[i][1]].push_back({edges[i][0],edges[i][2]});\n }\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n vector<int>ans(n,INT_MAX);\n ans[0]=0;\n pq.push({0,0});\n unordered_set<int>s;\n while(!pq.empty()){\n auto p=pq.top();\n pq.pop();\n int time=p.first;\n int node=p.second;\n if(s.find(node)!=s.end())continue;\n s.insert(node);\n if (time >= disappear[node]) continue;\n for(auto & vec:adj[node]){\n int adjNode= vec.first;\n int t= vec.second;\n if(s.find(adjNode)==s.end()&&t + time<ans[adjNode] && t+time<disappear[adjNode] ){\n ans[adjNode]=time+t;\n pq.push({time+t,adjNode});\n }\n }\n }\n for(int i=0;i<n;i++){\n if(ans[i]==INT_MAX) ans[i]=-1;\n }\n return ans;\n }\n};", "memory": "262069" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n priority_queue<pair<int, int>> pq; // time, node\n vector<vector<pair<int, int>>> g(n + 10);\n for(int i = 0; i < edges.size(); i++){\n if(edges[i][0] == edges[i][1])\n continue;\n // cout<<\n g[edges[i][0]].push_back({edges[i][1], edges[i][2]});\n g[edges[i][1]].push_back({edges[i][0], edges[i][2]});\n }\n\n vector<int> ans(n, INT_MAX);\n pq.push({0, 0});\n while(!pq.empty()){\n auto [curTime, curNode] = pq.top();\n curTime *= -1; // for pq sorting\n pq.pop();\n if(disappear[curNode] <= curTime || ans[curNode] != INT_MAX)\n continue;\n \n // cout<<curNode<<\" \"<<curTime<<\"\\n\";\n ans[curNode] = curTime;\n // unordered_map<int, int> nodes;\n for(auto nxt : g[curNode]){\n // if(nodes.find(nxt.first) != nodes.end() && nodes[nxt.first] < nxt.second)\n // continue;\n // nodes[nxt.first] = nxt.second;\n pq.push({(curTime + nxt.second)*-1, nxt.first});\n }\n }\n for(int &element : ans)\n if(element == INT_MAX)\n element = -1;\n return ans;\n }\n};", "memory": "263620" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\nvector<int> minimumTime(int n, vector<vector<int>> &edges, vector<int> &disappear)\n{\n int edgesSize = edges.size();\n\n vector<vector<int>> nodeList(n);\n vector<int> times(n, -1);\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;\n\n for (int i = 0; i < edges.size(); i++)\n {\n nodeList[edges[i][0]].push_back(edges[i][1]);\n nodeList[edges[i][0]].push_back(edges[i][2]);\n\n nodeList[edges[i][1]].push_back(edges[i][0]);\n nodeList[edges[i][1]].push_back(edges[i][2]);\n }\n\n pq.emplace(0, 0);\n times[0] = 0;\n\n vector<bool> visited(n, false);\n\n while (!pq.empty())\n {\n //\n int currentTime = pq.top().first;\n int currentNode = pq.top().second;\n pq.pop();\n if (visited[currentNode])\n {\n continue;\n }\n visited[currentNode] = true;\n\n if (currentTime >= disappear[currentNode])\n {\n continue;\n }\n\n int size = nodeList[currentNode].size();\n for (int i = 0; i < size; i += 2)\n {\n int childNode = nodeList[currentNode][i], edgeWeight = nodeList[currentNode][i + 1];\n int newTime = currentTime + edgeWeight;\n if (newTime < disappear[childNode] && (newTime < times[childNode] || times[childNode] == -1))\n {\n times[childNode] = newTime;\n pq.emplace(newTime, childNode);\n }\n }\n }\n\n return times;\n}\n\n};", "memory": "263620" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n \n unordered_map<int , list<pair<int,int>>>adj ;\n\n for(auto it : edges){\n int u = it[0];\n int v = it[1];\n int w = it[2];\n\n adj[u].push_back({v,w});\n adj[v].push_back({u,w});\n }\n\n vector<int> dist(n ,1e9);\n\n set<pair<int,int>>st;\n\n dist[0] = 0 ;\n st.insert({0,0});\n\n while(!st.empty()){\n auto top = *(st.begin()) ;\n\n int nodeDistance = top.first;\n int topNode = top.second;\n\n st.erase(st.begin());\n\n for(auto nbr : adj[topNode]){\n if(nodeDistance + nbr.second < dist[nbr.first] && nodeDistance + nbr.second < disappear[nbr.first]){\n auto record = st.find({dist[nbr.first] , nbr.first});\n\n if(record != st.end()){\n st.erase(record);\n }\n dist[nbr.first] = nodeDistance + nbr.second ;\n st.insert({dist[nbr.first] , nbr.first});\n }\n }\n }\n\n vector<int> ans ;\n\n for(int i = 0 ; i<n ; i++){\n if(dist[i]<disappear[i]){\n ans.push_back(dist[i]);\n }else{\n ans.push_back(-1);\n }\n }\n\n return ans;\n\n }\n \n};", "memory": "265171" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n // We need to find the minimum path cost from node 0 to all the nodes --> Dijkstra's algorithm.\n typedef pair<int, int> p;\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>> adj[n]; // Corrected type to store {dest, weight}\n \n for(auto& edge: edges) {\n adj[edge[0]].push_back({edge[1], edge[2]}); // Corrected pair storage\n adj[edge[1]].push_back({edge[0], edge[2]}); // Since it's an undirected graph\n }\n\n priority_queue<p, vector<p>, greater<p>> pq;\n unordered_set<int> st; // To mark whether a node is visited or not.\n vector<int> ans(n, -1);\n\n pq.push({0, 0}); // {cost, node}\n \n while(!pq.empty()) {\n int node = pq.top().second, cost = pq.top().first;\n pq.pop();\n\n if(st.find(node) != st.end()) continue; // Node already visited\n st.insert(node);\n ans[node] = cost;\n\n // Now push the adjacent nodes\n for(auto& x: adj[node]) {\n int nextNode = x.first; // Destination node\n int edgeWeight = x.second; // Edge weight\n\n // Push only if it can be reached before disappearing\n if(st.find(nextNode) == st.end() && cost + edgeWeight < disappear[nextNode]) {\n pq.push({cost + edgeWeight, nextNode}); // Correct cumulative cost\n }\n }\n }\n return ans;\n }\n};\n", "memory": "265171" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n int m = edges.size();\n unordered_map<int, unordered_map<int, int>> hm;\n for (int i = 0; i < m; ++i) {\n int u = edges[i][0], v = edges[i][1], l = edges[i][2];\n if (hm[u].count(v)) {\n hm[u][v] = min(hm[u][v], l);\n } else {\n hm[u][v] = l;\n }\n hm[v][u] = hm[u][v];\n }\n vector<int> dist(n, INT_MAX);\n dist[0] = 0;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n pq.push({0, 0});\n while (!pq.empty()) {\n int d = pq.top().first, x = pq.top().second;\n pq.pop();\n if (d > dist[x]) { // 注意这个减枝\n continue;\n }\n for (auto& [y, w] : hm[x]) {\n if (dist[y] > d + w && d + w < disappear[y]) {\n dist[y] = d + w;\n pq.push({dist[y], y});\n }\n }\n }\n for (int i = 0; i < n; ++i) {\n dist[i] = dist[i] == INT_MAX ? -1 : dist[i];\n }\n return dist;\n }\n};", "memory": "266723" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& e, vector<int>& d) \n {\n vector<int>ans(n,INT_MAX);\n ans[0]=0;\n // vector<vector<pair<int,int>>>g(n);\n unordered_map<int,unordered_map<int,int>>g;\n for(auto &x:e)\n {\n if(g.count(x[0])&&g[x[0]].count(x[1]))\n {\n g[x[0]][x[1]]=min(x[2],g[x[0]][x[1]]);\n g[x[1]][x[0]]=min(x[2],g[x[1]][x[0]]);\n continue;\n }\n g[x[0]][x[1]]=x[2];\n g[x[1]][x[0]]=x[2];\n }\n vector<bool>vis(n,false);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n pq.emplace(0,0);\n while(!pq.empty())\n {\n auto [time,node]=pq.top();\n pq.pop();\n if(vis[node])\n continue;\n vis[node]=true;\n for(auto &x:g[node])\n {\n if(time+x.second<d[x.first]&&time+x.second<ans[x.first])\n {\n ans[x.first]=time+x.second;\n pq.emplace(ans[x.first],x.first);\n }\n }\n }\n for(int i=0;i<n;i++)\n {\n if(ans[i]==INT_MAX)\n ans[i]=-1;\n }\n return ans;\n }\n};", "memory": "266723" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n int k=edges.size();\n vector<vector<pair<int,int>>> adj(n);\n for(int i=0;i<k;i++){\n adj[edges[i][0]].push_back({edges[i][1],edges[i][2]});\n adj[edges[i][1]].push_back({edges[i][0],edges[i][2]});\n }\n\n vector<int> time(n,-1);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n pq.push({0,0});\n unordered_set<int> st;\n while(!pq.empty()){\n pair<int,int> p=pq.top();\n pq.pop();\n int cost=p.first;\n int node=p.second;\n\n if(st.find(node)!=st.end())continue;\n time[node]=cost;\n st.insert(node);\n for(auto it : adj[node]){\n if(st.find(it.first)==st.end() && cost+it.second<disappear[it.first]){\n pq.push({cost+it.second,it.first});\n }\n }\n }\n return time;\n }\n};", "memory": "268274" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n \n unordered_map<int, unordered_map<int, int>> dirs;\n int maxTime = 0;\n for(auto&e :edges){\n #define fr e[0]\n #define to e[1]\n if ( dirs.count(fr) && dirs[fr].count(to) )\n {\n dirs[fr][to] = min(dirs[fr][to], e[2]);\n }\n else\n {\n dirs[fr][to] = e[2]; \n }\n \n if ( dirs.count(to) && dirs[to].count(fr) )\n {\n dirs[to][fr] = min( dirs[to][fr], e[2] );\n }\n else\n {\n dirs[to][fr] = e[2]; \n }\n //cout<< \"dirs[ e[1] ][ e[0] ]\" <<dirs[to][fr] << endl;\n //cout<< \"dirs[ e[0] ][ e[1] ]\" <<dirs[fr][to] << endl;\n }\n maxTime = *max_element(disappear.begin(), disappear.end());\n \n vector<int> ans(n, -1); \n priority_queue< pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> Q;\n Q.push({0, 0});\n while( Q.size() > 0 ){\n auto cur = Q.top(); Q.pop();\n #define curNode cur.second\n #define curTime cur.first\n if (curTime >= maxTime) break;\n \n //cout << curNode << \",\" << curTime << endl;\n if ( ans[curNode] >= 0 ){\n continue;\n }\n if ( curTime >= disappear[curNode] ) continue;\n \n ans[curNode] = curTime;\n for( auto&[nextNode, dTime]: dirs[curNode] ){\n //cout << nextNode << endl;\n if( ans[nextNode] == -1 ){\n //cout << \"Node = \" << nextNode << \", Old time = \" << ans[nextNode] << \", New time = \" <<curTime+dTime <<endl;\n Q.push({curTime+dTime,nextNode});\n }\n }\n }\n return ans;\n }\n};", "memory": "268274" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n typedef pair<int,int> P;\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n unordered_map<int,vector<pair<int,int>>>adj;\n for(auto &edge : edges){\n adj[edge[0]].push_back({edge[1],edge[2]});\n adj[edge[1]].push_back({edge[0],edge[2]});\n }\n vector<int>ans(n, INT_MAX);\n priority_queue<P,vector<P>, greater<P>>pq;\n pq.push({0, 0});\n ans[0] = 0;\n while(!pq.empty()){\n int wt = pq.top().first;\n int u = pq.top().second;\n pq.pop();\n if(ans[u] < wt)continue;//Imp case to avoid TLE\n for(auto &ver : adj[u]){\n int tot = wt + ver.second;\n int v = ver.first;\n if(tot < ans[v] and disappear[v] > tot){\n ans[v] = tot;\n pq.push({tot,v});\n }\n }\n }\n for(auto &it : ans){\n if(it == INT_MAX){\n it = -1;\n }\n }\n return ans;\n }\n};", "memory": "269825" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int V, vector<vector<int>>& edges, vector<int>& t) {\n unordered_map<int, vector<pair<int, int>>> adj;\n\n // Building the adjacency list\n for (auto& edge : edges) {\n int u = edge[0];\n int v = edge[1];\n int wt = edge[2];\n adj[u].push_back({v, wt});\n adj[v].push_back({u, wt});\n }\n\n // Priority queue for Dijkstra's algorithm (min-heap)\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n\n // Distance array initialized to infinity\n vector<int> dist(V, INT_MAX);\n dist[0] = 0;\n\n // Start with the source node (0)\n pq.push({0, 0});\n\n while (!pq.empty()) {\n int time = pq.top().first;\n int u = pq.top().second;\n pq.pop();\n\n // Skip this node if it arrives too late or if we already have a better distance\n if (time > t[u] || time > dist[u]) continue;\n\n // Explore all neighbors\n for (auto& nb : adj[u]) {\n int v = nb.first; \n int wt = nb.second; \n int newTime = time + wt;\n\n // If a shorter path to neighbor v is found and v hasn't disappeared\n if (newTime < dist[v] && newTime < t[v]) { \n dist[v] = newTime;\n pq.push({newTime, v});\n }\n }\n }\n\n // Convert unreachable nodes' distances from INT_MAX to -1\n for (int i = 0; i < V; i++) {\n if (dist[i] == INT_MAX) dist[i] = -1;\n }\n\n return dist;\n }\n};", "memory": "269825" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int,int>>adj[n];\n for(auto it:edges){\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n vector<int>dist(n,1e9);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n pq.push({0,0});\n dist[0]=0;\n while(!pq.empty()){\n auto it=pq.top();\n int dis=it.first;\n int node=it.second;\n\n pq.pop();\n if(dis>dist[node]){\n continue;\n }\n for(auto it:adj[node]){\n if(it.second+dis<dist[it.first]&&it.second+dis<disappear[it.first]){\n dist[it.first]=it.second+dis;\n pq.push({dist[it.first],it.first});\n }\n }\n }\n for(int i=0;i<n;i++){\n if(dist[i]==1e9||dist[i]>=disappear[i]){\n dist[i]=-1;\n }\n }\n return dist;\n }\n};", "memory": "271376" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& dis) {\n vector<pair<int,int>> adj[n];\n for(auto x:edges){\n adj[x[0]].push_back({x[1],x[2]});\n adj[x[1]].push_back({x[0],x[2]});\n }\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q1;\n q1.push({0,0});\n vector<int> ans(n,1e9);\n ans[0]=0;\n while(!q1.empty()){\n int node=q1.top().second;\n int time=q1.top().first;\n q1.pop();\n if(ans[node]<time) continue;\n for(auto x:adj[node]){\n int v=x.first,total=x.second+time;\n if(total<dis[v] && ans[v]>total){\n ans[v]=total;\n q1.push({total,v});\n }\n }\n }\n for(int i=0;i<n;i++) if(ans[i]==1e9) ans[i]=-1;\n return ans;\n }\n};", "memory": "271376" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> dijkstra(int n, unordered_map<int, vector<pair<int,int>>> &adj, vector<int>& disappear){\n vector<int> result(n, -1);\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;\n vector<int> distance(n, INT_MAX);\n pq.push({0,0});\n distance[0]= 0;\n\n while(!pq.empty()){\n int time= pq.top().first;\n int node= pq.top().second;\n pq.pop();\n // cout<<node<<\" \"<<time<<\" \"<<disappear[node]<<endl;\n if(time >= disappear[node] || result[node]!= -1){\n cout<<\"check \"<<node<<endl;\n continue;\n }\n \n result[node]=time;\n\n for(auto &v: adj[node]){\n int next= v.first;\n int t= time + v.second;\n // cout<<v.second<<endl;\n\n if(t < distance[next]){\n // cout<<t<<endl;\n distance[next]= t;\n pq.push({t, next});\n }\n }\n // break;\n }\n return result;\n } \n\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n unordered_map<int, vector<pair<int,int>>> adj;\n for(auto &v: edges){\n adj[v[0]].push_back({v[1], v[2]});\n adj[v[1]].push_back({v[0], v[2]});\n }\n\n return dijkstra(n, adj, disappear);\n }\n};", "memory": "272928" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int,int>> adj[n];\n for(auto a:edges){\n adj[a[0]].push_back({a[1],a[2]});\n adj[a[1]].push_back({a[0],a[2]});\n }\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n vector<int> dis(n,-1);\n pq.push({0,0});\n while(!pq.empty()){\n int t=pq.top().first;\n int x=pq.top().second;\n pq.pop();\n \n if(dis[x]!=-1) continue;\n dis[x]=t;\n for(auto a:adj[x]){\n if(dis[a.first]==-1&&t+a.second<disappear[a.first]){\n pq.push({t+a.second,a.first});\n }\n }\n }\n return dis;\n }\n};", "memory": "272928" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#include <vector>\n#include <queue>\n#include <unordered_set>\n#include <climits>\nusing namespace std;\n\nclass Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n // Build adjacency list\n vector<pair<int, int>> adj[n];\n for (auto it : edges) {\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n \n // Initialize distances and visited array\n vector<int> v(n, 1e9), vis(n, 0);\n unordered_set<int> s;\n v[0] = 0; // Starting node distance is 0\n \n // Min-heap priority queue {distance to reach node, node index}\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;\n q.push({0, 0}); // Push starting node into the priority queue\n \n while (!q.empty()) {\n int node = q.top().second;\n int dis = q.top().first;\n q.pop();\n \n // Skip the node if it's already visited\n if (vis[node]) continue;\n \n // Mark the node as visited and set its distance\n vis[node] = 1;\n v[node] = dis;\n \n // Process all neighbors\n for (auto it : adj[node]) {\n int neighbor = it.first;\n int length = it.second;\n \n // Update distance if the new path is valid and shorter\n if (disappear[neighbor] > length + dis && v[neighbor] > length + dis) {\n q.push({length + dis, neighbor});\n }\n }\n }\n \n // Prepare the result vector, setting unreachable nodes to -1\n for (int i = 0; i < n; i++) {\n if (v[i] == 1e9) v[i] = -1;\n }\n \n return v;\n }\n};\n", "memory": "274479" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>>adj[n];\n for(auto it: edges){\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n vector<int>v(n, 1e9), vis(n, 0);\n unordered_set<int>s;\n v[0]=0;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>q;\n q.push({0, 0});\n while(!q.empty()){\n int node=q.top().second, dis=q.top().first;\n q.pop();\n if(vis[node])continue;\n vis[node]=1;\n v[node]=dis;\n for(auto it: adj[node]){\n if(disappear[it.first]>it.second+dis && v[it.first]>it.second+dis){\n q.push({it.second+dis, it.first});\n }\n }\n }\n for(int i=0; i<n; i++){\n if(v[i]==1e9)v[i]=-1;\n }\n return v;\n }\n};", "memory": "274479" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "// Single Source, Shrotest path problem => Dijikstra Algo:\n// Dijikstra:\nclass Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>> adj[n];\n for (auto it: edges) {\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n\n vector<int> vis(n, 0);\n vector<int> dist(n, -1);\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n\n pq.push({0, 0});\n dist[0] = 0;\n\n while (!pq.empty()) {\n int time = pq.top().first;\n int node = pq.top().second;\n pq.pop();\n\n if (vis[node] || time >= disappear[node]) {\n continue;\n }\n\n vis[node] = 1;\n dist[node] = time;\n\n for (auto it: adj[node]) {\n int nextNode = it.first;\n int edgeTime = it.second;\n\n if (!vis[nextNode]) {\n pq.push({time + edgeTime, nextNode});\n }\n }\n }\n\n return dist;\n }\n};", "memory": "276030" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& e, vector<int>& d) {\n vector<vector<pair<int,int>>> adj(n);\n for(auto it : e){\n int u = it[0];\n int v = it[1];\n int time = it[2];\n adj[u].push_back({v, time});\n adj[v].push_back({u, time});\n }\n \n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n vector<int> dist(n, INT_MAX);\n dist[0] = 0;\n pq.push({0, 0}); // (time, node)\n \n while(!pq.empty()){\n int time = pq.top().first;\n int u = pq.top().second;\n pq.pop();\n \n if(time >= d[u]) continue;\n if(time > dist[u]) continue;\n \n for(auto it : adj[u]){\n int v = it.first;\n int new_time = time + it.second;\n \n if(new_time < dist[v]){\n dist[v] = new_time;\n pq.push({new_time, v});\n }\n }\n }\n \n for(int i = 0; i < n; ++i){\n if(dist[i] >= d[i]) dist[i] = -1;\n }\n \n return dist;\n }\n};\n", "memory": "276030" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pair<int, int>>> G(n);\n for(const auto& edge: edges){\n int a = edge[0], b = edge[1], length = edge[2];\n G[a].push_back({b, length});\n G[b].push_back({a, length});\n }\n vector<int> D(n, -1);\n set<pair<int, int>> pq;\n pq.insert({0, 0});\n while(!pq.empty()){\n auto [time, node] = *pq.begin();\n pq.erase(pq.begin());\n if(D[node] != -1){\n continue;\n }\n D[node] = time;\n for(const auto& [next, length]: G[node]){\n if(D[next] != -1){\n continue;\n }\n long long new_length = time + length;\n if(disappear[next] <= new_length){\n continue;\n }\n pq.insert({new_length, next});\n }\n }\n return D;\n }\n};", "memory": "277581" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<int> result;\n\n vector<vector<pair<int, int>>> adj (n) ;\n for (auto it :edges ){\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n\n }\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q1 ;\n vector<int> mindis (n , INT_MAX);\n q1.push({0,0});\n mindis[0]=0;\n\n while (!q1.empty()){\n pair<int, int> np = q1.top();\n q1.pop();\n int node = np.second;\n int dis = np.first;\n\n if(dis >= disappear[node] || mindis[node]<dis) continue;\n\n for(auto it : adj[node]){\n int ft = dis+it.second;\n int n = it.first;\n if ((ft) < mindis[n] && disappear[n]>(ft)){\n mindis[n]= ft;\n q1.push({ft, n});\n }\n }\n }\n\n for (auto it : mindis){\n result.push_back(it==INT_MAX ? -1 : it);\n }\n\n return result ;\n }\n};", "memory": "277581" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pair<int,int>>>adj(n);\n for(auto it:edges){\n int u = it[0];\n int v = it[1];\n int w = it[2];\n adj[u].push_back({v,w});\n adj[v].push_back({u,w});\n }\n\n vector<int>dist(n,-1),vis(n,0);\n priority_queue< pair<int,int> , vector<pair<int,int>> , greater<pair<int,int>> >pq;\n pq.push({0,0});\n while(!pq.empty()){\n int dis = pq.top().first;\n int node = pq.top().second;\n pq.pop();\n if(vis[node] || dis>=disappear[node]) continue;\n\n dist[node] = dis;\n vis[node] = 1;\n\n for(auto it:adj[node]){\n if(vis[it.first]==0){\n pq.push({dis+it.second,it.first});\n }\n }\n }\n return dist;\n }\n};", "memory": "279133" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<int>ans(n,-1);\n vector<pair<int,int>>v[n];\n for(auto it:edges){\n v[it[0]].push_back({it[1],it[2]});\n v[it[1]].push_back({it[0],it[2]});\n }\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;\n q.push({0,0});\n vector<int>vis(n,0);\n while(!q.empty()){\n auto it=q.top();\n q.pop();\n if(vis[it.second]){\n continue;\n }\n vis[it.second]=1;\n if(disappear[it.second]>it.first){\n ans[it.second]=it.first;\n for(auto it1:v[it.second]){\n q.push({it1.second+it.first,it1.first});\n }\n }\n\n }\n return ans;\n \n }\n};", "memory": "279133" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>>adj[n];\n for(auto it: edges){\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n vector<int>result(n, INT_MAX);\n unordered_set<int>s;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>q;\n result[0] = 0;\n q.push({result[0], 0});\n\n unordered_map<int, bool> visited;\n // vis\n \n while(!q.empty()){\n int node=q.top().second, dis=q.top().first;\n q.pop();\n if(visited.count(node)) \n continue;\n visited[node] = true;\n for(auto &[neighbour, weight]: adj[node]){\n if(result[neighbour] > weight+dis && disappear[neighbour] > weight+dis){\n result[neighbour] = weight+dis;\n q.push({result[neighbour], neighbour});\n }\n }\n }\n for(auto & i : result) {\n if(i==INT_MAX)\n i = -1;\n }\n \n return result;\n }\n};", "memory": "280684" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>>adj[n];\n for(auto it: edges){\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n vector<int>result(n, INT_MAX);\n unordered_set<int>s;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>q;\n result[0] = 0;\n q.push({result[0], 0});\n\n unordered_map<int, bool> visited;\n // vis\n \n while(!q.empty()){\n int node=q.top().second, dis=q.top().first;\n q.pop();\n if(visited.count(node)) \n continue;\n visited[node] = true;\n for(auto &[neighbour, weight]: adj[node]){\n if(result[neighbour] >= weight+dis && disappear[neighbour] > weight+dis){\n result[neighbour] = weight+dis;\n q.push({result[neighbour], neighbour});\n }\n }\n }\n for(auto & i : result) {\n if(i==INT_MAX)\n i = -1;\n }\n \n return result;\n }\n};", "memory": "280684" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>>adj[n];\n for(auto it: edges){\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n vector<int>v(n, -1);\n unordered_set<int>s;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>q;\n q.push({0, 0});\n while(!q.empty()){\n int node=q.top().second, dis=q.top().first;\n q.pop();\n if(s.find(node)!=s.end())continue;\n s.insert(node);\n v[node]=dis;\n for(auto it: adj[node]){\n if(s.find(it.first)==s.end() && disappear[it.first]>it.second+dis){\n q.push({it.second+dis, it.first});\n }\n }\n }\n return v;\n }\n};", "memory": "282235" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>>adj[n];\n for(auto it: edges){\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n vector<int>v(n, -1);\n unordered_set<int>s;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>q;\n q.push({0, 0});\n while(!q.empty()){\n int node=q.top().second, dis=q.top().first;\n q.pop();\n if(s.find(node)!=s.end())continue;\n s.insert(node);\n v[node]=dis;\n for(auto it: adj[node]){\n if(s.find(it.first)==s.end() && disappear[it.first]>it.second+dis){\n q.push({it.second+dis, it.first});\n }\n }\n }\n return v;\n }\n};", "memory": "283786" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>>adj[n];\n for(auto it: edges){\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n vector<int>v(n, -1);\n unordered_set<int>s;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>q;\n q.push({0, 0});\n while(!q.empty()){\n int node=q.top().second, dis=q.top().first;\n q.pop();\n if(s.find(node)!=s.end())continue;\n s.insert(node);\n v[node]=dis;\n for(auto it: adj[node]){\n if(s.find(it.first)==s.end() && disappear[it.first]>it.second+dis){\n q.push({it.second+dis, it.first});\n }\n }\n }\n return v;\n }\n};", "memory": "283786" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>>adj[n];\n for(auto it: edges){\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n vector<int>v(n, -1);\n unordered_set<int>s;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>q;\n q.push({0, 0});\n while(!q.empty()){\n int node=q.top().second, dis=q.top().first;\n q.pop();\n if(s.find(node)!=s.end())continue;\n s.insert(node);\n v[node]=dis;\n for(auto it: adj[node]){\n if(s.find(it.first)==s.end() && disappear[it.first]>it.second+dis){\n q.push({it.second+dis, it.first});\n }\n }\n }\n return v;\n }\n};", "memory": "285338" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>>adj[n];\n for(auto it: edges){\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n vector<int>v(n, -1);\n unordered_set<int>s;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>q;\n q.push({0, 0});\n while(!q.empty()){\n int node=q.top().second, dis=q.top().first;\n q.pop();\n if(s.find(node)!=s.end())continue;\n s.insert(node);\n v[node]=dis;\n for(auto it: adj[node]){\n if(s.find(it.first)==s.end() && disappear[it.first]>it.second+dis){\n q.push({it.second+dis, it.first});\n }\n }\n }\n return v;\n }\n};", "memory": "285338" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& e, vector<int>& v) {\n vector<pair<int, int>> g[n];\n int m = e.size();\n for(auto it : e)\n {\n if(it[0] == it[1])\n continue;\n g[it[0]].push_back({it[1], it[2]});\n g[it[1]].push_back({it[0], it[2]});\n }\n\n vector<int> ans(n, (int)1e9);\n set<pair<int, int>> s;\n s.insert({0, 0});\n ans[0] = 0;\n while(!s.empty())\n {\n int node = (*s.begin()).second;\n s.erase(*s.begin());\n\n for(auto it: g[node])\n {\n int child = it.first;\n int wt = it.second;\n\n if(ans[node] + wt < v[child] && ans[child] > ans[node] + wt)\n {\n if(ans[child] != (int)1e9)\n s.erase({ans[child], child});\n ans[child] = ans[node] + wt;\n s.insert({ans[child], child});\n }\n }\n }\n\n for(auto &x : ans)\n {\n if(x == (int)1e9)\n x = -1;\n }\n return ans;\n }\n};", "memory": "286889" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int,int>> adj[n];\n for(auto it:edges){\n int u = it[0];\n int v = it[1];\n int wt = it[2];\n adj[u].push_back({v,wt});\n adj[v].push_back({u,wt});\n }\n\n vector<int> dist(n,1e9);\n // priority_queue<pair<int,int>, vector<pair<int,int>>, \n // greater<pair<int,int>>> pq;\n set<pair<int,int>> pq;\n pq.insert({0,0});\n dist[0] = 0;\n while(!pq.empty()){\n int node = pq.begin()->second;\n int dis = pq.begin()->first;\n pq.erase(pq.begin());\n\n for(auto it:adj[node]){\n int adjNode = it.first;\n int adjWt = it.second;\n if(dist[node] + adjWt < dist[adjNode] && dist[node] + adjWt < disappear[adjNode]){\n pq.erase({dist[adjNode],adjNode});\n dist[adjNode] = dist[node] + adjWt;\n pq.insert({dist[adjNode],adjNode});\n }\n }\n }\n\n for(int i=0;i<n;i++){\n if(dist[i]==1e9)dist[i] = -1;\n }\n return dist;\n }\n};", "memory": "286889" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#include<queue>\n#include<vector>\nusing namespace std;\nusing int2 = pair<int,int>;\n\nclass Solution {\npublic:\n \n void dijkstara(int node,vector<int>&d,vector<vector<pair<int,int>>>& adj,vector<int>& disappear){ \n priority_queue<int2,vector<int2>,greater<int2>>pq;\n pq.emplace(0,node);\n d[node] = 0; \n unordered_set<int>s;\n\n while(!pq.empty()) { \n auto[time,curr] = pq.top();\n pq.pop();\n\n if(s.find(curr) != s.end()) continue;\n d[curr] = time;\n s.insert(curr);\n \n\n for(auto nbr : adj[curr]) { \n auto[nbr_node,nbr_wt] = nbr;\n if(s.find(nbr_node) == s.end() && disappear[nbr_node] > nbr_wt + time ) {\n pq.emplace(nbr_wt + time,nbr_node);\n }\n }\n }\n }\n \n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pair<int,int>>>adj(n);\n\n for(auto i : edges ){ \n int u = i[0];\n int v = i[1];\n int wt = i[2];\n\n adj[u].push_back({v,wt});\n adj[v].push_back({u,wt});\n }\n\n vector<int>dist(n,-1);\n\n dijkstara(0,dist,adj,disappear);\n\n \n\n return dist;\n }\n};", "memory": "288440" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pair<int, int>>> adj(n);\n for(auto it: edges){\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n vector<int> ans(n, -1);\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n pq.push({0,0});\n\n set<int> s;\n while(!pq.empty()){\n int node=pq.top().second; int dis=pq.top().first;\n pq.pop();\n if(s.find(node)!=s.end()) continue;\n s.insert(node);\n ans[node]=dis;\n\n for(auto it: adj[node]){\n if(s.find(it.first)==s.end() && disappear[it.first]>it.second+dis){\n pq.push({it.second+dis, it.first});\n }\n }\n }\n return ans;\n }\n};", "memory": "288440" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n unordered_map<int, vector<pair<int, int>>> edge;\n for (auto it : edges) {\n int el1 = it[0];\n int el2 = it[1];\n int len = it[2];\n\n edge[el1].push_back({el2, len});\n edge[el2].push_back({el1, len});\n }\n\n // Use a min-heap (priority queue) to store {accumulated_time, node}\n priority_queue<pair<int, int>, vector<pair<int, int>>,\n greater<pair<int, int>>>\n pq;\n vector<int> ans(n, INT_MAX);\n\n // Start from node 0 (assuming node index starts from 0)\n pq.push({0, 0}); // {time, node}\n ans[0] = 0;\n\n while (!pq.empty()) {\n auto [current_time, node] = pq.top();\n pq.pop();\n\n if (current_time > ans[node]) continue;\n\n for (auto [next_node, time_to_next] : edge[node]) {\n int new_time = current_time + time_to_next;\n\n // Check disappear constraint and update if new_time is better\n if (new_time < disappear[next_node] && new_time < ans[next_node]) {\n ans[next_node] = new_time;\n pq.push({new_time, next_node});\n }\n }\n }\n\n // Replace INT_MAX with -1 for unreachable nodes\n for (int i = 0; i < n; i++) {\n if (ans[i] == INT_MAX) {\n ans[i] = -1;\n }\n }\n return ans;\n }\n};", "memory": "289991" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#define ll int\n#define REP(i,a,n)for(int i=a;i<n;i++)\n#define f first\n#define sec second\nclass Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& dis) {\n vector<vector<pair<ll,ll>>>g(n);\n for(auto e:edges)\n {\n g[e[0]].push_back({e[1],e[2]});\n g[e[1]].push_back({e[0],e[2]});\n }\n set<pair<ll,ll>>s;\n s.insert({0,0});\n vector<ll>d(n,1e9);\n d[0]=0;\n while(!s.empty())\n {\n auto it=*s.begin();\n s.erase(s.begin());\n ll v=it.sec;\n ll dist=it.f;\n if(dist>d[v])continue;\n for(auto child:g[v])\n {\n ll u=child.f;\n ll w=child.sec;\n if(d[u]<(d[v]+w))continue;\n if(d[v]+w>=dis[u])continue;\n d[u]=d[v]+w;\n s.insert({d[u],u});\n }\n }\n REP(i,0,n)\n {\n if(d[i]==1e9)d[i]=-1;\n }\n return d;\n }\n};", "memory": "289991" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n typedef pair<int,int> ppi ;\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n // yes djk once more\n unordered_map<int,vector<pair<int,int>>>adj;\n for(auto edge : edges){\n int u = edge[0];\n int v = edge[1];\n int t = edge[2];\n adj[u].push_back({v,t});\n adj[v].push_back({u,t});\n }\n vector<int>dist(n,INT_MAX);\n dist[0] = 0 ;\n priority_queue<ppi,vector<ppi>,greater<ppi>>pq ; \n pq.push({0,0}) ; // time , node;\n\n while(!pq.empty()){\n \n auto p = pq.top();\n pq.pop();\n int node = p.second;\n int time = p.first;\n if(time > dist[node]){\n continue ;\n }\n for(auto vt : adj[node]){\n int v = vt.first ;\n int t = vt.second ; \n // if(time >= disappear[v])continue;\n if(dist [v] > time + t and time+t < disappear[v]){\n dist[v] = time+t;\n pq.push({dist[v],v});\n }\n }\n }\n for(int k : dist){\n cout<<k<<endl;\n }\n vector<int>ans(n,0);\n for(int i=0;i<n;i++){\n ans[i] = dist[i]==INT_MAX ? -1 : dist[i];\n }\n return ans;\n }\n};", "memory": "291543" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n typedef pair<int,int> ppi ;\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n // yes djk once more\n unordered_map<int,vector<pair<int,int>>>adj;\n for(auto edge : edges){\n int u = edge[0];\n int v = edge[1];\n int t = edge[2];\n adj[u].push_back({v,t});\n adj[v].push_back({u,t});\n }\n vector<int>dist(n,INT_MAX);\n dist[0] = 0 ;\n priority_queue<ppi,vector<ppi>,greater<ppi>>pq ; \n pq.push({0,0}) ; // time , node;\n\n while(!pq.empty()){\n \n auto p = pq.top();\n pq.pop();\n int node = p.second;\n int time = p.first;\n if(time > dist[node]){\n continue ;\n }\n for(auto vt : adj[node]){\n int v = vt.first ;\n int t = vt.second ; \n if(time >= disappear[v])continue;\n if(dist [v] > time + t and time+t < disappear[v]){\n dist[v] = time+t;\n pq.push({dist[v],v});\n }\n }\n }\n for(int k : dist){\n cout<<k<<endl;\n }\n vector<int>ans(n,0);\n for(int i=0;i<n;i++){\n ans[i] = dist[i]==INT_MAX ? -1 : dist[i];\n }\n return ans;\n }\n};", "memory": "291543" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n \n // create graph\n unordered_map<int,vector<int>>graph;\n map<pair<int,int>,int>weight;\n for(auto el:edges){\n int u= el[0];\n int v=el[1];\n int wt=el[2];\n if (u == v) {\n continue;\n }\n if(weight.count({u,v})){\n weight[{v,u}]=weight[{u,v}]=min(weight[{u,v}],wt);\n }\n else{\n graph[u].push_back(v);\n graph[v].push_back(u);\n weight[{u,v}]=weight[{v,u}]=wt;\n }\n }\n\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n\n vector<int>ans;\n pq.push({0,0}); //dist,node\n vector<int>dist=vector<int>(n,INT_MAX);\n unordered_map<int,int>vis;\n dist[0]=0;\n while(!pq.empty()){\n\n auto tp=pq.top();\n pq.pop();\n int node=tp.second;\n int dis=tp.first;\n if(vis[node]|| dis>=disappear[node])continue;\n vis[node]=1;\n for(auto child:graph[node]){\n if(dist[node]+weight[{node,child}]<dist[child]){\n dist[child]=dist[node]+weight[{node,child}];\n pq.push({dist[child], child});\n }\n }\n\n }\n for(int i=0;i<dist.size();i++){\n if(dist[i]==INT_MAX||dist[i]>=disappear[i]){\n ans.push_back(-1);\n }else{\n ans.push_back(dist[i]);\n }\n }\n return ans;\n }\n};", "memory": "293094" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n \n // create graph\n map<int,vector<int>>graph;\n map<pair<int,int>,int>weight;\n for(auto el:edges){\n int u= el[0];\n int v=el[1];\n int wt=el[2];\n if (u == v) {\n continue;\n }\n if(weight.count({u,v})){\n weight[{v,u}]=weight[{u,v}]=min(weight[{u,v}],wt);\n \n }\n else{\n graph[u].push_back(v);\n graph[v].push_back(u);\n weight[{u,v}]=weight[{v,u}]=wt;\n }\n }\n\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n\n vector<int>ans;\n pq.push({0,0}); //dist,node\n vector<int>dist=vector<int>(n,INT_MAX);\n unordered_map<int,int>vis;\n dist[0]=0;\n while(!pq.empty()){\n\n auto tp=pq.top();\n pq.pop();\n int node=tp.second;\n int dis=tp.first;\n if(vis[node]|| dis>=disappear[node])continue;\n vis[node]=1;\n for(auto child:graph[node]){\n if(dist[node]+weight[{node,child}]<dist[child]){\n dist[child]=dist[node]+weight[{node,child}];\n pq.push({dist[child], child});\n }\n }\n\n }\n for(int i=0;i<dist.size();i++){\n if(dist[i]==INT_MAX||dist[i]>=disappear[i]){\n ans.push_back(-1);\n }else{\n ans.push_back(dist[i]);\n }\n }\n return ans;\n }\n};", "memory": "294645" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#define ll long long\n\nclass Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<map<int, ll>> adj(n);\n for (auto &it : edges) {\n if (!adj[it[0]][it[1]]) {\n adj[it[0]][it[1]] = it[2];\n } else {\n adj[it[0]][it[1]] = min(adj[it[0]][it[1]], (ll)it[2]);\n }\n if (!adj[it[1]][it[0]]) {\n adj[it[1]][it[0]] = it[2];\n } else {\n adj[it[1]][it[0]] = min(adj[it[1]][it[0]], (ll)it[2]);\n }\n }\n priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;\n vector<int> dist(n, 1e6);\n pq.push({0, 0});\n dist[0] = 0;\n vector<int> vis(n, 0);\n while (pq.size()) {\n int u = pq.top().second;\n pq.pop();\n if (dist[u] >= disappear[u]) continue;\n if(vis[u]) continue;\n vis[u] = 1;\n for (auto &it : adj[u]) {\n int v = it.first;\n if (dist[v] > dist[u] + adj[u][v]) {\n dist[v] = dist[u] + adj[u][v];\n pq.push({dist[v], v});\n }\n }\n }\n for (int i = 0; i < n; i++) {\n if (dist[i] == 1e6 || dist[i] >= disappear[i]) dist[i] = -1;\n }\n return dist;\n }\n};", "memory": "296196" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "typedef pair<int, int> PII;\nclass Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n int m = edges.size(); \n vector<vector<PII>> ad(n);\n\n for(int i=0; i<m; i++){\n int x = edges[i][0]; \n int y = edges[i][1]; \n int d = edges[i][2]; \n ad[x].push_back({y, d});\n ad[y].push_back({x, d});\n }\n\n set<PII> st; \n st.insert({0, 0}); \n vector<int> ans(n, INT_MAX);\n\n while(st.size()>0){\n PII fr = *st.begin();\n st.erase(st.begin());\n int d = fr.first; \n int x = fr.second; \n\n if(ans[x] != INT_MAX) continue; \n\n if(disappear[x] <= d) {\n ans[x] = -1;\n continue; \n }\n\n ans[x] = d; \n\n for(int i=0; i<ad[x].size(); i++){\n int y = ad[x][i].first; \n int w = ad[x][i].second;\n if(disappear[y] > d+w)\n st.insert({d+w, y});\n }\n }\n\n for(int i=0; i<ans.size(); i++){\n if(ans[i] == INT_MAX){\n ans[i] = -1; \n }\n }\n\n return ans; \n }\n};", "memory": "297748" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#define ll long long int\n#define pi pair<int,ll>\n#define mp make_pair\nclass Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pi>> adj(n);\n for (auto edge : edges) {\n int x = edge[0];\n int y = edge[1];\n int z = edge[2];\n\n adj[x].emplace_back(y, z);\n adj[y].emplace_back(x, z);\n\n }\n\n vector<int> dis(n, 1e9);\n priority_queue<pi, vector<pi>, greater<pi>> pq;\n pq.push(make_pair(0, 0));\n dis[0] = 0;\n vector<bool> vis(n,false);\n\n while (!pq.empty()) {\n int u = pq.top().second;\n int d = pq.top().first;\n pq.pop();\n if(vis[u]) continue;\n vis[u] = true;\n for (auto &neighbor : adj[u]) {\n int v = neighbor.first;\n int weight = neighbor.second;\n ll cd = dis[u] + weight;\n if (disappear[v] > cd && cd < dis[v]) {\n dis[v] = cd;\n pq.push(make_pair(dis[v], v));\n }\n }\n }\n\n for(int i=0;i<n;++i){\n if(dis[i] == 1e9){\n dis[i] = -1;\n }\n }\n return dis;\n }\n};", "memory": "297748" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n \n int szEdges = edges.size();\n vector<pair<int, int>> adj[50000];\n {\n unordered_map<int, unordered_map<int, int>> mp;\n mp.reserve(n);\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n int p = edges[i][2];\n if (u > v) swap(u, v);\n if (mp[u].find(v) == mp[u].end()) mp[u][v] = p;\n mp[u][v] = min(mp[u][v], p);\n }\n int cnt[50000] = {0};\n for (int i = 0; i < n; i++)\n {\n if (mp.find(i) == mp.end()) continue;\n for (auto it : mp[i])\n {\n int v = it.first;\n cnt[i]++; cnt[v]++;\n }\n }\n for (int i = 0; i < n; i++) \n adj[i].reserve(cnt[i]);\n for (int i = 0; i < n; i++)\n {\n if (mp.find(i) == mp.end()) continue;\n for (auto it : mp[i])\n {\n int v = it.first;\n int p = it.second;\n adj[i].push_back({v, p});\n adj[v].push_back({i, p});\n }\n }\n }\n\n /*\n {\n int cnt[50000] = {0};\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n cnt[u]++; cnt[v]++;\n }\n for (int i = 0; i < n; i++) \n adj[i].reserve(cnt[i]);\n }\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n int p = edges[i][2];\n adj[u].push_back({v, p});\n adj[v].push_back({u, p});\n }*/\n \n int dp[50000]; dp[0] = 0;\n for (int i = 1; i < n; i++) dp[i] = INT_MAX;\n\n priority_queue<pair<int, int>> heap;\n heap.push({0, 0});\n while (!heap.empty())\n {\n int u = heap.top().second;\n int cur = -heap.top().first;\n heap.pop();\n\n if (cur > dp[u]) continue;\n if (dp[u] >= disappear[u]) continue;\n\n int l = adj[u].size();\n /*\n for (auto it : mp[u])\n {\n int v = it.first;\n int p = it.second;\n int update = dp[u] + p;\n if (update >= dp[v]) continue;\n dp[v] = update;\n heap.push({-dp[v], v});\n }*/\n for (int i = 0; i < l; i++)\n {\n int v = adj[u][i].first;\n int p = adj[u][i].second;\n int update = dp[u] + p;\n if (update >= dp[v]) continue;\n dp[v] = update;\n heap.push({-dp[v], v});\n }\n }\n\n vector<int> ans(n);\n for (int i = 0; i < n; i++) \n {\n bool ch = dp[i] == INT_MAX || dp[i] >= disappear[i];\n ans[i] = ch ? -1 : dp[i];\n }\n return ans;\n }\n};", "memory": "299299" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n unordered_map<int,vector<vector<int>>> graph;\n for (const auto& edge : edges) {\n graph[edge[0]].emplace_back(vector<int>{edge[1],edge[2]});\n graph[edge[1]].emplace_back(vector<int>{edge[0],edge[2]});\n }\n\n vector<int> result(n, -1);\n\n unordered_map<int,pair<int,bool>> shortest;\n for (int i=0; i<n; ++i) {\n shortest[i] = { numeric_limits<int>::max(), false };\n }\n\n int found = 0;\n const auto cmp = [](const auto& e1, const auto& e2) {\n return e1.second > e2.second;\n };\n priority_queue<pair<int,int>,vector<pair<int,int>>,decltype(cmp)> q(cmp);\n q.emplace(0, 0);\n while (!q.empty()) {\n const auto [cur, time] = q.top();\n q.pop();\n\n if (shortest[cur].second) {\n continue;\n }\n \n if (time >= disappear[cur]) {\n graph.erase(cur);\n continue;\n }\n\n shortest[cur].first = time;\n shortest[cur].second = true;\n result[cur] = time;\n\n ++found;\n if (found == n) {\n break;\n }\n\n for (const auto& neighbour : graph[cur]) {\n if (time+neighbour[1] < disappear[neighbour[0]] && time+neighbour[1] < shortest[neighbour[0]].first) {\n shortest[neighbour[0]].first = time + neighbour[1];\n q.emplace(neighbour[0], shortest[neighbour[0]].first);\n }\n }\n }\n\n return result;\n }\n};", "memory": "300850" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n unordered_map<int,vector<vector<int>>> graph;\n for (const auto& edge : edges) {\n graph[edge[0]].emplace_back(vector<int>{edge[1],edge[2]});\n graph[edge[1]].emplace_back(vector<int>{edge[0],edge[2]});\n }\n\n vector<int> result(n, -1);\n\n unordered_map<int,pair<int,bool>> shortest;\n for (int i=0; i<n; ++i) {\n shortest[i] = { numeric_limits<int>::max(), false };\n }\n\n int found = 0;\n const auto cmp = [](const auto& e1, const auto& e2) {\n return e1.second > e2.second;\n };\n priority_queue<pair<int,int>,vector<pair<int,int>>,decltype(cmp)> q(cmp);\n q.emplace(0, 0);\n while (!q.empty()) {\n const auto [cur, time] = q.top();\n q.pop();\n\n if (shortest[cur].second) {\n continue;\n }\n \n if (time >= disappear[cur]) {\n graph.erase(cur);\n continue;\n }\n\n shortest[cur].first = time;\n shortest[cur].second = true;\n result[cur] = time;\n\n ++found;\n if (found == n) {\n break;\n }\n\n for (const auto& neighbour : graph[cur]) {\n if (time+neighbour[1] < disappear[neighbour[0]] && time+neighbour[1] < shortest[neighbour[0]].first) {\n shortest[neighbour[0]].first = time + neighbour[1];\n q.emplace(neighbour[0], shortest[neighbour[0]].first);\n }\n }\n }\n\n return result;\n }\n};", "memory": "300850" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n unordered_map<int,vector<vector<int>>> graph;\n for (const auto& edge : edges) {\n graph[edge[0]].emplace_back(vector<int>{edge[1],edge[2]});\n graph[edge[1]].emplace_back(vector<int>{edge[0],edge[2]});\n }\n\n vector<int> result(n, -1);\n\n unordered_map<int,pair<int,bool>> shortest;\n for (int i=0; i<n; ++i) {\n shortest[i] = { numeric_limits<int>::max(), false };\n }\n\n int found = 0;\n const auto cmp = [](const auto& e1, const auto& e2) {\n return e1.second > e2.second;\n };\n priority_queue<pair<int,int>,vector<pair<int,int>>,decltype(cmp)> q(cmp);\n q.emplace(0, 0);\n while (!q.empty()) {\n const auto [cur, time] = q.top();\n q.pop();\n\n if (shortest[cur].second) {\n continue;\n }\n \n if (time >= disappear[cur]) {\n graph.erase(cur);\n continue;\n }\n\n shortest[cur].first = time;\n shortest[cur].second = true;\n result[cur] = time;\n\n ++found;\n if (found == n) {\n break;\n }\n\n for (const auto& neighbour : graph[cur]) {\n if (time+neighbour[1] < disappear[neighbour[0]] && time+neighbour[1] < shortest[neighbour[0]].first) {\n shortest[neighbour[0]].first = time + neighbour[1];\n q.emplace(neighbour[0], shortest[neighbour[0]].first);\n }\n }\n }\n\n return result;\n }\n};", "memory": "302401" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n unordered_map<int,vector<vector<int>>> graph;\n for (const auto& edge : edges) {\n graph[edge[0]].emplace_back(vector<int>{edge[1],edge[2]});\n graph[edge[1]].emplace_back(vector<int>{edge[0],edge[2]});\n }\n\n vector<int> result(n, -1);\n\n unordered_map<int,pair<int,bool>> shortest;\n for (int i=0; i<n; ++i) {\n shortest[i] = { numeric_limits<int>::max(), false };\n }\n\n int found = 0;\n const auto cmp = [](const auto& e1, const auto& e2) {\n return e1.second > e2.second;\n };\n priority_queue<pair<int,int>,vector<pair<int,int>>,decltype(cmp)> q(cmp);\n q.emplace(0, 0);\n while (!q.empty()) {\n const auto [cur, time] = q.top();\n q.pop();\n\n if (shortest[cur].second) {\n continue;\n }\n \n if (time >= disappear[cur]) {\n graph.erase(cur);\n continue;\n }\n\n shortest[cur].first = time;\n shortest[cur].second = true;\n result[cur] = time;\n\n ++found;\n if (found == n) {\n break;\n }\n\n for (const auto& neighbour : graph[cur]) {\n if (time+neighbour[1] < disappear[neighbour[0]] && time+neighbour[1] < shortest[neighbour[0]].first) {\n shortest[neighbour[0]].first = time + neighbour[1];\n q.emplace(neighbour[0], shortest[neighbour[0]].first);\n }\n }\n }\n\n return result;\n }\n};", "memory": "302401" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n pq.push({0,0});\n vector<vector<vector<int>>> graph(n);\n for(int i=0;i<edges.size();i++){\n int u=edges[i][0];\n int v=edges[i][1];\n int l=edges[i][2];\n graph[u].push_back({v,l});\n graph[v].push_back({u,l});\n }\n vector<int> ans(n,INT_MAX);\n ans[0]=0;\n while(!pq.empty()){\n pair<int,int> p=pq.top();pq.pop();\n int d=p.first;\n int u=p.second;\n if (d > ans[u] )\n continue;\n for(auto i:graph[u]){\n int v=i[0];\n int l=i[1];\n if(d+l<ans[v]&&d+l<disappear[v]){\n ans[v]=d+l;\n pq.push({d+l,v});\n } \n }\n \n }\n for(int i=0;i<n;i++){\n if(ans[i]==INT_MAX)\n ans[i]=-1;\n }\n return ans;\n \n \n \n }\n};", "memory": "303953" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n pq.push({0,0});\n vector<vector<vector<int>>> graph(n);\n for(int i=0;i<edges.size();i++){\n int u=edges[i][0];\n int v=edges[i][1];\n int l=edges[i][2];\n graph[u].push_back({v,l});\n graph[v].push_back({u,l});\n }\n vector<int> ans(n,INT_MAX);\n ans[0]=0;\n while(!pq.empty()){\n pair<int,int> p=pq.top();pq.pop();\n int d=p.first;\n int u=p.second;\n if (d > ans[u] )\n continue;\n for(auto i:graph[u]){\n int v=i[0];\n int l=i[1];\n if(d+l<ans[v]&&d+l<disappear[v]){\n ans[v]=d+l;\n pq.push({d+l,v});\n } \n }\n \n }\n for(int i=0;i<n;i++){\n if(ans[i]==INT_MAX)\n ans[i]=-1;\n }\n return ans;\n \n \n \n }\n};", "memory": "303953" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n \n unordered_map<int, unordered_map<int, int>> dirs;\n for(auto&e :edges){\n if ( dirs[ e[0] ][ e[1] ] == 0){ dirs[ e[0] ][ e[1] ] = INT_MAX; }\n \n if ( dirs[ e[1] ][ e[0] ] == 0){ dirs[ e[1] ][ e[0] ] = INT_MAX; } \n dirs[ e[0] ][ e[1] ] = min(dirs[ e[0] ][ e[1] ], e[2]);\n dirs[ e[1] ][ e[0] ] = min(dirs[ e[1] ][ e[0] ], e[2]);\n }\n \n vector<int> ans(n, -1); \n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> Q;\n Q.push({0, 0});\n while( Q.size() > 0 ){\n auto cur = Q.top(); Q.pop();\n int curNode = cur[1];\n int curTime = cur[0];\n \n //cout << curNode << \",\" << curTime << endl;\n if ( ans[curNode] >= 0 && ans[curNode] <= curTime){\n continue;\n }\n if ( curTime >= disappear[curNode] ) continue;\n \n ans[curNode] = curTime;\n for( auto&[nextNode, dTime]: dirs[curNode] ){\n //cout << nextNode << endl;\n if( ans[nextNode] == -1 || ans[nextNode] > curTime+dTime){\n //cout << \"Node = \" << nextNode << \", Old time = \" << ans[nextNode] << \", New time = \" <<curTime+dTime <<endl;\n Q.push({curTime+dTime,nextNode});\n }\n }\n }\n return ans;\n }\n};", "memory": "308606" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n \n unordered_map<int, unordered_map<int, int>> dirs;\n for(auto&e :edges){\n if ( dirs[ e[0] ][ e[1] ] == 0){ dirs[ e[0] ][ e[1] ] = INT_MAX; }\n \n if ( dirs[ e[1] ][ e[0] ] == 0){ dirs[ e[1] ][ e[0] ] = INT_MAX; } \n dirs[ e[0] ][ e[1] ] = min(dirs[ e[0] ][ e[1] ], e[2]);\n dirs[ e[1] ][ e[0] ] = min(dirs[ e[1] ][ e[0] ], e[2]);\n }\n \n vector<int> ans(n, -1); \n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> Q;\n Q.push({0, 0});\n while( Q.size() > 0 ){\n auto cur = Q.top(); Q.pop();\n int curNode = cur[1];\n int curTime = cur[0];\n \n //cout << curNode << \",\" << curTime << endl;\n if ( ans[curNode] >= 0 && ans[curNode] <= curTime){\n continue;\n }\n if ( curTime >= disappear[curNode] ) continue;\n \n ans[curNode] = curTime;\n for( auto&[nextNode, dTime]: dirs[curNode] ){\n //cout << nextNode << endl;\n if( ans[nextNode] == -1 ){\n //cout << \"Node = \" << nextNode << \", Old time = \" << ans[nextNode] << \", New time = \" <<curTime+dTime <<endl;\n Q.push({curTime+dTime,nextNode});\n }\n }\n }\n return ans;\n }\n};", "memory": "308606" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& ed, vector<int>& da) {\n vector<int> dis(n+1,(int)1e9),ans(n,0);ans[0]=0;\n vector<unordered_map<int,int>> v(n);\n for(int i=0;i<ed.size();i++){\n if(v[ed[i][0]].count(ed[i][1])){\n v[ed[i][0]][ed[i][1]]=min(v[ed[i][0]][ed[i][1]],ed[i][2]);\n v[ed[i][1]][ed[i][0]]=min(v[ed[i][1]][ed[i][0]],ed[i][2]);\n continue;\n }\n v[ed[i][0]][ed[i][1]]=ed[i][2];\n v[ed[i][1]][ed[i][0]]=ed[i][2];\n }\n vector<int> vis(n,0);\n priority_queue<pair<int,int>> q;\n dis[0]=0;\n q.push({0,0});\n while(!q.empty()){\n auto it = q.top();q.pop();\n int c=it.first,a=it.second;\n if(vis[a])continue;\n // cout<<\"for \"<<a<<\" \"<<c<<endl;\n ans[a]=-c;\n for(auto e:v[a]){\n if(vis[e.first])continue;\n // cout<<e.first<<\" \"<<e.second<<endl;\n dis[e.first] = min(dis[e.first],-it.first+e.second);\n if(dis[e.first]<da[e.first])q.push({-dis[e.first],e.first});\n else ans[e.first]=-1;\n }\n vis[a]=1;\n }\n for(int i=0;i<n;i++){\n if(ans[i]==-1)continue;\n if(dis[i]==1e9){ans[i]=-1;continue;}\n ans[i]=dis[i];\n }\n return ans;\n }\n};", "memory": "310158" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n unordered_map<int, vector<pair<int, int>>> adj;\n for (auto e : edges) {\n adj[e[0]].push_back({e[1], e[2]});\n adj[e[1]].push_back({e[0], e[2]});\n }\n\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;\n pq.push({0, 0}); // {time, node}\n vector<int> ans(n, -1);\n vector<bool> vis(n,false);\n ans[0]=0;\n while (!pq.empty()) {\n auto p = pq.top();\n pq.pop();\n int t = p[0];\n int node = p[1];\n if(vis[node]||t >= disappear[node]) continue;\n vis[node]=true;\n for (auto v : adj[node]) {\n int nextNode = v.first;\n int nextTime = v.second + t;\n if (nextTime >= disappear[nextNode]) continue;\n if (ans[nextNode] == -1 || ans[nextNode] > nextTime) {\n ans[nextNode] = nextTime;\n pq.push({nextTime, nextNode});\n }\n }\n }\n\n return ans;\n }\n};\n", "memory": "311709" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& e, vector<int>& dis) {\n set<pair<int,int>> pq;\n\n vector<vector<vector<int>>> adj(n);\n\n for(int i=0; i<e.size(); i++ ){\n adj[e[i][0]].push_back({e[i][1], e[i][2]});\n adj[e[i][1]].push_back({e[i][0], e[i][2]});\n }\n\n pq.insert(make_pair(0,0));\n\n vector<int> dist(n,INT_MAX);\n dist[0] = 0;\n\n while(pq.size()){\n auto topEl = *(pq.begin());\n int u = topEl.second;\n int d = topEl.first;\n pq.erase(topEl);\n for(auto it: adj[u]){\n int w = it[1];\n int v = it[0];\n\n if( (dist[v] > dist[u] + w) && (dis[v] > dist[u] + w) ) {\n\n if(dist[u] != INT_MAX) {\n pq.erase({dist[v], v});\n }\n dist[v] = dist[u] + w;\n pq.insert(make_pair(dist[v], v));\n }\n }\n }\n for(int i=0; i<dist.size(); i++){\n if(dist[i] == INT_MAX){\n dist[i] = -1;\n }\n }\n return dist;\n }\n};\n\n", "memory": "313260" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n vector<int> dis(n,1e9);\n dis[0]=0;\n vector<vector<int>> adj[n];\n for(auto x:edges){\n if(x[0]==x[1]) continue;\n adj[x[0]].push_back({x[1],x[2]});\n adj[x[1]].push_back({x[0],x[2]});\n }\n pq.push({dis[0],0});\n while(pq.size()){\n auto top=pq.top();\n pq.pop();\n int node=top.second;\n int d=top.first;\n if(d>dis[node]) {\n continue;\n }\n for(auto x:adj[node]){\n if(dis[x[0]]>dis[node]+x[1] and dis[node]+x[1]<disappear[x[0]]){\n dis[x[0]]=dis[node]+x[1];\n pq.push({dis[x[0]],x[0]});\n }\n }\n }\n for(auto &x:dis){\n if(x==1e9) x=-1;\n }\n return dis;\n }\n};", "memory": "314811" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& e, vector<int>& disappear) {\n vector<vector<int>>adj[n];\n for(auto it:e){\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n vector<int>dis(n,INT_MAX);\n dis[0]=0;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n pq.push({0,0});\n \n while(!pq.empty()){\n int node=pq.top().second;\n int currdis=pq.top().first;\n pq.pop();\n if(dis[node]<currdis) continue;\n for(auto it:adj[node]){\n int v=it[0],wt=it[1];\n if(currdis+wt<disappear[v] and currdis+wt<dis[v]){\n dis[v]=currdis+wt;\n pq.push({dis[v],v});\n }\n \n }\n }\n for(int i=0;i<n;i++){\n if(dis[i]==INT_MAX) dis[i]=-1;\n }\n return dis;\n }\n};", "memory": "316363" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<int> dist(n,1e9);\n dist[0]=0;\n vector<vector<int>> adj[n];\n for(auto it:edges)\n {\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n pq.push({0,0});\n while(!pq.empty())\n {\n auto it=pq.top();\n pq.pop();\n int node=it.second;\n int w=it.first;\n if(w>dist[node])continue;\n for(auto t:adj[node])\n {\n int wt=t[1];\n if(w+wt<dist[t[0]] && w+wt<disappear[t[0]] && dist[node]<=disappear[node])\n {\n dist[t[0]]=w+wt;\n pq.push({w+wt,t[0]});\n }\n \n \n \n }\n }\n for(int i=0;i<dist.size();i++)\n {\n if(dist[i]==1e9)dist[i]=-1;\n }\n\n return dist;\n }\n};", "memory": "317914" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n\n vector<vector<int>> adj[n];\n for(auto it: edges){\n int u = it[0];\n int v = it[1];\n int wt = it[2];\n adj[u].push_back({v,wt});\n adj[v].push_back({u,wt});\n }\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;\n vector<int> dist(n,1e9);\n dist[0]=0;\n q.push({0,0});\n while(!q.empty()){\n int node = q.top().second;\n int dis = q.top().first;\n q.pop();\n if(dist[node] < dis) continue;\n if(disappear[node] <= dis){\n // dist[node] = -1;\n continue;\n } \n for(auto it: adj[node]){\n int nbr = it[0];\n int wt = it[1];\n if(dist[nbr] > dis + wt){\n dist[nbr] = dis + wt;\n // if(disappear[nbr] <= dist[nbr]) dist[nbr] = -1;\n q.push({dist[nbr],nbr});\n }\n }\n }\n for(int i = 0 ; i < n ; i++){\n if(dist[i] == 1e9 || disappear[i] <= dist[i]) dist[i] = -1;\n }\n return dist;\n\n }\n};", "memory": "317914" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n \n int szEdges = edges.size();\n vector<pair<int, int>> adj[50000];\n\n unordered_map<int, unordered_map<int, int>> mp;\n mp.reserve(n);\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n int p = edges[i][2];\n if (u > v) swap(u, v);\n if (mp[u].find(v) == mp[u].end()) mp[u][v] = p;\n mp[u][v] = min(mp[u][v], p);\n }\n for (int i = 0; i < n; i++)\n {\n if (mp.find(i) == mp.end()) continue;\n for (auto it : mp[i])\n {\n int v = it.first;\n int p = it.second;\n adj[i].push_back({v, p});\n adj[v].push_back({i, p});\n }\n }\n \n\n /*\n {\n int cnt[50000] = {0};\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n cnt[u]++; cnt[v]++;\n }\n for (int i = 0; i < n; i++) \n adj[i].reserve(cnt[i]);\n }\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n int p = edges[i][2];\n adj[u].push_back({v, p});\n adj[v].push_back({u, p});\n }*/\n \n int dp[50000]; dp[0] = 0;\n for (int i = 1; i < n; i++) dp[i] = INT_MAX;\n\n priority_queue<pair<int, int>> heap;\n heap.push({0, 0});\n while (!heap.empty())\n {\n int u = heap.top().second;\n int cur = -heap.top().first;\n heap.pop();\n\n if (cur > dp[u]) continue;\n if (dp[u] >= disappear[u]) continue;\n\n int l = adj[u].size();\n /*\n for (auto it : mp[u])\n {\n int v = it.first;\n int p = it.second;\n int update = dp[u] + p;\n if (update >= dp[v]) continue;\n dp[v] = update;\n heap.push({-dp[v], v});\n }*/\n for (int i = 0; i < l; i++)\n {\n int v = adj[u][i].first;\n int p = adj[u][i].second;\n int update = dp[u] + p;\n if (update >= dp[v]) continue;\n dp[v] = update;\n heap.push({-dp[v], v});\n }\n }\n\n vector<int> ans(n);\n for (int i = 0; i < n; i++) \n {\n bool ch = dp[i] == INT_MAX || dp[i] >= disappear[i];\n ans[i] = ch ? -1 : dp[i];\n }\n return ans;\n }\n};", "memory": "319465" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n \n int szEdges = edges.size();\n vector<pair<int, int>> adj[50000];\n {\n unordered_map<int, unordered_map<int, int>> mp;\n mp.reserve(n);\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n int p = edges[i][2];\n if (u > v) swap(u, v);\n if (mp[u].find(v) == mp[u].end()) mp[u][v] = p;\n mp[u][v] = min(mp[u][v], p);\n }\n for (int i = 0; i < n; i++)\n {\n if (mp.find(i) == mp.end()) continue;\n for (auto it : mp[i])\n {\n int v = it.first;\n int p = it.second;\n adj[i].push_back({v, p});\n adj[v].push_back({i, p});\n }\n }\n }\n\n /*\n {\n int cnt[50000] = {0};\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n cnt[u]++; cnt[v]++;\n }\n for (int i = 0; i < n; i++) \n adj[i].reserve(cnt[i]);\n }\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n int p = edges[i][2];\n adj[u].push_back({v, p});\n adj[v].push_back({u, p});\n }*/\n \n int dp[50000]; dp[0] = 0;\n for (int i = 1; i < n; i++) dp[i] = INT_MAX;\n\n priority_queue<pair<int, int>> heap;\n heap.push({0, 0});\n while (!heap.empty())\n {\n int u = heap.top().second;\n int cur = -heap.top().first;\n heap.pop();\n\n if (cur > dp[u]) continue;\n if (dp[u] >= disappear[u]) continue;\n\n int l = adj[u].size();\n /*\n for (auto it : mp[u])\n {\n int v = it.first;\n int p = it.second;\n int update = dp[u] + p;\n if (update >= dp[v]) continue;\n dp[v] = update;\n heap.push({-dp[v], v});\n }*/\n for (int i = 0; i < l; i++)\n {\n int v = adj[u][i].first;\n int p = adj[u][i].second;\n int update = dp[u] + p;\n if (update >= dp[v]) continue;\n dp[v] = update;\n heap.push({-dp[v], v});\n }\n }\n\n vector<int> ans(n);\n for (int i = 0; i < n; i++) \n {\n bool ch = dp[i] == INT_MAX || dp[i] >= disappear[i];\n ans[i] = ch ? -1 : dp[i];\n }\n return ans;\n }\n};", "memory": "321016" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n // Dijkstra's algorithm with priority given to time\n unordered_map<int, list<pair<int, int>>> adj;\n for (auto it : edges) {\n int u = it[0];\n int v = it[1];\n int wt = it[2];\n adj[u].push_back({v, wt});\n adj[v].push_back({u, wt});\n }\n\n // To store the minimum time to reach each node\n vector<int> ans(n, -1); // Initialize with -1\n unordered_map<int, bool> visited; // Track visited nodes\n\n // Priority queue (min-heap) for Dijkstra\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;\n q.push({0, 0}); // Start with node 0 and distance 0\n\n while (!q.empty()) {\n int dis = q.top().first; // Current distance\n int node = q.top().second; // Current node\n q.pop();\n\n if (visited[node]) continue;\n\n // Mark node as visited\n visited[node] = true;\n ans[node] = dis;\n\n // Process all neighbors\n for (auto it : adj[node]) {\n int neighbor = it.first;\n int weight = it.second;\n\n // If the neighbor is not visited and we can reach it before disappear\n if (!visited[neighbor] && dis + weight < disappear[neighbor]) {\n q.push({dis + weight, neighbor});\n } else if (!visited[neighbor] && dis + weight > disappear[neighbor]) {\n ans[neighbor] = -1; // Mark as unreachable\n }\n }\n }\n\n return ans;\n }\n};\n", "memory": "322568" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> shortestPath(unordered_map<int,set<pair<int,int>>> &adj,int n,int S,vector<int>& disappear){\n //base cases\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n pq.push({0,S});\n unordered_map<int,bool> vis;\n vector<int> dist(n,INT_MAX);\n dist[0]=0;\n while(!pq.empty()){\n //\n pair<int,int> p=pq.top();\n pq.pop();\n int currNode=p.second;\n int currDist=p.first;\n if(vis[currNode]){\n continue;\n }\n vis[currNode]=true;\n for(auto nbr:adj[currNode]){\n int v=nbr.first;\n int weight=nbr.second;\n if(vis[v] || disappear[v]<=currDist+weight){\n continue;\n }\n if(currDist+weight < dist[v]){\n dist[v]=currDist+weight;\n pq.push({dist[v],v});\n }\n }\n }\n return dist;\n }\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n unordered_map<int,set<pair<int,int>>> adj;\n for(int i=0;i<edges.size();i++){\n int u=edges[i][0];\n int v=edges[i][1];\n int w=edges[i][2];\n adj[u].insert({v,w});\n adj[v].insert({u,w});\n }\n vector<int> dist=shortestPath(adj,n,0,disappear);\n for(int i=0;i<n;i++){\n cout<<dist[i]<<\" \";\n }\n cout<<endl;\n for(int i=0;i<n;i++){\n if(disappear[i]<=dist[i]){\n dist[i]=-1;\n }\n }\n return dist;\n }\n};", "memory": "322568" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\nstatic const int N = 1e5+1;\nvector<pair<int,int>>adj[N];\n\n\n\nvoid f(int node,int n,vector<int>& disappear,vector<int>&dist)\n{\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n pq.push({0,0});\n dist[0] =0;\n while(!pq.empty())\n {\n auto p = pq.top();\n pq.pop();\n int cdis = p.first,u=p.second;\n if(cdis >= disappear[u] || dist[u]<cdis) continue;\n for(auto &it1 : adj[u])\n {\n int v = it1.first,w =it1.second;\n if(dist[v]>dist[u]+w && disappear[v]>dist[u]+w)\n {\n dist[v] = dist[u]+w;\n pq.push({dist[v],v});\n }\n }\n }\n}\n\nvector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) \n{\n vector<int>dist(N,INT_MAX);\n for(auto &it : edges)\n {\n int u = it[0],v = it[1],w=it[2];\n adj[u].push_back({v,w});\n adj[v].push_back({u,w});\n }\n f(0,n,disappear,dist);\n vector<int>ans;\n for(int i=0;i<n;i++)\n {\n cout<<dist[i]<<\" \";\n if(dist[i]!=INT_MAX)\n {\n ans.push_back(dist[i]);\n }\n else{\n ans.push_back(-1);\n }\n }\n return ans;\n}\n};", "memory": "324119" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector< vector< pair<int,int> > > graph(n,vector<pair<int,int>>());\n map<pair<int,int>,int> mp;\n for(int i = 0; i < edges.size(); i++){\n if(edges[i][0] == edges[i][1]) continue;\n if(mp.find({edges[i][0],edges[i][1]}) != mp.end()){\n if(mp[{edges[i][0],edges[i][1]}] > edges[i][2]){\n mp[{edges[i][0],edges[i][1]}] = edges[i][2];\n mp[{edges[i][1],edges[i][0]}] = edges[i][2];\n }\n }\n else{\n mp[{edges[i][0],edges[i][1]}] = edges[i][2];\n mp[{edges[i][1],edges[i][0]}] = edges[i][2];\n }\n }\n\n for(auto it = mp.begin(); it != mp.end(); it++){\n int u = (*it).first.first;\n int v = (*it).first.second;\n int wt = (*it).second;\n graph[u].push_back({v,wt});\n }\n\n if(graph.size() == 0){\n vector<int> ans;\n ans.push_back(0);\n for(int i = 1; i < n; i++) ans.push_back(-1);\n return ans;\n }\n priority_queue< pair<int,int>, vector< pair<int,int> >, greater< pair<int,int> > > pq;\n pq.push({0,0});\n \n vector<int> distance(n,INT_MAX);\n distance[0] = 0;\n\n vector<int> vis(n,0);\n\n while(not pq.empty()){\n int node = pq.top().second;\n int dist = pq.top().first;\n pq.pop();\n\n if(vis[node]) continue;\n else vis[node] = 1;\n\n for(auto neighbour : graph[node]){\n int neighbourNode = neighbour.first;\n int nodeToNeighbour = neighbour.second;\n\n if(vis[neighbourNode]) continue;\n\n if(distance[neighbourNode] > dist + nodeToNeighbour){\n distance[neighbourNode] = dist + nodeToNeighbour;\n }\n\n if(distance[neighbourNode] >= disappear[neighbourNode]){\n distance[neighbourNode] = INT_MAX;\n }\n else{\n pq.push({distance[neighbourNode],neighbourNode});\n }\n }\n }\n\n for(int i = 0; i < distance.size(); i++){\n if(distance[i] == INT_MAX) distance[i] = -1;\n }\n return distance;\n }\n};", "memory": "325670" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n //vector<pair<int, int>> adj[50000];\n unordered_map<int, unordered_map<int, int>> mp;\n\n int szEdges = edges.size();\n mp.reserve(n);\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n int p = edges[i][2];\n if (mp[u].find(v) == mp[u].end()) mp[u][v] = p;\n if (mp[v].find(u) == mp[v].end()) mp[v][u] = p;\n mp[u][v] = mp[v][u] = min(mp[u][v], p);\n }/*\n {\n int cnt[50000] = {0};\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n cnt[u]++; cnt[v]++;\n }\n for (int i = 0; i < n; i++) \n adj[i].reserve(cnt[i]);\n }\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n int p = edges[i][2];\n adj[u].push_back({v, p});\n adj[v].push_back({u, p});\n }*/\n \n int dp[50000]; dp[0] = 0;\n for (int i = 1; i < n; i++) dp[i] = INT_MAX;\n\n priority_queue<pair<int, int>> heap;\n heap.push({0, 0});\n while (!heap.empty())\n {\n int u = heap.top().second;\n int cur = -heap.top().first;\n heap.pop();\n\n if (cur > dp[u]) continue;\n if (dp[u] >= disappear[u]) continue;\n\n //int l = adj[u].size();\n for (auto it : mp[u])\n {\n int v = it.first;\n int p = it.second;\n int update = dp[u] + p;\n if (update >= dp[v]) continue;\n dp[v] = update;\n heap.push({-dp[v], v});\n }/*\n for (int i = 0; i < l; i++)\n {\n int v = adj[u][i].first;\n int p = adj[u][i].second;\n int update = dp[u] + p;\n if (update >= dp[v]) continue;\n dp[v] = update;\n heap.push({-dp[v], v});\n }*/\n }\n\n vector<int> ans(n);\n for (int i = 0; i < n; i++) \n {\n bool ch = dp[i] == INT_MAX || dp[i] >= disappear[i];\n ans[i] = ch ? -1 : dp[i];\n }\n return ans;\n }\n};", "memory": "325670" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& e, vector<int>& ds) {\n vector<vector<int>>adj[n];\n for(auto it:e){\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n\n\n \n vector<int>dis(n,1e9);\n dis[0]=0;\n set<pair<int,int>> st;\n st.insert({0,0});\n \n while(!st.empty()){\n auto it=*(st.begin());\n st.erase(it);\n int d=it.first;\n int node=it.second;\n \n for(auto ptr:adj[node]){\n int w=ptr[1];\n int child=ptr[0];\n if(d+w<dis[child] && d+w<ds[child]){\n st.erase({dis[child],child});\n dis[child]=d+w;\n st.insert({d+w,child});\n }\n \n }\n \n }\n for(int i=0;i<n;i++){\n if(dis[i]>ds[i]) dis[i]=-1;\n }\n return dis;\n }\n};", "memory": "327221" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<int>> adj[n];\n for(auto it:edges){\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n } \n set<pair<int,int>> pq;\n pq.insert({0,0});\n vector<int> dis(n,1e9);\n dis[0] = 0;\n while (!pq.empty())\n {\n auto node = (*pq.begin()).second;\n int dist = (*pq.begin()).first;\n pq.erase(pq.begin());\n for(auto it:adj[node]){\n if(disappear[it[0]] > dist + it[1]){\n if(dis[it[0]] > dist + it[1]){\n \n if(dis[it[0]] != 1e9) \n pq.erase({dis[it[0]], it[0]}); \n\n dis[it[0]] = dist + it[1];\n pq.insert({dis[it[0]],it[0]});\n }\n }\n }\n }\n for(auto i = 0;i<n;i++){\n if(dis[i] >= 1e9){\n dis[i] = -1;\n }\n }\n return dis;\n }\n};", "memory": "328773" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<int>> adj[n];\n for(auto it:edges){\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n } \n set<pair<int,int>> pq;\n pq.insert({0,0});\n vector<int> dis(n,1e9);\n dis[0] = 0;\n while (!pq.empty())\n {\n auto node = (*pq.begin()).second;\n int dist = (*pq.begin()).first;\n pq.erase(pq.begin());\n for(auto it:adj[node]){\n if(disappear[it[0]] > dist + it[1]){\n if(dis[it[0]] > dist + it[1]){\n \n if(dis[it[0]] != 1e9) \n pq.erase({dis[it[0]], it[0]}); \n\n dis[it[0]] = dist + it[1];\n pq.insert({dis[it[0]],it[0]});\n }\n }\n }\n }\n for(auto i = 0;i<n;i++){\n if(dis[i] >= 1e9){\n dis[i] = -1;\n }\n }\n return dis;\n }\n};", "memory": "328773" }
3,389
<p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p> <p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won&#39;t be able to visit it.</p> <p><strong>Note</strong>&nbsp;that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p> <p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is our starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won&#39;t be able to visit it.</li> <li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p> <p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p> <ul> <li>For node 0, we don&#39;t need any time as it is the starting point.</li> <li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li> <li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Exactly when we reach node 1, it disappears.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li> <li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>5</sup></code></li> <li><code>disappear.length == n</code></li> <li><code>1 &lt;= disappear[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<vector<int>>> graph(n);\n for (auto &e : edges) {\n graph[e[0]].push_back({e[1], e[2]});\n graph[e[1]].push_back({e[0], e[2]});\n }\n\n priority_queue<vector<int>> pq;\n pq.push({0, 0});\n\n vector<int> ans(n, -1);\n\n while (!pq.empty()) {\n auto &top = pq.top();\n\n int curNode = top[1];\n int distance = -top[0];\n pq.pop();\n\n\n if (ans[curNode] != -1) continue;\n\n if (distance < disappear[curNode]) {\n ans[curNode] = distance;\n\n for (auto &e : graph[curNode]) {\n pq.push({-(distance + e[1]), e[0]});\n }\n } else {\n ans[curNode] = -1;\n }\n }\n\n return ans;\n }\n};", "memory": "330324" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "//m0\n//O(n)\n#define tii tuple<int,int>\nstatic auto _ = [](){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size();\n long long ans = 0;\n\n //cnt := the number as the right endpoint, the count of such good subarrays.\n //no overflow\n //132 strictly decreasing monotonic stack.\n stack<tii> monosta_cnt;\n for(const int& x: nums){\n long long cur_cnt = 0;\n\n while(monosta_cnt.size() && !( get<0>( monosta_cnt.top() ) > x ) ){\n auto [last_val, last_cnt] = monosta_cnt.top();\n if(last_val == x){\n cur_cnt = 1 + last_cnt;\n }\n\n monosta_cnt.pop();\n }\n\n monosta_cnt.push(tii(x, cur_cnt));\n ans += cur_cnt;\n }\n return ans + n;\n }\n};", "memory": "111762" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n stack<pair<int,int>> s;\n long long res = 0;\n for (const int n : nums) {\n while (!s.empty() && s.top().first < n) {\n s.pop();\n }\n if (s.empty() || s.top().first != n) {\n s.emplace(n, 0);\n }\n s.top().second++;\n res += s.top().second;\n }\n return res;\n }\n};", "memory": "111762" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "using ll = long long;\nusing pll = pair<ll, ll>;\n\nint speedup = []{\n ios::sync_with_stdio(0); cin.tie(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n\n long long numberOfSubarrays(vector<int>& nums) {\n ll ans = 0;\n\n stack<pll> st; // {value, cnt}\n for (ll i = 0; i < nums.size(); i++) {\n while (!st.empty() && st.top().first < nums[i]) st.pop();\n if (!st.empty() && st.top().first == nums[i]) st.top().second++;\n else st.push({nums[i], 1});\n\n ans += st.top().second;\n }\n\n return ans;\n }\n};", "memory": "113687" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "using ll = long long;\nusing pll = pair<ll, ll>;\n\nclass Solution {\npublic:\n\n long long numberOfSubarrays(vector<int>& nums) {\n ll ans = 0;\n\n stack<pll> st; // {value, cnt}\n for (ll i = 0; i < nums.size(); i++) {\n while (!st.empty() && st.top().first < nums[i]) st.pop();\n if (!st.empty() && st.top().first == nums[i]) st.top().second++;\n else st.push({nums[i], 1});\n\n ans += st.top().second;\n }\n\n return ans;\n }\n};", "memory": "113687" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n \n stack<pair<int,int>>st;\n int n = nums.size();\n long long ans= 0;\n for(int i = n-1 ; i>=0 ;i--){\n\n while(!st.empty() && st.top().first < nums[i]){\n st.pop();\n }\n \n if(st.empty()){\n st.push({nums[i],1});\n }else{\n if(st.top().first == nums[i]){\n st.push({nums[i] ,st.top().second+1});\n }else{\n st.push({nums[i] ,1});\n }\n }\n ans +=st.top().second;\n }\n return ans;\n }\n};", "memory": "115612" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n vector<int>cnt(nums.size(),1);\n long long ans=0;\n stack<int>st;\n for(int i=0;i<nums.size();i++){\n while(!st.empty() and nums[st.top()]<nums[i]){\n st.pop();\n }\n if(!st.empty()){\n if(nums[i]==nums[st.top()]){\n cnt[i]+=cnt[st.top()];\n }\n }\n ans+=cnt[i];\n st.push(i);\n\n }\n return ans;\n }\n};", "memory": "115612" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\n\nusing namespace std;\n\n// Store positive values only\nclass SegmentTreeArrayMaximum {\n private:\n int maximum; // always equals to 2^m for some m\n vector<int> arr;\n const vector<int> &source;\n int size;\n\n int _getStandard(int start, int end) {\n int length = end - start;\n\n if (length == 1) {\n return start < size ? source[start] : 0;\n } else {\n int index = maximum / length - 1 + start / length;\n if (arr[index] < 0) {\n int middle = (start + end) / 2;\n arr[index] = max(_getStandard(start, middle), _getStandard(middle, end));\n }\n return arr[index];\n }\n }\n\n public:\n SegmentTreeArrayMaximum(const vector<int> &source): source(source) {\n size = source.size();\n maximum = pow(2, ceil(log2(size + 1)));\n\n arr = vector<int>(maximum, -1);\n }\n\n int get(int start, int end) {\n // [start, end)\n int multipleOf = 2;\n int maximum = 0;\n\n while (true) {\n int closestStart = ((start + multipleOf - 1) / multipleOf) * multipleOf;\n int closestEnd = (end / multipleOf) * multipleOf;\n\n if (closestStart != start) {\n // cout << \"Asking [\" << start << \", \" << closestStart << \")\" << endl;\n maximum = max(_getStandard(start, closestStart), maximum);\n }\n if (closestEnd != end) {\n // cout << \"Asking [\" << closestEnd << \", \" << end << \")\" << endl;\n maximum = max(_getStandard(closestEnd, end), maximum);\n }\n\n start = closestStart;\n end = closestEnd;\n\n if (start >= end) {\n break;\n }\n\n multipleOf *= 2;\n }\n\n return maximum;\n }\n};\n\nclass Solution {\n public:\n long long numberOfSubarrays(vector<int>& nums) {\n SegmentTreeArrayMaximum maximumFinder(nums);\n\n int n = nums.size();\n vector<int> sortedIndices(n, 0);\n \n // Fill with indices from 0 to n-1\n for (int i=0; i<n; i++) {\n sortedIndices[i] = i;\n }\n\n // Sorted by indices\n sort(sortedIndices.begin(), sortedIndices.end(), [&] (const int &lhs, const int &rhs) {\n return nums[lhs] < nums[rhs] || (nums[lhs] == nums[rhs] && lhs < rhs);\n });\n\n // Find the neighbors that has the same values\n long long answer = 0;\n int currentValue = 0, valueStart = -1, valueEnd = -1;\n for (int i=0; i<n; i++) {\n if (nums[sortedIndices[i]] != currentValue) {\n currentValue = nums[sortedIndices[i]];\n valueStart = i;\n }\n\n valueEnd = i;\n if (i == n-1 || nums[sortedIndices[i + 1]] != currentValue) {\n // It is the last one\n answer += numberOfSubarraysGivenMax(\n maximumFinder, sortedIndices, valueStart, valueEnd, currentValue\n );\n }\n }\n\n return answer;\n }\n \n private:\n long long numberOfSubarraysGivenMax(\n SegmentTreeArrayMaximum &maximumFinder, const vector<int> &sortedIndices, int start, int end, int currentValue\n ) {\n // end: inclusive\n long long answer = 0;\n\n long long consecutiveIntervals = 0;\n for (int i=start; i<end; i++) {\n int intervalMaximum = maximumFinder.get(sortedIndices[i], sortedIndices[i + 1]);\n\n // cout << \"Maximum in [\" << sortedIndices[start] << \", \" << sortedIndices[start + 1] << \") is \" << intervalMaximum << endl;\n\n if (intervalMaximum <= currentValue) {\n consecutiveIntervals += 1;\n } else {\n answer += (consecutiveIntervals + 1) * consecutiveIntervals / 2;\n consecutiveIntervals = 0;\n }\n }\n\n answer += (consecutiveIntervals + 1) * consecutiveIntervals / 2;\n\n answer += end - start + 1;\n\n // cout << currentValue << \" \" << answer << endl;\n\n return answer;\n }\n};", "memory": "117537" }