Destination City - Leetcode 1436 - Python
Skills:
Algorithm Basics90%
Key Takeaways
Solves Leetcode 1436 using Python to find the destination city
Full Transcript
hey everyone welcome back and let's write some more neat code today so today let's solve the problem destination city we're given an array paths where each path is actually an edge at least in terms of graphs it is basically a direct connection from City a to City B which again we can just think of as nodes in our graph and our goal here is to return the destination city which we Define as the city that does not have any sort of outgoing Edge what makes this problem relatively easy is that our graph is actually going to be a pretty simple structure it's basically going to form a single line without any Loop and therefore we're going to be guaranteed to have a destination city so in reality this is a really simple graph it pretty much looks like a linked list to me and this question is really just asking us what is the last node in the linked list now of course the difficulty comes from the input that we're given like we're not given this in the format of a link list we're given the list of edges for example here on the left I'm actually going to take this example because it's relatively simple we have B connecting to C so we have something like this B goes to C we have d goes to B so we got this here and then we have C goes to a so with all those put together this this is what we end up with and of course what we want to return here is the last one now there are multiple ways that we could solve this problem intuitively it kind of makes sense to just build out the graph and then we can pretty much start from anywhere we could start at the beginning of the linked list or we could just start in the middle either way we're just going to keep traversing it until we get to the end and then just return what the last one is like that's a relatively simple way to do this an even easier way way is to note the last thing that they say here actually not there I think here the last city is the one that does not have any outgoing City from it so what we can actually do here is go through these list of edges and we can record every single City that does have an outgoing Edge because the first one is the source and the second one is the destination right like with BC here we had this Edge created so for every single one of these we can say Okay B has an outgoing Edge D has an outgoing Edge C also has an outgoing Edge and then the last one that we're left with here is a it did not have an outgoing Edge so that's the one we would return now in terms of code how do we implement this the easiest way at least in my opinion is to take all of these like the ones that do have outgoing edges throw them in a hash set because it's an efficient data structure and then what we would do is iterate over every city in the input and actually we don't have to iterate over every city because we already know like these first ones in each pair are not going to be the result so we really only have to iterate over the Second City in every pair and for every city the Second City in every pair we're going to check does c exist in the hash set yes it does does B exist in the hash set yes it does does a exist in the hash set no it does not what does that mean that means a does not have an outgoing Edge so of course a is the result a is the city without any outgoing path that's what we're told to return so that's what we do now every time we iterate over these like that's going to be o of N and for us to check for each city is it in the hash set that's constant time so it's going to be an O of n algorithm to do this second portion and to actually build the hash set itself that's also going to be o of n so the overall time complexity is going to be just Big O of n the space complexity as well is going to be B go of n of course because that's the size of the hash set by the way n in this case is just a number of pairs that were given in the input array now let's code this up so here I'm going to create a hash set I'm just going to call it s you can give it a better variable name if you'd like but it doesn't really matter in this case and what we're going to do is for every pair or path in the list of paths we want to add the source City to the set so we can do that very easily like this so now that's pretty much it we've initialized the hash set to the way we want it the second part is going to be once again iterating over every path in the list of paths and now we check if the Second City like the distination is not in the hash set then it must be the one that does not have an outgoing Edge and it must be the one that we should return and that's exactly what we do it's guaranteed that one is going to exist so we don't need to put another return statement out here so this is the entire code let's run it to make sure that it works and as you can see on the left guess it does and it's pretty efficient if you found this helpful please like And subscribe if you're preparing for coding interviews check out n code. thanks for watching and I'll see you soon
Original Description
🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews
🧑💼 LinkedIn: https://www.linkedin.com/in/navdeep-singh-3aaa14161/
🥷 Discord: https://discord.gg/ddjKRXPqtk
🐦 Twitter: https://twitter.com/neetcode1
🐮 Support the channel: https://www.patreon.com/NEETcode
⭐ BLIND-75 PLAYLIST: https://www.youtube.com/watch?v=KLlXCFG5TnA&list=PLot-Xpze53ldVwtstag2TL4HQhAnC8ATf
💡 DYNAMIC PROGRAMMING PLAYLIST: https://www.youtube.com/watch?v=73r3KWiEvyk&list=PLot-Xpze53lcvx_tjrr_m2lgD2NsRHlNO&index=1
Problem Link: https://leetcode.com/problems/destination-city/
0:00 - Read the problem
0:30 - Drawing Explanation
4:19 - Coding Explanation
leetcode 1436
#neetcode #leetcode #python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeetCodeIO · NeetCodeIO · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Leetcode 149 - Maximum Points on a Line - Python
NeetCodeIO
Design Linked List - Leetcode 707 - Python
NeetCodeIO
Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python
NeetCodeIO
Design Browser History - Leetcode 1472 - Python
NeetCodeIO
Number of Good Paths - Leetcode 2421 - Python
NeetCodeIO
Flip String to Monotone Increasing - Leetcode 926 - Python
NeetCodeIO
Maximum Sum Circular Subarray - Leetcode 918 - Python
NeetCodeIO
Find Closest Node to Given Two Nodes - Leetcode 2359 - Python
NeetCodeIO
Concatenated Words - Leetcode 472 - Python
NeetCodeIO
Data Stream as Disjoint Intervals - Leetcode 352 - Python
NeetCodeIO
LFU Cache - Leetcode 460 - Python
NeetCodeIO
N-th Tribonacci Number - Leetcode 1137
NeetCodeIO
Best Team with no Conflicts - Leetcode 1626 - Python
NeetCodeIO
Greatest Common Divisor of Strings - Leetcode 1071 - Python
NeetCodeIO
Shortest Path in a Binary Matrix - Leetcode 1091 - Python
NeetCodeIO
Insert into a Binary Search Tree - Leetcode 701 - Python
NeetCodeIO
Delete Node in a BST - Leetcode 450 - Python
NeetCodeIO
Shuffle the Array (Constant Space) - Leetcode 1470 - Python
NeetCodeIO
Fruits into Basket - Leetcode 904 - Python
NeetCodeIO
Number of Subarrays of size K and Average Greater than or Equal to Threshold - Leetcode 1343 Python
NeetCodeIO
Naming a Company - Leetcode 2306 - Python
NeetCodeIO
As Far from Land as Possible - Leetcode 1162 - Python
NeetCodeIO
Shortest Path with Alternating Colors - Leetcode 1129 - Python
NeetCodeIO
Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python
NeetCodeIO
Count Odd Numbers in an Interval Range - Leetcode 1523 - Python
NeetCodeIO
Contains Duplicate II - Leetcode 219 - Python
NeetCodeIO
Path with Maximum Probability - Leetcode 1514 - Python
NeetCodeIO
Add to Array-Form of Integer - Leetcode 989 - Python
NeetCodeIO
Unique Paths II - Leetcode 63 - Python
NeetCodeIO
Minimum Distance between BST Nodes - Leetcode 783 - Python
NeetCodeIO
Design Hashmap - Leetcode 706 - Python
NeetCodeIO
Range Sum Query Immutable - Leetcode 303 - Python
NeetCodeIO
Binary Tree Zigzag Level Order Traversal - Leetcode 103 - Python
NeetCodeIO
Middle of the Linked List - Leetcode 876 - Python
NeetCodeIO
Course Schedule IV - Leetcode 1462 - Python
NeetCodeIO
Single Element in a Sorted Array - Leetcode 540 - Python
NeetCodeIO
Capacity to Ship Packages - Leetcode 1011 - Python
NeetCodeIO
IPO - Leetcode 502 - Python
NeetCodeIO
Minimize Deviation in Array - Leetcode 1675 - Python
NeetCodeIO
Longest Turbulent Array - Leetcode 978 - Python
NeetCodeIO
Last Stone Weight II - Leetcode 1049 - Python
NeetCodeIO
Construct Quad Tree - Leetcode 427 - Python
NeetCodeIO
Find Duplicate Subtrees - Leetcode 652 - Python
NeetCodeIO
Sort an Array - Leetcode 912 - Python
NeetCodeIO
Ones and Zeroes - Leetcode 474 - Python
NeetCodeIO
Remove Duplicates from Sorted Array II - Leetcode 80 - Python
NeetCodeIO
Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
NeetCodeIO
Concatenation of Array - Leetcode 1929 - Python
NeetCodeIO
Symmetric Tree - Leetcode 101 - Python
NeetCodeIO
Check Completeness of a Binary Tree - Leetcode 958 - Python
NeetCodeIO
Construct Binary Tree from Inorder and Postorder Traversal - Leetcode 106 - Python
NeetCodeIO
Find Peak Element - Leetcode 162 - Python
NeetCodeIO
Accounts Merge - Leetcode 721 - Python
NeetCodeIO
Binary Tree Preorder Traversal (Iterative) - Leetcode 144 - Python
NeetCodeIO
Binary Tree Postorder Traversal (Iterative) - Leetcode 145 - Python
NeetCodeIO
Number of Zero-Filled Subarrays - Leetcode 2348 - Python
NeetCodeIO
Minimum Score of a Path Between Two Cities - Leetcode 2492 - Python
NeetCodeIO
Sqrt(x) - Leetcode 69 - Python
NeetCodeIO
Successful Pairs of Spells and Potions - Leetcode 2300 - Python
NeetCodeIO
Optimal Partition of String - Leetcode 2405 - Python
NeetCodeIO
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
The Minecraft anvil is a tree-cost optimization problem in disguise
Dev.to · Mark
KMP Algorithm (Knuth-Morris-Pratt): The Smart Way to Perform String Matching in O(N)
Dev.to · Jaspreet singh
Every Backtracking Problem Is the Same Three Lines. I Just Couldn't See the Tree.
Dev.to · Alex Mateo
DSA From Zero to Hero #3: Sliding Window (Fixed Size) Explained With a Java Example
Medium · Programming
Chapters (3)
Read the problem
0:30
Drawing Explanation
4:19
Coding Explanation
🎓
Tutor Explanation
DeepCamp AI