question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
ipo
Golang | Priority Queue
golang-priority-queue-by-yfw13-2mw8
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n- golang implementation
tw13
NORMAL
2023-02-23T00:59:31.859210+00:00
2023-02-23T00:59:31.859246+00:00
542
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- golang implementation of official solution https://leetcode.com/problems/ipo/solutions/2959870/ipo/?orderBy=most_votes\n\n\n# Complexity\n- Time complexity: $$O(nlog...
3
0
['Go']
1
ipo
✅easiest-soln | Goldman Sachs🔥| well-explained👁️
easiest-soln-goldman-sachs-well-explaine-mfqy
If you found my answer helpful, please consider giving it an upvote\uD83D\uDE0A\nReviseWithArsh #6Companies30Days Challenge 2023\nChallenge Company 2 : Goldman
nandini-gangrade
NORMAL
2023-01-11T13:50:55.694704+00:00
2023-01-11T13:50:55.694741+00:00
623
false
## If you found my answer helpful, please consider giving it an upvote\uD83D\uDE0A\n**ReviseWithArsh #6Companies30Days Challenge 2023\nChallenge Company 2 : Goldman Sachs\nQ9. IPO**\n\n### Approach\n\n![9.jpg](https://assets.leetcode.com/users/images/fd117b53-eb1f-4648-8c3a-e523a971f0bc_1673445011.7832413.jpeg)\n\n\n##...
3
0
['C++']
0
ipo
Priority-Queue||C++
priority-queuec-by-ivr_turbo-e64y
\nclass Solution {\npublic:\n int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {\n priority_queue<int>pq; // top ele
ivr_turbo
NORMAL
2021-01-09T20:06:44.559795+00:00
2021-01-09T20:08:30.812207+00:00
411
false
```\nclass Solution {\npublic:\n int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {\n priority_queue<int>pq; // top element = max profit among all projects with Capital<=W; \n vector<pair<int,int>>v; // to store index of projects\n for(int i = 0;i<Capital.size()...
3
0
['C', 'Heap (Priority Queue)']
2
ipo
greedy solution using 2 priority queue
greedy-solution-using-2-priority-queue-b-bq1j
\n//pair defined\ntypedef pair<int,int> p;\nclass Solution {\npublic:\n int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {\
mohit_chau
NORMAL
2020-09-23T20:25:06.154937+00:00
2020-09-23T20:25:06.154987+00:00
402
false
```\n//pair defined\ntypedef pair<int,int> p;\nclass Solution {\npublic:\n int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {\n\t //create minHeap to sort on the basis of capital\n priority_queue<p, vector<p>, greater<p>> minHeap;\n\t\t//create maxHeap to sort on the basis...
3
0
['C', 'Heap (Priority Queue)', 'C++']
1
ipo
Python Greedy
python-greedy-by-wangqiuc-x4d5
The heuristic is, for each investment, we choose the largest profit from current doable projects, where doable means project\'s requested capital is no higher t
wangqiuc
NORMAL
2019-04-15T03:11:50.193395+00:00
2019-07-26T18:32:06.141196+00:00
393
false
The heuristic is, for each investment, we choose the largest profit from current doable projects, where doable means project\'s requested capital is no higher than current ```W```. \nAnd our ```W``` should keep increasing as capital doesn\'t decrease when we invest. Therefore, a greedy search can solve the problem by o...
3
0
[]
0
ipo
Sorting + One O(k)-size Priority Queue Solution
sorting-one-ok-size-priority-queue-solut-d7lc
There are many excellent solutions based on two priority queue solution, however, we do not need to maintain two priority queues that contain all projects. \n I
wangxinbo
NORMAL
2017-02-15T22:49:03.314000+00:00
2017-02-15T22:49:03.314000+00:00
1,757
false
There are many excellent solutions based on two priority queue solution, however, we do not need to maintain two priority queues that contain all projects. \n* If we sort the Capital in increasing order, we can insert "doable" project into the pq until we meet an "undoable" project. \n\n* We need only one priority queu...
3
1
[]
2
ipo
KOTLIN and JAVA solution using 2 PriorityQueues
kotlin-and-java-solution-using-2-priorit-zuyd
\u2618\uFE0F\u2618\uFE0F\u2618\uFE0F If this solution was helpful, please consider upvoting! \u2705\n\n# Intuition\nTo maximize capital, we should always select
anlk
NORMAL
2024-11-15T21:58:51.308775+00:00
2024-11-15T21:58:51.308805+00:00
20
false
\u2618\uFE0F\u2618\uFE0F\u2618\uFE0F If this solution was helpful, please consider upvoting! \u2705\n\n# Intuition\nTo maximize capital, we should always select the most profitable project that we can afford with the current capital. \n\nThis approach combines sorting by capital (to ensure we only pick feasible project...
2
0
['Array', 'Greedy', 'Sorting', 'Heap (Priority Queue)', 'Java', 'Kotlin']
0
ipo
Easy and Right approach with details
easy-and-right-approach-with-details-by-zqeam
Intuition\nYou are given n projects where the ith project has a pure profit profits[i] and a minimum capital of capital[i] is needed to start it.\n\nInitially,
anand_shukla1312
NORMAL
2024-06-15T07:00:24.807943+00:00
2024-06-15T07:00:24.807965+00:00
418
false
# Intuition\nYou are given n projects where the ith project has a pure profit profits[i] and a minimum capital of capital[i] is needed to start it.\n\nInitially, you have w capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital.\n\nPick a list of at most k...
2
0
['Sorting', 'Heap (Priority Queue)', 'Iterator', 'Python3']
2
ipo
Easy Explained Solution with comments||C++||Java
easy-explained-solution-with-commentscja-5do8
Approach\n here are the key points explaining the code:\n\nClass and Comparator Function:\n\nThe Solution class contains the logic to maximize capital after sel
Ashutosh_Kumar_0506
NORMAL
2024-06-15T06:22:29.564549+00:00
2024-06-15T06:22:29.564577+00:00
206
false
# Approach\n here are the key points explaining the code:\n\nClass and Comparator Function:\n\nThe Solution class contains the logic to maximize capital after selecting projects.\nA static comparator function cmp sorts pairs of capital and profits.\nSorts primarily by ascending capital.\nIf capitals are equal, sorts by...
2
0
['Array', 'Greedy', 'Sorting', 'Heap (Priority Queue)', 'C++']
1
ipo
Easy to understand | 💯 Fast | maxHeap | Sorting 🔥 🔥 🔥
easy-to-understand-fast-maxheap-sorting-giygq
Intuition\nThe intuition behind this code is to maximize the available capital after selecting up to k projects, by strategically choosing the projects with the
deleted_user
NORMAL
2024-06-15T05:16:11.933790+00:00
2024-06-15T05:16:11.933825+00:00
11
false
# Intuition\nThe intuition behind this code is to maximize the available capital after selecting up to k projects, by strategically choosing the projects with the highest profit that can be started within the current capital constraints. It does this by sorting projects by their capital requirements to quickly find the...
2
0
['Python3']
0
ipo
Easy to understand c++ solution using priority_queue && sorting
easy-to-understand-c-solution-using-prio-0lxy
Intuition\n Describe your first thoughts on how to solve this problem. \npush the elements in array in sorted order till it is less than current ans then pick t
prathameshmg0205
NORMAL
2024-06-15T05:12:39.178883+00:00
2024-06-15T05:12:39.178924+00:00
185
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\npush the elements in array in sorted order till it is less than current ans then pick the top elements till it does not become greater than top element\nbreak according to condition\n\n# Approach\n<!-- Describe your approach to solving th...
2
0
['C++']
1
ipo
Easy Segment Tree Solution :)
easy-segment-tree-solution-by-user20222-eapo
Code\n\nclass SegmentTree{\n vector<int>tree;\n public:\n vector<int>nodes;\n SegmentTree(vector<int>v){\n int n = v.size();\n nodes =
user20222
NORMAL
2024-06-15T05:07:39.831978+00:00
2024-06-15T05:07:39.832010+00:00
243
false
# Code\n```\nclass SegmentTree{\n vector<int>tree;\n public:\n vector<int>nodes;\n SegmentTree(vector<int>v){\n int n = v.size();\n nodes = v;\n tree.resize(4*n);\n build(1,0,n-1);\n }\n int build(int node,int l,int r){\n if(l==r){\n tree[node] = l;\n ...
2
0
['Segment Tree', 'C++']
0
ipo
Easy to Understand | Beats 99%
easy-to-understand-beats-99-by-gameboey-pm9z
Intuition\nUsing max heap for profits we can easily maximize our capital after k projects.\n\n# Approach\n- Create pairs of profits[i] and capital[i].`\n- Sort
gameboey
NORMAL
2024-06-15T01:45:41.926742+00:00
2024-06-15T01:45:41.926765+00:00
677
false
# Intuition\nUsing max heap for `profits` we can easily maximize our capital after k projects.\n\n# Approach\n- Create pairs of profits[i] and capital[i].`\n- Sort pairs by required capital.\n- MaxHeap: While iterating:\n1. Push profits of affordable projects into a max heap.\n2. Pop and add top profit to capital.\n- R...
2
0
['C++', 'Java', 'Go', 'Python3', 'JavaScript']
2
ipo
[Swift] built-in Heap
swift-built-in-heap-by-iamhands0me-5rmp
\n\n# Code\n\nimport Collections\n\nclass Solution {\n func findMaximizedCapital(_ k: Int, _ w: Int, _ profits: [Int], _ capital: [Int]) -> Int {\n va
iamhands0me
NORMAL
2024-06-15T01:29:55.790249+00:00
2024-06-15T01:29:55.790269+00:00
76
false
\n\n# Code\n```\nimport Collections\n\nclass Solution {\n func findMaximizedCapital(_ k: Int, _ w: Int, _ profits: [Int], _ capital: [Int]) -> Int {\n var projects = zip(profits, capital).map(Project.init).sorted { $0.capital > $1.capital }\n var hp: Heap<Project> = []\n var currentCapital = w\n...
2
0
['Swift', 'Sorting', 'Heap (Priority Queue)']
0
ipo
Swift💯
swift-by-upvotethispls-80mg
Greedy (accepted answer)\n\nimport Collections\n\nclass Solution {\n func findMaximizedCapital(_ k: Int, _ w: Int, _ profits: [Int], _ capital: [Int]) -> Int
UpvoteThisPls
NORMAL
2024-06-15T01:06:10.015422+00:00
2024-06-15T01:18:56.205248+00:00
78
false
**Greedy (accepted answer)**\n```\nimport Collections\n\nclass Solution {\n func findMaximizedCapital(_ k: Int, _ w: Int, _ profits: [Int], _ capital: [Int]) -> Int {\n var indices = capital.indices.sorted {capital[$0] > capital[$1]}\n var heap = Heap<Int>()\n\n return (0 ..< k).reduce(into: w) ...
2
0
['Swift']
0
ipo
Dart using PriorityQueues
dart-using-priorityqueues-by-dartist-o4jh
Code\n\nimport \'package:collection/collection.dart\';\n\ntypedef proCap = ({int capital, int profit});\n\nclass Solution {\n int findMaximizedCapital(int k, i
Dartist
NORMAL
2024-05-21T16:28:19.717211+00:00
2024-05-21T16:30:15.344684+00:00
14
false
# Code\n```\nimport \'package:collection/collection.dart\';\n\ntypedef proCap = ({int capital, int profit});\n\nclass Solution {\n int findMaximizedCapital(int k, int w, List<int> profits, List<int> capital) {\n final pqCap = PriorityQueue<proCap>((a, b) => a.capital - b.capital);\n final pqPro = PriorityQueue<p...
2
0
['Dart']
0
ipo
SIMPLEST SOLUTION
simplest-solution-by-hcmus-hqhuy-bfnc
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\nUse a pointer to test
HCMUS-HQHuy
NORMAL
2024-04-20T09:29:25.718939+00:00
2024-04-20T09:29:25.718972+00:00
167
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nUse a pointer to test what you can push when you have capital equals w, because w always increases so p increase(sorted array).\n\n# Complexity\n- Time complexity: $...
2
0
['C++']
3
ipo
[Java] Easy solution using max heap
java-easy-solution-using-max-heap-by-ytc-zmm7
java\nclass Solution {\n public int findMaximizedCapital(int k, int w, final int[] profits, final int[] capital) {\n final int n = profits.length;\n
YTchouar
NORMAL
2024-04-01T04:55:13.923454+00:00
2024-04-01T04:55:13.923491+00:00
587
false
```java\nclass Solution {\n public int findMaximizedCapital(int k, int w, final int[] profits, final int[] capital) {\n final int n = profits.length;\n final int[][] projects = new int[n][2];\n\n for(int i = 0; i < n; ++i) {\n projects[i][0] = capital[i];\n projects[i][1] =...
2
0
['Java']
1
ipo
3D Dynamic Programming || C++
3d-dynamic-programming-c-by-dalwadiharsh-j67q
Intuition\n Describe your first thoughts on how to solve this problem. \nThere is more suitable ways through using heap, but tried using 3d DP and it worked, bu
dalwadiharsh09
NORMAL
2023-12-22T21:46:41.757384+00:00
2023-12-22T21:46:41.757414+00:00
299
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThere is more suitable ways through using heap, but tried using 3d DP and it worked, but has exceeded memory problem for large test cases.\n\n# Code\n```\nclass Solution {\npublic:\n int f(int index, int w, int k, vector<int>& c, vecto...
2
0
['Array', 'Dynamic Programming', 'C++']
4
ipo
Best Java Solution || Priority Queue || Sorting
best-java-solution-priority-queue-sortin-m4b2
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
ravikumar50
NORMAL
2023-12-22T19:05:22.618610+00:00
2023-12-22T19:05:22.618638+00:00
413
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
0
['Java']
0
ipo
PriorityQueue Optimize Solution
priorityqueue-optimize-solution-by-shree-ekwf
Complexity\n- Time complexity:O(N)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(N*2)\n Add your space complexity here, e.g. O(n) \n\n# Co
Shree_Govind_Jee
NORMAL
2023-10-30T13:37:10.897681+00:00
2023-10-30T13:37:10.897704+00:00
17
false
# Complexity\n- Time complexity:O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N*2)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {\n int [][] proj = ne...
2
0
['Array', 'Greedy', 'Sorting', 'Heap (Priority Queue)', 'Java']
1
ipo
O(klogn)
oklogn-by-pathaksanjeev38-o686
\n\n# Code\n\nclass Solution:\n def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:\n maxProfit = [] # onl
pathaksanjeev38
NORMAL
2023-08-29T19:31:35.718185+00:00
2023-08-29T19:31:35.718203+00:00
111
false
\n\n# Code\n```\nclass Solution:\n def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:\n maxProfit = [] # only projects we can afford\n minCapital = [(c,p) for c,p in zip(capital,profits)]\n heapq.heapify(minCapital) # sort on the bases of capital\n\n...
2
0
['Heap (Priority Queue)', 'Python3']
0
ipo
Easy C++|| Heap || Clean code
easy-c-heap-clean-code-by-harshit_patel2-nd0f
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Harshit_Patel2002
NORMAL
2023-06-14T06:34:49.120399+00:00
2023-06-14T06:34:49.120437+00:00
56
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
0
['C++']
2
reorder-routes-to-make-all-paths-lead-to-the-city-zero
C++/Java Track Direction
cjava-track-direction-by-votrubac-5om0
Based on the problem description, we have a tree, and node zero is the root. \n\nHowever, the direction can point either from a parent to a child (positive), or
votrubac
NORMAL
2020-05-31T04:02:03.530046+00:00
2020-06-02T01:51:02.009096+00:00
37,864
false
Based on the problem description, we have a tree, and node zero is the root. \n\nHowever, the direction can point either from a parent to a child (positive), or from a child to its parent (negative). To solve the problem, we traverse the tree and count edges that are directed from a parent to a child. Direction of thos...
410
13
[]
49
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Simple Explanation | DFS with edge deletion | Idea | Code | Comments | Q & A
simple-explanation-dfs-with-edge-deletio-1xg0
Key Idea\nThe base case that we know is - All outgoing edges from 0 must be reversed. So add the count into our final result. \nAt this stage, we can say all ed
interviewrecipes
NORMAL
2020-05-31T04:03:09.879086+00:00
2020-06-05T03:51:45.972667+00:00
22,935
false
**Key Idea**\nThe base case that we know is - All outgoing edges from 0 must be reversed. So add the count into our final result. \nAt this stage, we can say all edges are coming into node 0. What is the next thing we need to do?\nMake sure that all nodes that are connected to 0 have all edges coming into them.\nE.g. i...
224
9
['Depth-First Search']
17
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[Python3] Easy Short DFS
python3-easy-short-dfs-by-localhostghost-1xbb
Start from node 0 (the capital) and dfs on the path and see if the path is \nin the same direction as the traversal. If it is on the same direction that \nmeans
localhostghost
NORMAL
2020-05-31T04:06:50.800461+00:00
2020-09-06T17:06:27.779992+00:00
14,758
false
Start from `node 0` (the capital) and dfs on the path and see if the path is \nin the same direction as the traversal. If it is on the same direction that \nmeans we need to reverse it because it can never get to the capital.\n\n```\nclass Solution:\n def minReorder(self, n: int, connections: List[List[int]]) -> int...
186
9
[]
20
reorder-routes-to-make-all-paths-lead-to-the-city-zero
straightforward c++ solution || bfs
straightforward-c-solution-bfs-by-helloi-b1uj
We put outward facing edges in one vector, and keep the reverse in another. \n\nStarting from the city, we switch edges that are facing away from us. \n\nIf t
helloisabelle
NORMAL
2020-09-04T00:05:29.028218+00:00
2020-09-04T17:32:03.952627+00:00
9,670
false
We put outward facing edges in one vector, and keep the reverse in another. \n\nStarting from the city, we switch edges that are facing away from us. \n\nIf there is a node that faces inward to us that we haven\'t visited yet, it would be in our back vector. \nWe need to add inward facing nodes to the queue as well, ...
159
3
['Breadth-First Search', 'C']
16
reorder-routes-to-make-all-paths-lead-to-the-city-zero
🔥Easy Solutions with Exaplanation in Java 📝, Python 🐍, and C++ 🖥️🧐Look at once 💻
easy-solutions-with-exaplanation-in-java-h49p
Intuition\nWe can use DFS to traverse the tree and change the direction of edges if needed.\n\n# Approach\nFirst, we create an adjacency list to represent the t
Vikas-Pathak-123
NORMAL
2023-03-24T00:51:45.613216+00:00
2023-03-24T01:39:55.522181+00:00
21,789
false
# Intuition\nWe can use DFS to traverse the tree and change the direction of edges if needed.\n\n# Approach\nFirst, we create an adjacency list to represent the tree. Each node in the list contains a list of its neighbors.\n\nTo change the direction of edges, we assign a direction to each edge. If an edge goes from nod...
152
8
['Depth-First Search', 'Graph', 'Python', 'C++', 'Java']
12
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Java Simple BFS from Origin
java-simple-bfs-from-origin-by-hobiter-c0cm
Could be solved by DFS or BFS. BFS is easy to debug, and not causing stackoverflow.\n\n public int minReorder(int n, int[][] cs) {\n Set<String> st =
hobiter
NORMAL
2020-05-31T04:02:25.643790+00:00
2020-05-31T18:12:35.037268+00:00
9,532
false
Could be solved by DFS or BFS. BFS is easy to debug, and not causing stackoverflow.\n```\n public int minReorder(int n, int[][] cs) {\n Set<String> st = new HashSet<>();\n Map<Integer, Set<Integer>> map = new HashMap<>();\n for (int[] c : cs) {\n st.add(c[0] + "," + c[1]);\n ...
77
9
[]
11
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Image Explanation🏆- [Complete Intuition - DFS] - C++/Java/Python
image-explanation-complete-intuition-dfs-5fuh
Video Solution (Aryan Mittal)\nReorder Routes to Make All Paths Lead to the City Zero by Aryan Mittal\n\n\n\n\n# Approach & Intution\n\n\n\n\n\n\n\n\n\n# Code\n
aryan_0077
NORMAL
2023-03-24T02:10:25.595152+00:00
2023-03-24T05:18:46.387219+00:00
12,451
false
# Video Solution (`Aryan Mittal`)\n`Reorder Routes to Make All Paths Lead to the City Zero` by `Aryan Mittal`\n![meta4.png](https://assets.leetcode.com/users/images/30b858e7-6f23-4721-bce4-c3df73a6037a_1679635121.8728836.png)\n\n\n\n# Approach & Intution\n![image.png](https://assets.leetcode.com/users/images/6f7886fc-f...
73
2
['Depth-First Search', 'Graph', 'Python', 'C++', 'Java']
7
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Easy C++ DFS solution || commented
easy-c-dfs-solution-commented-by-saiteja-yo98
\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n \n //for every city store the adjacent city along wit
saiteja_balla0413
NORMAL
2021-07-02T06:34:43.457944+00:00
2021-07-02T06:34:43.457983+00:00
4,056
false
```\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n \n //for every city store the adjacent city along with direction \n //to store the direction we use positive indicating a road from a to b for a\n //we use negative indicating there is a road from...
61
1
['C']
7
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[Python3] Simple BFS solution with explanation
python3-simple-bfs-solution-with-explana-rdiw
The key idea is to recognize that the given graph is directed and it may not be possible to visit every node starting from 0. In order to traverse the graph, we
redsand
NORMAL
2020-10-07T11:31:20.525502+00:00
2020-10-11T13:31:46.037804+00:00
3,087
false
The key idea is to recognize that the given graph is directed and it may not be possible to visit every node starting from 0. In order to traverse the graph, we need to make sure that every node is reachable from 0.\n\nSo turn the graph into an undirected one while noting down the original direction. Here, I have used ...
61
0
['Breadth-First Search', 'Graph', 'Python']
7
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[Python] Clean dfs with explanations
python-clean-dfs-with-explanations-by-db-teel
Let us put all the edges into adjacency list twice, one with weight 1 and one with weight -1 with oppisite direction. Then what we do is just traverse our graph
dbabichev
NORMAL
2020-05-31T04:07:27.335840+00:00
2020-05-31T04:26:40.113053+00:00
3,618
false
Let us put all the edges into adjacency list twice, one with weight `1` and one with weight `-1` with oppisite direction. Then what we do is just traverse our graph using usual dfs, and when we try to visit some neighbour, we check if this edge is usual or reversed.\n\n**Complexity** is `O(V+E)`, because we traverse ou...
57
5
[]
6
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[C++] Concise & Easy to understand DFS Solution | With Explanation
c-concise-easy-to-understand-dfs-solutio-60hv
The idea is to do a DFS traversal starting from node 0. For nodes other than 0, we check the parent who called DFS on it, and if it doesn\'t have an outgoing ed
anandthegreat
NORMAL
2020-05-31T05:15:20.767560+00:00
2020-05-31T16:40:49.782685+00:00
3,032
false
The idea is to do a DFS traversal starting from node 0. For nodes other than 0, we check the parent who called DFS on it, and if it doesn\'t have an outgoing edge to the parent, we increment the answer. We don\'t really need to reverse any edge. \n```\nvoid dfs(int idx,int caller,vector<vector<int>> &adjList,vector<ve...
39
2
['Depth-First Search', 'C']
4
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Java O(n) dfs with explanation
java-on-dfs-with-explanation-by-renato4-7jvb
Explanation: Since the problem states that we will always have n-1 connections and we will always be able to reorder so that every city can reach 0, we can conc
renato4
NORMAL
2020-05-31T04:18:36.688217+00:00
2020-06-01T02:13:42.499741+00:00
3,486
false
Explanation: Since the problem states that we will always have `n-1` connections and we will always be able to reorder so that every city can reach 0, we can conclude that no cycles will exist. If you are not conviced, try to think about a case where you have `n-1` edges and a cycle. You will notice that if you do that...
31
3
[]
4
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Day 83 || DFS || Easiest Beginner Friendly Sol
day-83-dfs-easiest-beginner-friendly-sol-2vps
NOTE - PLEASE READ INTUITION AND APPROACH FIRST THEN SEE THE CODE. YOU WILL DEFINITELY UNDERSTAND THE CODE LINE BY LINE AFTER SEEING THE APPROACH.\n\n# Intuitio
singhabhinash
NORMAL
2023-03-28T16:50:03.967201+00:00
2023-03-28T16:50:03.967240+00:00
5,789
false
**NOTE - PLEASE READ INTUITION AND APPROACH FIRST THEN SEE THE CODE. YOU WILL DEFINITELY UNDERSTAND THE CODE LINE BY LINE AFTER SEEING THE APPROACH.**\n\n# Intuition of this Problem :\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach for this Problem :\n1. Create an adjacency list \'adj...
30
1
['Depth-First Search', 'Graph', 'C++', 'Java', 'Python3']
4
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Visual Explanation | Simple Python DFS Solution | Idea | Comments
visual-explanation-simple-python-dfs-sol-t0ba
\n\nThought Process ...\n We want all directions towards city 0\n We know that we can reach to every city from 0 if its bidirectional\n Then we assume that we v
hanjo108
NORMAL
2021-12-07T05:10:24.558728+00:00
2021-12-07T18:46:13.431740+00:00
1,524
false
![image](https://assets.leetcode.com/users/images/eea7bb30-0a27-4665-b6e1-4457bef2fb75_1638852748.6979036.png)\n\n**Thought Process ...**\n* We want all directions towards city 0\n* We know that we can reach to every city from 0 if its bidirectional\n* Then we assume that we visit every city starting from 0, and whenev...
26
0
['Depth-First Search', 'Python']
3
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Easy BFS Solution in JAVA in Depth
easy-bfs-solution-in-java-in-depth-by-_n-uzjp
Key Idea\nSo the basic idea is to make the whole graph bidirectional , with an exception that the other edge will be a fake one (reverse of the edge given in co
_nakul_30
NORMAL
2020-09-13T09:19:20.394935+00:00
2020-09-13T09:20:50.534304+00:00
1,436
false
**Key Idea**\nSo the basic idea is to make the whole graph bidirectional , with an exception that the other edge will be a fake one (*reverse of the edge given in connections list*) as shown below : \n\n\n\n![image](https://assets.leetcode.com/users/images/8c49d921-192f-4dd3-b990-9bd8dca03333_1599987352.8303022.png)\n\...
24
0
['Breadth-First Search']
5
reorder-routes-to-make-all-paths-lead-to-the-city-zero
C++ || line by line explanation
c-line-by-line-explanation-by-amankatiya-hnj6
Intuition\nWe need find the number of road to rearrange in other words number of roads to reverse to reach 0 from each and every node.\n\n# Approach\nWe are go
amankatiyar783597
NORMAL
2023-03-24T03:35:31.457041+00:00
2023-03-24T03:42:13.489834+00:00
6,470
false
# Intuition\nWe need find the number of road to rearrange in other words number of roads to reverse to reach 0 from each and every node.\n\n# Approach\nWe are going to use concept of incoming and out-going edges in node.\n\n\nLet\'s start solving this problem along with code and explanaion of logic behiend each step.....
22
0
['C++']
2
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[C++] Basic DFS Solution | No erasing required | Mark direction of Edges
c-basic-dfs-solution-no-erasing-required-dit8
\nclass Solution {\npublic:\n // Correct Solution\n // We are just making an undirected graph, where we are giving (+ve) sign to outgoing\n // edges an
debanjan2002
NORMAL
2021-12-18T12:12:04.871171+00:00
2021-12-18T12:12:04.871199+00:00
1,991
false
```\nclass Solution {\npublic:\n // Correct Solution\n // We are just making an undirected graph, where we are giving (+ve) sign to outgoing\n // edges and pretending that there is also the same incoming edge (or in general the opposite edge)\n // and inserting it into the adj[] list as (-ve) elements.\n ...
20
2
['Depth-First Search', 'C', 'C++']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Java beats 96% just a few lines
java-beats-96-just-a-few-lines-by-tomeri-02rk
\n public int minReorder(int n, int[][] connections) {\n int res = 0;\n Set<Integer> s = new HashSet<>();\n int u, v;\n s.add(0);\n
tomerittah
NORMAL
2020-09-04T16:32:49.360723+00:00
2020-09-04T16:32:49.360794+00:00
1,584
false
```\n public int minReorder(int n, int[][] connections) {\n int res = 0;\n Set<Integer> s = new HashSet<>();\n int u, v;\n s.add(0);\n \n while (s.size() != n) {\n for (int i = 0; i < connections.length; i++) {\n u = connections[i][0];\n ...
20
0
[]
3
reorder-routes-to-make-all-paths-lead-to-the-city-zero
✅ Java | Easy | BFS
java-easy-bfs-by-kalinga-fee9
\nclass Solution {\n public int minReorder(int n, int[][] connections) {\n List<List<Integer>> adj=new ArrayList<>();\n for(int i=0;i<n;i++){\n
kalinga
NORMAL
2023-03-24T02:32:50.129720+00:00
2023-03-24T02:32:50.129764+00:00
3,786
false
```\nclass Solution {\n public int minReorder(int n, int[][] connections) {\n List<List<Integer>> adj=new ArrayList<>();\n for(int i=0;i<n;i++){\n adj.add(new ArrayList<>());\n }\n for(int i=0;i<connections.length;i++){\n adj.get(connections[i][0]).add(connections[i]...
19
1
['Breadth-First Search', 'Graph', 'Java']
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Python| Easy and fast | Beats 99%
python-easy-and-fast-beats-99-by-slavahe-8v9x
Time complexity: O(n)\n\nShort description:\nIn this solution, we add cities one by one to the country map. This addition follows 3 rules:\n1)If the first city
slavaherasymov
NORMAL
2021-02-18T11:46:39.167848+00:00
2021-02-18T11:47:42.331111+00:00
2,888
false
Time complexity: O(n)\n\nShort description:\nIn this solution, we add cities one by one to the country map. This addition follows 3 rules:\n1)If the first city of the road is already on the map, we need to change direction of this road and add a new city to the map.\n2)If the second city of the road is already on the ...
19
0
['Python', 'Python3']
4
reorder-routes-to-make-all-paths-lead-to-the-city-zero
JAVA | Easy Solution Using Adjacency List
java-easy-solution-using-adjacency-list-9bgzw
\nclass Solution {\n List<Integer>[] incoming, outgoing;\n HashSet<Integer> visited;\n int ans;\n public int minReorder(int n, int[][] connections){
onefineday01
NORMAL
2020-06-07T12:18:45.815982+00:00
2020-06-07T12:18:45.816013+00:00
1,957
false
```\nclass Solution {\n List<Integer>[] incoming, outgoing;\n HashSet<Integer> visited;\n int ans;\n public int minReorder(int n, int[][] connections){\n ans = 0;\n incoming = new ArrayList[n];\n outgoing = new ArrayList[n];\n for(int i = 0; i < n; i++){\n incoming[i] ...
17
0
['Depth-First Search', 'Graph', 'Java']
4
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[Java] Straight Forward DFS (Generic Solution) with Detail Explanation
java-straight-forward-dfs-generic-soluti-c07u
The idea which I have tried to implement through code is as follows. \nThe code will automatically handle the directions. Don\'t worry much about them yet.\nThe
manrajsingh007
NORMAL
2020-05-31T06:06:33.985636+00:00
2020-05-31T07:29:05.979956+00:00
1,310
false
The idea which I have tried to implement through code is as follows. \nThe code will automatically handle the directions. Don\'t worry much about them yet.\nThere is no cycle in the graph.\nOnce you start traversing from 0, you start doing a dfs, all the nodes which you have visited traversing the edges were the wrong ...
16
8
[]
7
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Using 0-1 BFS!!! Easy C++ Solution!!🔥🔥🔥
using-0-1-bfs-easy-c-solution-by-vrishav-zl1j
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nHere, we are using 0-1
vrishav28
NORMAL
2023-03-24T04:00:26.168653+00:00
2023-03-24T04:00:26.168686+00:00
2,208
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHere, we are using 0-1 BFS. We will treat every edge which is already given hav weight 1 and the opposite of that edge as 0. Then we will apply BFS and find the total ...
14
0
['C++']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Complete Explanation with intuition || Simple DFS || Easy to understand
complete-explanation-with-intuition-simp-65ka
\n\n\nclass Solution {\npublic:\n void dfs(vector<vector<pair<int, int>>>&adj, vector<bool>& visited, int& ans, int currNode)\n {\n visited[currNod
mohakharjani
NORMAL
2023-03-24T00:34:00.032450+00:00
2023-03-24T10:06:05.352698+00:00
2,364
false
![image](https://assets.leetcode.com/users/images/854694eb-e723-44cc-9129-b8176c865a17_1679619937.4753957.png)\n\n```\nclass Solution {\npublic:\n void dfs(vector<vector<pair<int, int>>>&adj, vector<bool>& visited, int& ans, int currNode)\n {\n visited[currNode] = true;\n for (pair<int, int>adjNode ...
14
1
['Depth-First Search', 'Graph', 'C', 'C++']
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
My Java DFS Solution with thought process
my-java-dfs-solution-with-thought-proces-0ey5
\n/* \nMy thought process\nSo here we can first actually build the graph and I uses a List<List<integer>> for it.\nHere if we are having parent->child, then we
vrohith
NORMAL
2020-09-30T05:38:43.952805+00:00
2020-09-30T05:38:43.952847+00:00
1,989
false
```\n/* \nMy thought process\nSo here we can first actually build the graph and I uses a List<List<integer>> for it.\nHere if we are having parent->child, then we take the sign as +ve and if vice versa we consider it as negative.\nFor traditional dfs, we need to keep track of the visited node thus we declare a boolean ...
14
1
['Depth-First Search', 'Graph', 'Java']
2
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Easy C++ Solution without Graphs
easy-c-solution-without-graphs-by-gazal1-yaik
I am inserting those nodes into the set from which we are having a path to 0. say -\neg- n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]\nnow I add 0 to th
gazal1199
NORMAL
2020-05-31T04:05:22.012482+00:00
2020-05-31T05:09:43.999945+00:00
693
false
I am inserting those nodes into the set from which we are having a path to 0. say -\neg- n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]\nnow I add 0 to the set , now connection[0] represents a path from 0 to 1, but i want this from 1 to 0, so I increased the count by 1.\nNow this means I have reversed this path ,...
14
8
[]
5
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Simple Python Solution
simple-python-solution-by-sanatanmishra4-1q5j
\n# Code\n\nclass Solution:\n def minReorder(self, n: int, connections: List[List[int]]) -> int:\n dt = defaultdict(list)\n for a,b in connecti
sanatanmishra42
NORMAL
2023-03-24T04:45:42.505564+00:00
2023-03-24T04:45:42.505606+00:00
3,439
false
\n# Code\n```\nclass Solution:\n def minReorder(self, n: int, connections: List[List[int]]) -> int:\n dt = defaultdict(list)\n for a,b in connections:\n dt[a].append((b,True))\n dt[b].append((a,False))\n ans = 0\n q = [0]\n vis = set()\n while(q):\n ...
12
0
['Python3']
7
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[ Without Graph ] Simple Solution using Set, faster than 100.00% of C++ online submissions
without-graph-simple-solution-using-set-doj94
\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n set <int > vv;\n int c=0;\n int l=connections.
lazerx
NORMAL
2020-05-31T04:19:43.578514+00:00
2020-05-31T05:16:35.153041+00:00
688
false
```\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n set <int > vv;\n int c=0;\n int l=connections.size();\n for(int i=0;i<l;i++)\n {\n if(connections[i][1]==0 || connections[i][0]==0)\n {\n if(connections...
11
8
['C', 'Ordered Set']
3
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Java | DFS Solution With Explanation | Cost of Moving Forward In Case of Bidirectional Roads
java-dfs-solution-with-explanation-cost-ro9hj
Problem Intuition-\nClearly, We need roads that take us to zero.\nIf Start a dfs from the zero, It will follow forward edges. The forward edges will take us far
asthakri50
NORMAL
2022-07-04T18:37:14.913459+00:00
2022-07-04T18:37:44.322658+00:00
937
false
**Problem Intuition-**\nClearly, We need roads that take us to zero.\nIf Start a dfs from the zero, It will follow forward edges. The forward edges will take us far from zero, So you need to reverse those edges.\n\n\n**Code Explanation-**\nConsider the graph to be birectional, (rather make it directional) , such that t...
10
0
['Depth-First Search', 'Java']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
JavaScript Solution - DFS Approach?
javascript-solution-dfs-approach-by-dead-qm0d
I am not sure if this is a good way of doing it, but I thought I would post and get your guys\' opinions. Thank you.\n\n\nvar minReorder = function(n, connectio
Deadication
NORMAL
2020-10-11T21:46:43.414249+00:00
2020-10-11T21:46:43.414312+00:00
1,117
false
I am not sure if this is a good way of doing it, but I thought I would post and get your guys\' opinions. Thank you.\n\n```\nvar minReorder = function(n, connections) {\n const graph = [];\n const set = new Set();\n \n for (let i = 0; i < n; i++) {\n graph[i] = [];\n }\n \n for (const [u, v]...
10
0
['Depth-First Search', 'JavaScript']
2
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Clean Python 3, BFS O(N)
clean-python-3-bfs-on-by-lenchen1112-p97w
Make an undirected graph and perform BFS start from 0.\nThen just check reverse roads while BFS.\nTime: O(N)\nSpace: O(N)\n\nimport collections\nclass Solution:
lenchen1112
NORMAL
2020-05-31T04:02:35.460190+00:00
2020-05-31T04:02:41.586532+00:00
664
false
Make an undirected graph and perform BFS start from 0.\nThen just check reverse roads while BFS.\nTime: `O(N)`\nSpace: `O(N)`\n```\nimport collections\nclass Solution:\n def minReorder(self, n: int, connections: List[List[int]]) -> int:\n graph = collections.defaultdict(list)\n visited = set([0])\n ...
10
6
[]
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
MOST INTUITIVE C++ SOLUTION !!
most-intuitive-c-solution-by-binarywizar-warf
\n\n# Intuition\nWe Need To Reach Capital City. For This, We Need To Run A DFS Traversal From Capital City (Node 0) To All The Cities. For This, We Need To Main
BinaryWizard_8
NORMAL
2024-05-04T16:57:30.526024+00:00
2024-05-04T17:03:17.197375+00:00
427
false
**![Upvote.jpeg](https://assets.leetcode.com/users/images/c0896a1a-406f-4963-b5aa-e6ad27a8275f_1714841801.5205965.jpeg)**\n\n# Intuition\nWe Need To Reach Capital City. For This, We Need To Run A DFS Traversal From Capital City (Node 0) To All The Cities. For This, We Need To Maintain An Adjacency List And Visited Arra...
9
0
['Depth-First Search', 'Graph', 'C++']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
JAVA || Easy Solution with Explanation || 100% Faster code
java-easy-solution-with-explanation-100-prazj
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
shivrastogi
NORMAL
2023-03-24T02:22:44.286871+00:00
2023-03-24T02:22:44.286901+00:00
1,544
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
9
0
['Java']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Python 3 95% speed
python-3-95-speed-by-very_drole-hh9l
\n\n# Code\n\nclass Solution:\n def minReorder(self, n: int, connections: List[List[int]]) -> int:\n graph = defaultdict(set)\n for a, b in con
very_drole
NORMAL
2022-09-30T20:58:01.983812+00:00
2022-09-30T20:58:01.983849+00:00
1,199
false
\n\n# Code\n```\nclass Solution:\n def minReorder(self, n: int, connections: List[List[int]]) -> int:\n graph = defaultdict(set)\n for a, b in connections:\n graph[a].add((b, True))\n graph[b].add((a, False))\n \n queue = deque([(0, False)])\n ans = 0\n ...
9
0
['Python3']
2
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[Python3] Simple DFS Solution. No set required, faster than 92%.
python3-simple-dfs-solution-no-set-requi-3kjs
To start we create out grid:\n\ngrid = defaultdict(list)\nfor a, b in connections:\n grid[a].append((b, 1))\n grid[b].append((a, 0))\n\nFor each connectio
Tommyd27
NORMAL
2023-03-24T09:59:55.580818+00:00
2023-03-24T09:59:55.580845+00:00
776
false
To start we create out grid:\n```\ngrid = defaultdict(list)\nfor a, b in connections:\n grid[a].append((b, 1))\n grid[b].append((a, 0))\n```\nFor each connection we have, there is a road from A to B. We add a road in both directions, so to speak, but store the direction in the second element of the tuple, with 1 ...
8
0
['Depth-First Search', 'Python3']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Easy to understand code with detailed explanation
easy-to-understand-code-with-detailed-ex-qx64
The problem asks us to reorient some roads such that each city can visit the capital city (city 0) and return the minimum number of roads changed. We are given
emmakuen
NORMAL
2023-03-24T08:27:51.437149+00:00
2023-03-24T08:27:51.437177+00:00
641
false
The problem asks us to reorient some roads such that each city can visit the capital city (city 0) and return the minimum number of roads changed. We are given n cities numbered from 0 to n-1 and n-1 roads that form a tree structure.\n\nTo solve this problem, we need to identify which roads need to be reversed to reach...
8
0
['JavaScript']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
JavaScript : Detailed explanation
javascript-detailed-explanation-by-jaypo-8a2v
Code\n\nvar minReorder = function(n, connections) {\n // from: (<from city>, [<to cities>])\n // to: (<to city>, [<from cities>])\n const from = new Ma
JayPokale
NORMAL
2023-03-24T03:34:03.819336+00:00
2023-03-24T03:34:03.819382+00:00
1,176
false
# Code\n```\nvar minReorder = function(n, connections) {\n // from: (<from city>, [<to cities>])\n // to: (<to city>, [<from cities>])\n const from = new Map(), to = new Map();\n\n // Function to insert in values in map\n const insert = (map, key, value) => {\n if(map.has(key)){\n const...
8
0
['JavaScript']
2
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[Python][Explanation] Approach with "virtual" nodes beats 85% with DFS
pythonexplanation-approach-with-virtual-s6xfu
Okay, we have to reorient the roads for making our event successful. Let\'s go!\n\nFirst of all, let\'s think about the building graph and traversal. \n\nQ & A:
nahirniak
NORMAL
2020-11-19T22:20:05.741118+00:00
2020-11-19T22:20:05.741161+00:00
527
false
Okay, we have to reorient the roads for making our event successful. Let\'s go!\n\nFirst of all, let\'s think about the building graph and traversal. \n\n**Q & A:**\n\nQ: How will we traverse this one?\nA: Let\'s build connections in opposite direction!\n\nQ: What the traversal will give us?\nA: We will able to reach o...
8
0
[]
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Java NO BFS/DFS. Only use Set O(V*E), Beat 100% Time, 100% Space.
java-no-bfsdfs-only-use-set-ove-beat-100-jbf3
Use set to contains node, and remove the node which can connect to 0.\nthere are 3 situtions: since edge is a -> b\n1. from 0 start edge, should reverse for su
flyatcmu
NORMAL
2020-05-31T05:58:15.924221+00:00
2020-06-01T01:05:48.163803+00:00
480
false
Use set to contains node, and remove the node which can connect to 0.\nthere are 3 situtions: since edge is a -> b\n1. from 0 start edge, should reverse for sure, count++;\n2. a - > b, if set NOT contains a, means a can connect to 0, and this edge a->b should reverse. count++;\n3. a -> b, if set NOT contains b, means ...
8
4
[]
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[BFS + DFS] Use DFS to explore and hashset to tracking old road orientation and trick
bfs-dfs-use-dfs-to-explore-and-hashset-t-lo0w
idea: traverse tree(or rode network) and count reversing\nTime Complexity: O(N)\nSpace Complexity: O(N)\nAlthough I solved 700+ leetcode problems, I am still r
codedayday
NORMAL
2020-05-31T04:29:36.705757+00:00
2020-06-04T03:54:58.656293+00:00
1,419
false
idea: traverse tree(or rode network) and count reversing\nTime Complexity: O(N)\nSpace Complexity: O(N)\nAlthough I solved 700+ leetcode problems, I am still rusty about some grammer. So I honestly documented all the bugs before deriving the final accepted solution. Wish it is helpful to the community. \n\n\n```\n// i...
8
1
['Depth-First Search', 'Breadth-First Search', 'C']
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
python dfs (actually simple not like the cringe overly short solutions)
python-dfs-actually-simple-not-like-the-ifjos
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
bruh50
NORMAL
2023-08-27T07:51:30.603616+00:00
2023-08-27T07:51:30.603632+00:00
823
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
7
0
['Python3']
3
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Concise DFS | C++
concise-dfs-c-by-tusharbhart-lc6c
\nclass Solution {\n int dfs(int node, int prnt, vector<pair<int, int>> adj[]) {\n int cnt = 0;\n for(auto ad : adj[node]) {\n if(ad
TusharBhart
NORMAL
2023-03-24T01:00:52.820692+00:00
2023-03-24T01:00:52.820719+00:00
2,023
false
```\nclass Solution {\n int dfs(int node, int prnt, vector<pair<int, int>> adj[]) {\n int cnt = 0;\n for(auto ad : adj[node]) {\n if(ad.first == prnt) continue;\n cnt += ad.second + dfs(ad.first, node, adj);\n }\n return cnt;\n }\npublic:\n int minReorder(int n...
7
0
['Depth-First Search', 'C++']
2
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[Go] [Golang] DFS, Adjacency List
go-golang-dfs-adjacency-list-by-f1rstmeh-6ej1
Since we know that graph is sparse we\'ll use Adjacency List to represent graph.\nn : number of cities\nm: number of roads\nSpace and Time complexity for Adjace
f1rstmehul
NORMAL
2022-03-09T10:44:12.942812+00:00
2022-03-16T03:01:08.128908+00:00
1,225
false
Since we know that graph is sparse we\'ll use Adjacency List to represent graph.\n**n : number of cities**\n**m: number of roads**\n*Space* and *Time* complexity for *Adjacency List* will be: **O(n + m)** [only if graph is sparse]\nIn worst case(i.e. when graph is dense) *space* and *time* complexity will be **n^2.**\n...
7
0
['Depth-First Search', 'Go']
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Video solution | Intuition explained in detail | C++ | DFS
video-solution-intuition-explained-in-de-01m1
Video\nHey everyone i have created a video solution for this problem (its in hindi), it involves intuitive explanation with code, this video is part of my playl
_code_concepts_
NORMAL
2024-10-24T10:00:43.001455+00:00
2024-10-24T10:00:43.001487+00:00
420
false
# Video\nHey everyone i have created a video solution for this problem (its in hindi), it involves intuitive explanation with code, this video is part of my playlist "Master Graphs"\nVideo link : https://www.youtube.com/watch?v=1aQPv0yDihs\nPlaylist link: : https://www.youtube.com/playlist?list=PLICVjZ3X1AcZ5c2oXYABLHl...
6
0
['C++']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
DFS (with explanation)
dfs-with-explanation-by-angielf-m20l
Approach\nThis solution uses a depth-first search (DFS) algorithm to determine the minimum number of edges that need to be changed to direct each city towards c
angielf
NORMAL
2024-05-17T05:24:59.818040+00:00
2024-05-17T05:24:59.818074+00:00
937
false
# Approach\nThis solution uses a depth-first search (DFS) algorithm to determine the minimum number of edges that need to be changed to direct each city towards city 0. Here is an overview of the approach:\n\n1. **Building the Graph**: The function first constructs a graph representation using an adjacency list. For ea...
6
0
['Depth-First Search', 'PHP', 'Python3', 'JavaScript']
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[C++]/DFS CODE WITH DETAILED EXPLAINATION AND MINIMUM TIME COMPLEXITY ✅✅✅
cdfs-code-with-detailed-explaination-and-bxs0
Intuition\n- First, we can think of counting the total number of edges that are not pointing towards zero because that will give us our final answer.\n- However
tushar_1290
NORMAL
2023-06-28T03:36:45.517643+00:00
2023-06-28T03:36:45.517662+00:00
683
false
# Intuition\n- First, we can think of counting the total number of edges that are not pointing towards zero because that will give us our final answer.\n- However, the question here is how to count these edges? Let\'s discuss the approach. Before proceeding i will suggest to think once again with this intuition.\n\n# A...
6
0
['C++']
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
C++ ✅ || BFS || Easy to Understand🔥
c-bfs-easy-to-understand-by-snowflakes17-sn82
\n# Approach\n Describe your approach to solving the problem. \n\n\n Create an adjacency list to represent the connections between the cities. \n We initialise
snowflakes17
NORMAL
2023-03-27T21:37:37.731688+00:00
2023-03-27T21:37:37.731737+00:00
246
false
\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n\n* Create an adjacency list to represent the connections between the cities. \n* We initialise the given directed edge with `weight 1` and create an opposite edge corresponding to the same with `weight 0`.\n\n* Initialize a queue to perform BFS, ...
6
0
['C++']
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Easy & Clear Solution Python 3
easy-clear-solution-python-3-by-moazmar-livi
Intuition\nThe problem requires us to reorient some edges in a tree such that all cities can visit the capital city, and we need to return the minimum number of
moazmar
NORMAL
2023-03-24T03:29:39.768153+00:00
2023-03-24T03:29:39.768197+00:00
1,150
false
# Intuition\nThe problem requires us to reorient some edges in a tree such that all cities can visit the capital city, and we need to return the minimum number of edges that need to be changed. To solve this, we can use a DFS algorithm to traverse the tree and count the number of edges that need to be reversed.\n# Appr...
6
0
['Python3']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
C++ || BFS || Graph || Commented & clean code
c-bfs-graph-commented-clean-code-by-kr_s-v06l
\n // Intuition -> we have to make all the edges going towards 0\n \n // so we will start from 0 , all the nodes which are going away from\n // 0 , w
KR_SK_01_In
NORMAL
2022-08-02T13:32:53.956896+00:00
2022-08-02T13:32:53.957244+00:00
838
false
```\n // Intuition -> we have to make all the edges going towards 0\n \n // so we will start from 0 , all the nodes which are going away from\n // 0 , we will take into count \n \n // Now the thing is , there is mixup of the all the edges , is it \n // sure that we will reach every nodes edge in tha...
6
0
['Breadth-First Search', 'Graph', 'C', 'C++']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Very Simple Explanation with diagram (just form new edges with weight = 0)
very-simple-explanation-with-diagram-jus-cwnx
\n\n\n the basic thing to note here is in this tree, whenever a parent is pointing to the child, that route needs to \n be inverted\n
uttu_dce
NORMAL
2021-08-20T14:12:10.068840+00:00
2022-08-08T08:44:39.593432+00:00
214
false
![image](https://assets.leetcode.com/users/images/0bad4901-9049-45ea-be17-c053a91bf519_1629469063.283581.jpeg)\n\n\n the basic thing to note here is in this tree, whenever a parent is pointing to the child, that route needs to \n be inverted\n so we have to make all the roads from child...
6
0
[]
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[2 Minute Read] Easy BFS
2-minute-read-easy-bfs-by-dead_lock-flag
The trick we are going to apply here is that we would add some extra edges to our given Tree (if you are thinking, how it\'s a tree, then a graph having n nodes
dead_lock
NORMAL
2021-07-19T16:39:53.728984+00:00
2021-09-04T18:00:50.398474+00:00
819
false
The trick we are going to apply here is that we would add some extra edges to our given Tree (if you are thinking, how it\'s a tree, then a graph having `n` nodes and `n - 1` edges such that there is only one path between any 2 pair of vertices is sure to be a Tree). \n\n**The TRICK**\n1. Reverse the direction of edges...
6
0
['Breadth-First Search', 'Java']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Python | BFS | Directed Graph + Undirected Graph | Explained with comments
python-bfs-directed-graph-undirected-gra-uccn
\nclass Solution:\n def minReorder(self, n: int, connections: List[List[int]]) -> int:\n from collections import defaultdict\n \n undire
underoos16
NORMAL
2020-05-31T04:22:17.315136+00:00
2020-05-31T05:11:47.632540+00:00
641
false
```\nclass Solution:\n def minReorder(self, n: int, connections: List[List[int]]) -> int:\n from collections import defaultdict\n \n undirected_graph = defaultdict(list)\n actual_graph = defaultdict(set) # for faster lookup\n\n for a,b in connections:\n undirected_graph...
6
0
['Breadth-First Search', 'Graph', 'Python']
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
BFS | 94 % Beats | Java - Simple Explanation
bfs-94-beats-java-simple-explanation-by-4xmg7
\n\n# Approach\n Describe your approach to solving the problem. \nFirst thing is making the graph bidirectional.\nIf an edge is given from a -> b, then we shoul
eshwaraprasad
NORMAL
2024-09-01T11:29:31.903851+00:00
2024-09-01T11:29:31.903871+00:00
501
false
\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst thing is making the graph bidirectional.\nIf an edge is given from a -> b, then we should build the graph\nby edges a -> b and b -> -a .\nHere minus sign indicates that we have created this new edge and it\'s not the given directed edge.\n\n...
5
0
['Java']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
BEATS 99% in time and 78% in space C# solution. DFS and BFS solution. Well commented.
beats-99-in-time-and-78-in-space-c-solut-g43t
General intuition\nThe logic is the same as that from the editorial solution. I just thought I\'d made this C# code much more readable and easy to understand. \
Aadityakiran_S
NORMAL
2023-09-08T04:35:24.087035+00:00
2023-09-08T10:07:24.604572+00:00
272
false
# General intuition\nThe logic is the same as that from the editorial solution. I just thought I\'d made this C# code much more readable and easy to understand. \n\nWe\'re basically doing a traversal. Since **all roads** need to **lead from other cities to 0**, if we traverse using an original edge away from zero, that...
5
0
['Depth-First Search', 'Breadth-First Search', 'Graph', 'C#']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
1466. Reorder Routes to Make All Paths Lead to the City Zero
1466-reorder-routes-to-make-all-paths-le-xuzs
***\n# Code\n\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& con) {\n vector<vector<int>> adj(n) , bk(n);\n vector<int
tusharsingh102003
NORMAL
2023-03-24T16:21:03.241102+00:00
2023-03-24T16:21:03.241139+00:00
901
false
***\n# Code\n```\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& con) {\n vector<vector<int>> adj(n) , bk(n);\n vector<int> v(n,0);\n v[0] =1;\n for(auto a : con){\n adj[a[0]].push_back(a[1]);\n bk[a[1]].push_back(a[0]);\n }\n in...
5
0
['C++']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
🔥 Java || Simple BFS Solution with Explanation ✅
java-simple-bfs-solution-with-explanatio-iq9m
\uD83D\uDCCC\uD83D\uDCCC The idea is simple : Perform Breadth First Traversal from the starting vertex , move BREADTH-WISE and keep on counting the edges needs
Yash_kr
NORMAL
2022-03-28T05:06:38.152222+00:00
2022-03-28T05:14:23.966643+00:00
236
false
\uD83D\uDCCC\uD83D\uDCCC **The idea is simple** : *Perform Breadth First Traversal from the starting vertex , move BREADTH-WISE and keep on counting the edges needs to be reversed. Outdegree Edges will be reversed while Indegree edges will be not*\uD83D\uDC4D\uD83C\uDFFB\n\nNow Lets say in BFS Traversal we reached som...
5
0
['Breadth-First Search']
2
reorder-routes-to-make-all-paths-lead-to-the-city-zero
C# solution (DFS)
c-solution-dfs-by-newbiecoder1-saik
Intuition\nStarting from city 0, traverse the graph by using DFS\nWhen encountering a foward path, we need to reverse it.\n\nComplexity\n- Time: O(n)\n- Space:
newbiecoder1
NORMAL
2022-03-21T16:38:02.928564+00:00
2024-09-28T05:22:13.321524+00:00
216
false
**Intuition**\nStarting from city ```0```, traverse the graph by using DFS\nWhen encountering a foward path, we need to reverse it.\n\n**Complexity**\n- Time: O(n)\n- Space: O(n)\n\n**Implementation**\n```\npublic class Solution {\n public int MinReorder(int n, int[][] connections) {\n \n List<int>[] g...
5
0
['Depth-First Search', 'Graph', 'C#']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
JAVA | BFS | Explained Solution
java-bfs-explained-solution-by-siddhantg-whpc
Here we\'ll simply construct out graph and also maintain a hashset where we\'ll store the original edges given to us. So when we\'ll start our normal BFS with 0
siddhantgupta792000
NORMAL
2022-02-23T02:04:01.508508+00:00
2022-02-23T02:04:01.508560+00:00
563
false
Here we\'ll simply construct out graph and also maintain a hashset where we\'ll store the original edges given to us. So when we\'ll start our normal BFS with 0 as starting node whenever we find a edge that is taking as away from our parent we\'ll check if that exits in our hashset and if yes then we\'ll increase our a...
5
0
['Depth-First Search', 'Breadth-First Search', 'Java']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Simple, clean and self-explanatory solution
simple-clean-and-self-explanatory-soluti-0i66
If you know any city x != 0that reaches 0, then you know 0 can be reached (eventually) if you reach the city x. Create a set to keep track of the cities that ca
aswin2
NORMAL
2020-09-07T17:22:31.267943+00:00
2020-09-07T17:22:31.267999+00:00
460
false
If you know any city `x != 0 `that reaches 0, then you know 0 can be reached (eventually) if you reach the city x. Create a set to keep track of the cities that can reach 0. As you traverse the connection array, keep adding those cities which has connection to any city that is in the set. If you found any city which i...
5
2
[]
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[Java] Runtime: 4 ms, faster than 99.21%
java-runtime-4-ms-faster-than-9921-by-10-vpsn
Runtime: 4 ms, faster than 99.21%\uFF0CMemory Usage: 53.9 MB, less than 100.00% of Java online submissions\n\n\npublic int minReorder(int n, int[][] roads) {\n\
103style
NORMAL
2020-05-31T04:03:59.067521+00:00
2020-06-03T01:58:06.799175+00:00
471
false
**Runtime: 4 ms, faster than 99.21%\uFF0CMemory Usage: 53.9 MB, less than 100.00% of Java online submissions**\n\n```\npublic int minReorder(int n, int[][] roads) {\n\tint res = 0;\n\t//find the first i make roads[i][0] =0 or roads[i][1] = 0, and swap roads[i] and roads[0]\n\tupdateZeroCityInHead(roads);\n\tint sortedI...
5
5
['Java']
7
reorder-routes-to-make-all-paths-lead-to-the-city-zero
JS/TS Solution DFS Approach
jsts-solution-dfs-approach-by-giogokul13-o14w
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n## Time
giogokul13
NORMAL
2024-09-17T16:40:51.481640+00:00
2024-09-17T16:40:51.481672+00:00
303
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n## Time complexity: O(N)\n- The time complexity of this solution is O(n), where n is the number of cities. This is because we are performing a DFS trav...
4
0
['Depth-First Search', 'Graph', 'TypeScript', 'JavaScript']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Beats 93%🔥|| DFS🔥|| easy JAVA Solution✅
beats-93-dfs-easy-java-solution-by-priya-b290
Code\n\nclass Solution {\n class Pair{\n int val;\n boolean f;\n Pair(int val,boolean f){\n this.val=val;\n this.f
priyanshu1078
NORMAL
2024-03-18T16:44:37.604402+00:00
2024-03-18T16:44:37.604438+00:00
394
false
# Code\n```\nclass Solution {\n class Pair{\n int val;\n boolean f;\n Pair(int val,boolean f){\n this.val=val;\n this.f=f;\n }\n }\n int ans;\n public int minReorder(int n, int[][] mat) {\n List<List<Pair>> adj=new ArrayList<>();\n for(int i=0;...
4
0
['Depth-First Search', 'Java']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Java | DFS | BSF | Beats 92%
java-dfs-bsf-beats-92-by-mcihan7-cxr3
Approach\n- minus(-) represents opposite direction. \n\n# Time Complexity\n \nO(n)\n\n\n\n# 1.DFS\njava\nclass Solution {\n int count =0;\n\n public int m
mcihan7
NORMAL
2024-02-11T00:16:03.453406+00:00
2024-02-11T00:17:47.086877+00:00
279
false
# Approach\n- minus(-) represents opposite direction. \n\n# Time Complexity\n \n$$O(n)$$\n\n\n\n# 1.DFS\n```java\nclass Solution {\n int count =0;\n\n public int minReorder(int n, int[][] connections) {\n boolean visit[] = new boolean[n];\n List<List<Integer>> graph = new ArrayList<>();\n\n f...
4
0
['Java']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Clean DFS + BFS Solution With Explanation (O(n^2) || O(n) time)
clean-dfs-bfs-solution-with-explanation-27kmy
Idea\n- The main idea of both solutions is that we will necessarily traverse from zero, visiting the node closest to it (even if we cannot reach it from zero) a
vltvdnl
NORMAL
2024-02-04T16:20:00.181477+00:00
2024-02-04T16:20:00.181512+00:00
230
false
# Idea\n- The main idea of both solutions is that we will necessarily traverse from zero, visiting the node closest to it (even if we cannot reach it from zero) and so on. If a node is directed to zero, ans is not incremented, otherwise ans++.\n- First we convert the given array into an adjacent list with a specific fe...
4
0
['Depth-First Search', 'Breadth-First Search', 'Graph', 'Go']
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
JavaScript Solution
javascript-solution-by-davinderpalrehal-w7jl
Intuition\nBuild an adjacency list with distances between the cities, where distance is 0 if incoming and 1 if outgoing.\n\n# Approach\nTraversing through the l
davinderpalrehal
NORMAL
2023-12-22T18:54:57.724840+00:00
2023-12-22T18:54:57.724870+00:00
163
false
# Intuition\nBuild an adjacency list with distances between the cities, where distance is 0 if incoming and 1 if outgoing.\n\n# Approach\nTraversing through the list just adding distances to cities that haven\'t been visited\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(n)$$\n\n# Code\n```\n/*...
4
0
['JavaScript']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
DFS || C++
dfs-c-by-deepakvrma-xqfa
\n# Code\n\nclass Solution {\npublic:\nint ans=0;\n void dfs(unordered_map<int,vector<int>>&mp ,unordered_map<int,bool>&vis,int m){\n vis[m]=true;\n
DeepakVrma
NORMAL
2023-10-11T18:30:48.216321+00:00
2023-10-11T18:30:48.216358+00:00
89
false
\n# Code\n```\nclass Solution {\npublic:\nint ans=0;\n void dfs(unordered_map<int,vector<int>>&mp ,unordered_map<int,bool>&vis,int m){\n vis[m]=true;\n for(auto i:mp[m]){\n if(i>=0){\n if(!vis[i]){ \n dfs(mp,vis,i);\n }\n }\n ...
4
0
['Depth-First Search', 'C++']
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
BFS C++ Bidirectional easy approach
bfs-c-bidirectional-easy-approach-by-ann-i3o4
\n\n# Code\n\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<pair<int, bool>>> adj_list(n);\n
annupriy
NORMAL
2023-03-25T10:37:44.074189+00:00
2023-03-25T10:37:44.074218+00:00
1,581
false
\n\n# Code\n```\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<pair<int, bool>>> adj_list(n);\n int count = 0;\n for(int i=0; i<connections.size(); i++){\n adj_list[connections[i][0]].push_back({connections[i][1], false});\n ...
4
0
['Breadth-First Search', 'C++']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Java || DFS Solution || Explained Intuition and Approach
java-dfs-solution-explained-intuition-an-rh17
```\nclass Solution {\n int count=0;\n public int minReorder(int n, int[][] edges) {\n // the graph which is provided is directed in different dirn
kurmiamreet44
NORMAL
2023-03-25T07:50:06.448409+00:00
2023-03-25T07:50:06.448456+00:00
190
false
```\nclass Solution {\n int count=0;\n public int minReorder(int n, int[][] edges) {\n // the graph which is provided is directed in different dirn\'s so we cannot reach all nodes if we start \n // traversing the graph from zero node.\n // to overcome this we make the graph directed in both w...
4
0
['Depth-First Search', 'Java']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
simple bfs solution
simple-bfs-solution-by-tawfik_045-bxls
\n\n# Code\n\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<pair<int,int>>>adj(n);\n fo
tawfik_045
NORMAL
2023-03-24T14:30:46.795268+00:00
2023-03-24T14:30:46.795312+00:00
1,477
false
\n\n# Code\n```\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<pair<int,int>>>adj(n);\n for(auto x:connections){\n adj[x[0]].push_back({x[1],1});\n adj[x[1]].push_back({x[0],0});\n }\n queue<int>qu;\n vec...
4
0
['C++']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[Kotlin] Dfs
kotlin-dfs-by-dzmtr-9qcv
First, create adjacency list. The trick is to mark in directed roads with minus.\nThis will help us to distinguish them from out directed roads on our way.\n\nT
dzmtr
NORMAL
2023-03-24T14:28:04.643230+00:00
2023-03-24T14:28:04.643281+00:00
30
false
First, create adjacency list. The trick is to mark `in` directed roads with minus.\nThis will help us to distinguish them from `out` directed roads on our way.\n\nThen do dfs and count how many of the roads are `out`.\n\n```\nclass Solution {\n fun minReorder(n: Int, connections: Array<IntArray>): Int {\n val...
4
0
['Kotlin']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
C++|| Graph || DFS || O(n) || 100% Faster
c-graph-dfs-on-100-faster-by-coder_0503-2gjm
Intuition\nConsider the graph as undirected graph and then do a DFS considering the src node as 0 and we need to reverse all the forward pointing edges.\nTo kee
Coder_0503
NORMAL
2023-03-24T12:29:48.480243+00:00
2023-03-24T12:29:48.480274+00:00
158
false
# Intuition\nConsider the graph as undirected graph and then do a DFS considering the src node as 0 and we need to reverse all the forward pointing edges.\nTo keep track of real edges I have made adjacency list as the vector<pair<int,int>> so if a real edge is present from a to b then i am inserting it like:-\na -> {b,...
4
0
['Depth-First Search', 'Graph', 'C++']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Easy JavaScript Solution using Queue and Insverse Graph. 92% faster then others.
easy-javascript-solution-using-queue-and-tnzr
Intuition\n1. Create Inverse Graph and Graph\n2. Take Queue with 0\n3. Loop through Queue and visit Inserve Geaph AND Graph\n4. IF Visited Through Graph ANSWER
rishabhthakkar7
NORMAL
2023-03-24T07:51:46.381907+00:00
2023-03-24T07:51:46.381938+00:00
375
false
# Intuition\n1. Create Inverse Graph and Graph\n2. Take Queue with 0\n3. Loop through Queue and visit Inserve Geaph AND Graph\n4. IF Visited Through Graph ANSWER++ AND VISITED\n5. SAME PROCESSS TILL Queue is not empty\n6. NOTE: this approach works her because we do have n-1 roads\n\n\n**PLEASE UPVOTE!!!**\n```\nvar mi...
4
0
['JavaScript']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Java | DFS | Clean code | O(n) time
java-dfs-clean-code-on-time-by-judgement-yln3
Intuition\n Describe your first thoughts on how to solve this problem. \nSince there are n cities and n-1 roads, and the graph is connected, there are bound to
judgementdey
NORMAL
2023-03-24T04:57:37.840339+00:00
2023-03-24T22:11:21.677768+00:00
278
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince there are `n` cities and `n-1` roads, and the graph is connected, there are bound to be no cycles in the graph. We can construct an adjacency list of the roads assuming they are bidirectional, but remembering the order of the roads ...
4
0
['Depth-First Search', 'Graph', 'Recursion', 'Java']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Kotlin DFS
kotlin-dfs-by-kotlinc-tlia
\nclass Solution {\n fun minReorder(n: Int, connections: Array<IntArray>): Int {\n val g = Array<MutableList<Pair<Int, Boolean>>>(n) { mutableListOf<Pair<In
kotlinc
NORMAL
2023-03-24T01:03:44.488251+00:00
2023-03-24T01:03:44.488295+00:00
111
false
```\nclass Solution {\n fun minReorder(n: Int, connections: Array<IntArray>): Int {\n val g = Array<MutableList<Pair<Int, Boolean>>>(n) { mutableListOf<Pair<Int, Boolean>>() }\n for ((a, b) in connections) {\n g[a].add(Pair(b, true)) // this edge is provided\n g[b].add(Pair(a, false)) // this edge is i...
4
0
['Kotlin']
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
C++ Fast and Intuitive Approach
c-fast-and-intuitive-approach-by-divyans-u9ec
Intuition\nUse DFS and a reverse graph to traverse.\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nWe create two adjacency lists.
divyansh-xz
NORMAL
2023-01-11T05:29:58.385844+00:00
2023-01-11T05:29:58.385892+00:00
1,215
false
# Intuition\nUse DFS and a reverse graph to traverse.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe create two adjacency lists. One of original graph and other of reverse of that graph(all edges direction reversed).\nWe create a queue from which we start dfs.\n<!-- Describe your...
4
0
['Depth-First Search', 'Graph', 'Queue', 'C++']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
C++ Solution
c-solution-by-pranto1209-8qxm
Approach\n Describe your approach to solving the problem. \n DFS\n\n# Code\n\nclass Solution {\npublic:\n vector<pair<int, int>> g[50005];\n int ans, v
pranto1209
NORMAL
2022-12-14T18:35:38.194428+00:00
2023-03-24T10:51:23.086382+00:00
456
false
# Approach\n<!-- Describe your approach to solving the problem. -->\n DFS\n\n# Code\n```\nclass Solution {\npublic:\n vector<pair<int, int>> g[50005];\n int ans, vis[50005];\n void dfs(int u) {\n vis[u] = 1;\n for(auto v: g[u]) {\n if(!vis[v.first]) {\n ans += v.secon...
4
0
['C++']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Simple python solution using BFS traversal
simple-python-solution-using-bfs-travers-bajl
\nclass Solution:\n def minReorder(self, n: int, connections: List[List[int]]) -> int:\n visited=[0]*n\n indegree=[[] for _ in range(n)]\n
beneath_ocean
NORMAL
2022-10-24T12:31:53.799646+00:00
2022-10-24T12:31:53.799732+00:00
1,544
false
```\nclass Solution:\n def minReorder(self, n: int, connections: List[List[int]]) -> int:\n visited=[0]*n\n indegree=[[] for _ in range(n)]\n outdegree=[[] for _ in range(n)]\n for frm,to in connections:\n indegree[to].append(frm)\n outdegree[frm].append(to)\n ...
4
0
['Breadth-First Search', 'Graph', 'Python', 'Python3']
1