Тёмный

Solving Coding Interview Questions in Python on LeetCode (easy & medium problems) 

Keith Galli
Подписаться 222 тыс.
Просмотров 175 тыс.
50% 1

In this video we walk through a series of eight coding interview questions on leetcode. These are algorithms problems that cover topics including data structures, time & space complexity, sorting, binary search, object oriented programming, breadth first search, tree traversals, and more. The difficulty of problems range from easy to medium and get gradually harder as you go through the video.
Practice your Python Pandas data science skills with problems on StrataScratch!
stratascratch.com/?via=keith
Join the Python Army to get access to perks!
RU-vid - / @keithgalli
Patreon - / keithgalli
If you enjoy this video, make sure to SUBSCRIBE to not miss future videos.
Checkout out my new channel where I will frequently be posting problems like these: / @techtrekbykeithgalli
Feel free to recommend the next problems that I should solve on RU-vid!
Links to problems:
LeetCode 1678: leetcode.com/problems/goal-pa...
LeetCode 1436: leetcode.com/problems/destina...
LeetCode 1365: leetcode.com/problems/how-man...
LeetCode 704: leetcode.com/problems/binary-...
LeetCode 409: leetcode.com/problems/longest...
LeetCode 1561: leetcode.com/problems/maximum...
LeetCode 1472: leetcode.com/problems/design-...
LeetCode 1110: leetcode.com/problems/delete-...
-------------------------
Timeline!
0:00 - Introduction
2:02 - LeetCode 1678 - Goal Parser Interpretation
6:04 - LeetCode 1436 - Destination City
14:07 - LeetCode 1365 - How Many Numbers Are Smaller Than the Current Number
33:13 - LeetCode 704 - Binary Search
46:13 - LeetCode 409 - Longest Palindrome
55:01 - LeetCode 1561 - Maximum Number of Coins You Can Get
1:10:09 - LeetCode 1472 - Design Browser History
1:22:32 - LeetCode 1110 - Delete Nodes And Return Forest
1:38:23 - Conclusion & Announcement!
-------------------------
Follow me on social media!
Instagram | / keithgalli
Twitter | / keithgalli
-------------------------
If you are curious to learn how I make my tutorials, check out this video: • How to Make a High Qua...
*I use affiliate links on the products that I recommend. I may earn a purchase commission or a referral bonus from the usage of these links.

Опубликовано:

 

3 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 142   
@KeithGalli
@KeithGalli 3 года назад
Hey everyone! If you enjoy this video, I just created a new channel where I'll be posting problems like this pretty frequently. Check it out here: ru-vid.com/show-UCge9kgmp38FIjGK4xdJsThwabout (will start posting when it hits 1000 subs) Also, we're getting so close to 100K!! So crazy. I really appreciate everyone's support and am super thankful to have a platform where the content can reach so many. Love ya'll!
@LNMLucasMasiero
@LNMLucasMasiero 3 года назад
It's so funny coding with your tutorials and inspired me to learn more of python. Thanks for your videos. They are really useful
@muskankaushik5628
@muskankaushik5628 3 года назад
subscribed! Looking forward to more great content.
@voquy1155
@voquy1155 3 года назад
Thanks for motivating me to come with python
@johnhhu2137
@johnhhu2137 2 года назад
class Solution: def destCity(self, paths):#: List[List[str]]) -> str: going_count = {} for i in paths: [*cities] = i for j in range(len(cities)-1): going_count[cities[j]] = going_count.get(cities[j], 0) + 1 going_count[cities[-1]] = 0 print(going_count) dest = [] for city in going_count: if going_count[city] == 0: dest.append(city) return dest paths = [['a', 'b', 'c', 'd'], ['a', 'c', 'e', 'f'], ['b', 'c', 'f'], ['d', 'j']]#, ['d', 'c']] # should not use dict solution = Solution() solution.destCity(paths)
@eatbreathedatascience9593
@eatbreathedatascience9593 2 года назад
May I know which software you use to do your video editing ? Thank you !
@Roastmaster704
@Roastmaster704 3 года назад
loved how you have labeled each segment of the video with the corresponding problem title , keep up the great work !!
@barecodedreamersacademy6147
@barecodedreamersacademy6147 2 года назад
TO LEARN AND CRACK COMPETITIVE PROGRAMS ru-vid.com/show-UCyoFQsVztx2oHWq14nY8A6A
@nileshshukla9600
@nileshshukla9600 2 года назад
We love the way you did this. I prefer this approach over youtubers that clearly comes prepared and solve it with no hesitancy and in a bit of complex way
@manyes7577
@manyes7577 Год назад
fun to watch people get stuck like me😁
@fatokunisaac5121
@fatokunisaac5121 Год назад
@@manyes7577 😅
@sauravthakur2915
@sauravthakur2915 3 года назад
Thankyou so much Keith! I just learned how to approach a problem. You made it looked so easy. Please post more content like this.
@KeithGalli
@KeithGalli 3 года назад
Glad it was helpful! You're very welcome. Definitely going to be posting more content like this on the new channel I created :)
@VortigrenHengist
@VortigrenHengist 2 года назад
@@KeithGalli what is that channel called?
@KeithGalli
@KeithGalli 2 года назад
TechTrek by Keith Galli: ru-vid.com
@ilanaizelman3993
@ilanaizelman3993 2 года назад
Great video! Please note the middle can overflow. Thus, we prefer to use mid=start+(end-start) / 2
@cbingu
@cbingu 2 года назад
you're brilliant bro. The way you think really helps me understand how to solve these questions
@ryandavis280
@ryandavis280 2 года назад
This is very helpful. more of these please!!
@hkemal2743
@hkemal2743 3 года назад
Thank you Keith for great Videos. A couple of suggestions: If you can dissect the problems, that would be great. E.g. String command = string method in Python. In order to solve this problem, you need to know class, string methods, replace() and functions. Here are the links for class,...,functions.
@kevinweiss8347
@kevinweiss8347 Год назад
It was really fun to walk through these with your video after trying each one myself. Some of my solutions were the same and others quite different. Overall a great video, thanks!
@timthegreatone
@timthegreatone 3 года назад
Thanks keith. I've been looking for content like this.
@KeithGalli
@KeithGalli 3 года назад
Glad to hear it! You're welcome :)
@shreehari2589
@shreehari2589 3 года назад
Hey Keith more videos on leetcode please, this video is super awesome, love the way how you explain each and every step, please do more videos on leetcode thank you
@barecodedreamersacademy6147
@barecodedreamersacademy6147 2 года назад
TO LEARN AND CRACK COMPETITIVE PROGRAMS ru-vid.com/show-UCyoFQsVztx2oHWq14nY8A6A
@raphaelokoye4310
@raphaelokoye4310 3 года назад
Love this!!! Pls more content in this direction.
@KeithGalli
@KeithGalli 3 года назад
Glad you enjoyed! Planning to post content like this pretty frequently on the new channel i created :)
@nataliatenoriomaia1635
@nataliatenoriomaia1635 2 года назад
Excellent video! Greatly helped me. Thank you very much!
@barecodedreamersacademy6147
@barecodedreamersacademy6147 2 года назад
TO LEARN AND CRACK COMPETITIVE PROGRAMS ru-vid.com/show-UCyoFQsVztx2oHWq14nY8A6A
@atharvapatil7549
@atharvapatil7549 3 года назад
Loved this 🔥 please do more of such leetcode questions. Let's hit that 100k and 1k😁
@bulidiriba4598
@bulidiriba4598 Год назад
great tutorial, helped me a lot!
@pratick5296
@pratick5296 3 года назад
Hey Keith love your content👍, Can you do maybe a series of videos on algorithms in python
@AnalystTosh
@AnalystTosh Год назад
Good stuff Keith.. learning from Kenya
@nileshshukla9600
@nileshshukla9600 2 года назад
Thank you for doing this.
@farheenahmed969
@farheenahmed969 2 года назад
Thank you, learned a lot from this video, Please post more videos
@barecodedreamersacademy6147
@barecodedreamersacademy6147 2 года назад
TO LEARN AND CRACK COMPETITIVE PROGRAMS ru-vid.com/show-UCyoFQsVztx2oHWq14nY8A6A
@Chuukwudi
@Chuukwudi 3 года назад
I love my own solution to the second question on Destination City. I simply returned the cityB that doesn't occur anywhere else in the whole list. class Solution: def destCity(self, paths: List[List[str]]) -> str: for path in paths: lister = [x for x in paths if x != path] if not (any(path[1] in listeri for listeri in lister)): return path[1]
@michaelscarn7375
@michaelscarn7375 3 года назад
that's code golfing, never do this if you work with team
@Chuukwudi
@Chuukwudi 3 года назад
I’d rather say “succinct”.
@iamjohnrobot
@iamjohnrobot 2 года назад
You could just zip the paths list, and find cities in the second zip chunk not in the first using set logic. I’m surprised he didn’t do this start_cities, dest_cities = list(zip(*paths)) result = dest_cities.”notin”(start_cities) Ooh and the result he read does this
@hectormotsepe1581
@hectormotsepe1581 3 года назад
Thank you Keith
@KeithGalli
@KeithGalli 3 года назад
You're welcome :)
@nardtaye2549
@nardtaye2549 3 года назад
Thank you, Keith! Best lessons , as usual!
@NK-hs7io
@NK-hs7io 3 года назад
Thank you, you are the best
@andyn6053
@andyn6053 3 года назад
Love your videos. Each time you upload a new one, its just like you read my mind and give me exactly what I need to become a data scientist. Please do a SQL tutorial or even better a "real world" example of how using SQL together with Python. Greetings from Sweden
@matan7645
@matan7645 3 года назад
Hey Keith, well done video! For the palindrome problem, your solution wouldn't work properly if you have 2 different characters with odd number of appearances, you would add both of them to the count while only one could be used as the palindrome "center"
@ofiregev
@ofiregev 2 года назад
Thank you very much for the video, it helped a lot !!(:
@thomazscg
@thomazscg 2 года назад
Que vídeo incrível. Surreal. Muito didático até para mim, brasileiro.
@advaymayank1410
@advaymayank1410 3 года назад
awesome!! btw congrats on 100k soon!
@KeithGalli
@KeithGalli 3 года назад
Thank you!!
@purpleandjodeci
@purpleandjodeci Год назад
Thank you
@BLAKKGHOST
@BLAKKGHOST 2 года назад
I have an interview as an intern at amazon on the 11th I will comment if I made it or not wish me luck!!! I haven't taken any alogirthm class before so I know this is going to be hard UPDATE[1 year later] I did not get a return offer after internship but I went to smaller fortune 500 then into fintech. I am a Mid Level SWE now. Its crazy how a year ago i didnt know anything.
@ilanaizelman3993
@ilanaizelman3993 2 года назад
How was it
@testa5687
@testa5687 2 года назад
How did it go
@jethrovic892
@jethrovic892 2 года назад
😂😂😂
@Luis-ef2zn
@Luis-ef2zn Год назад
@@testa5687 i think he got rekt
@Hephasto
@Hephasto Год назад
He got dumped, 101% sure
@user-pg4ct9py2c
@user-pg4ct9py2c Год назад
Hi, Keith! Thanks a lot for the video, it's awesome! I have an assumption of why performance is not that good in the last problem. You use a list to implement a queue, but as far as I know Python implements list as dynamic array and removing an element from the beginning has O(n) complexity (due to shifting), so lists are suitable to be used as stacks, but very slow when used as queues. Instead, we could use collections.deque which has amortized O(1) complexity of removing element from either the beginning or the end (so can be used efficiently both as a stack and a queue)
@briansigilai6534
@briansigilai6534 2 года назад
For 33:13 - LeetCode 704 - Binary Search,this is the most optimal solution. class Solution(object): def search(self, nums, target): return -1 if target not in nums else nums.index(target)
@SirMrMystery
@SirMrMystery Год назад
true but that is not binary search
@shawnbeans7389
@shawnbeans7389 3 года назад
close to 100k lets gooo
@paddygthatsme
@paddygthatsme Год назад
Thanks for the great video! How common are questions like this in data science interviews compared to the pandas type questions in your stratascratch video?
@adityamulik
@adityamulik 2 года назад
The very first problem of Goal Parser Interpretation, I made a mess. 1. First I didn't read the question properly. My mind started thinking of different edge cases, wherein different words might be given as test inputs. (e.g. F()()t(ball), etc) 2. I started looping over the string, I'm much habituated to loop over things which is the worst approach ever. 3. I started trying to figure out to how to replace the single brackets without creating blank spaces. 4. I never read the constraints properly. Thanks Keith for this video, it help me analyze my problems and I can start working on fixing those whilst attempting these interview questions.
@argile5
@argile5 Год назад
I solved the first one very simple. You copy the input list and reverse sorted it. Then use a loop to put the data as keys into a dictionary with values equal to the loop index. You see right away that since its sorted, the loop index gives the correct numerical output values for each number. From there, to create the output , you just loop through the original array and use dictionary key search to put the corresponding values into an output array.
@duonghue8388
@duonghue8388 3 года назад
Thank you so much
@KeithGalli
@KeithGalli 3 года назад
you're welcome!
@barecodedreamersacademy6147
@barecodedreamersacademy6147 2 года назад
TO LEARN AND CRACK COMPETITIVE PROGRAMS ru-vid.com/show-UCyoFQsVztx2oHWq14nY8A6A
@jonpounds1922
@jonpounds1922 3 года назад
NEAT!
@KeithGalli
@KeithGalli 3 года назад
:)
@sarthak_chauhan1010
@sarthak_chauhan1010 Год назад
30:20 I genuinely loves how he react to his mistakes.
@peteryoung4606
@peteryoung4606 2 года назад
for the 1436, the key point is using hash table. def destCity(self, paths: List[List[str]]) -> str: citiesA = {path[0] for path in paths} return next(path[1] for path in paths if path[1] not in citiesA)
@dariuszspiewak5624
@dariuszspiewak5624 Год назад
I've found this (rather elegant) pythonic solution to the longest_palindrome challenge: def longestPalindrome(self, s: str) -> int: d = Counter(s).values() return sum(2 * (c // 2) for c in d) + any(c % 2 == 1 for c in d)
@mustangjoe2071
@mustangjoe2071 2 года назад
For 1561 maximum number of coins: piles.sort() c = -2 result = 0 for i in range(len(piles)//3): result += piles[ c ] c -= 2 return result Or in for loop piles.pop() result += piles.pop()
@anandakrishnanp6579
@anandakrishnanp6579 3 года назад
Bro PLZZ do continue this series....each week please do post 1hr videos on leetcode python solutions
@KeithGalli
@KeithGalli 3 года назад
Going to try to post a problem or two a week on the new channel I created, make sure to check that out!
@barecodedreamersacademy6147
@barecodedreamersacademy6147 2 года назад
TO LEARN AND CRACK COMPETITIVE PROGRAMS ru-vid.com/show-UCyoFQsVztx2oHWq14nY8A6A
@swarupsarangi734
@swarupsarangi734 2 года назад
For Second Solution, I simply converted the nested list to the dictionary and iterated the dictionary to find the city which is not a key in that dictionary : cities = dict(cities) #{'London': 'New York', 'New York': 'Lima', 'Lima': 'Sao Paulo'} for city in cities.values() : if city not in cities.keys() : return city
@user_unknown04
@user_unknown04 Месяц назад
21:27 you probably could have used sets to remove redundant elements, then sort it ( after converting the set into list) then you can proceed further
@bipolarchemist
@bipolarchemist 2 года назад
If the language is Python and you are allowed to 'use' Python, for question 704. Binary Search my initial thought was 'return nums.index(target)'. Why reinvent the wheel unless it was specified that the solution should be faster than the built-in function. Great video and this did definitely get the brain thinking about possible solutions and ways to improve them.
@neilt1352
@neilt1352 2 года назад
.index(target) iterates through the array starting from the 0th index until the array[i] == target. Its just linear search O(n) time. Binary search is O(logn) time.
@chadguru9565
@chadguru9565 2 года назад
My solution was: If the second index of any pair does not exist as the source of any other pairs, then that's the city. def destCity(self, paths: List[List[str]]) -> str: src=[] dst=[] for x in paths: src.append(x[0]) dst.append(x[1]) for d in dst: if not (d in src): return d
@techsudo5170
@techsudo5170 2 года назад
I applied for an SRE role with a company but they also wanted someone with some Python experience, other than building speech to text chatbots and local Jarvis virtual assistants I've never worked with Python on this type of level. I did horribly on the assessment which lead me to this video. Don't know why but I cannot wrap my head around this binary, data parsing or algorithm stuff with Python, it's next level type stuff... Big ups to all that understand this, I think I'll just stick with bash scripting and IAC- Orchestration/Automation and API's using Terraform, Ansible, Packer 😒
@trex9966
@trex9966 3 года назад
For smallerNumbersThanCurrent() could you get rid of the if statement and just overwrite the dictionary values, because the last overwrite will be the correct one to save. Or does writing to a dictionary take more time than the if statement to check?
@chickaberga2
@chickaberga2 3 года назад
Nice I always wanted to be a good Uber hacker dude dudette
@laxmikantnishad6902
@laxmikantnishad6902 3 года назад
Great video! , but can you make a video on data structure and algorithms because I understand from you way better than any other RU-vid channel.
@KeithGalli
@KeithGalli 3 года назад
There's a lot to teach with data structures and algorithms, but it is a topic that I would like to make videos on at some point.
@laxmikantnishad6902
@laxmikantnishad6902 3 года назад
@@KeithGalli, I will wait until then. you are my favorite teacher
@johnhhu2137
@johnhhu2137 2 года назад
Ok, this one works in my condition class Solution: def destCity(self, paths):#: List[List[str]]) -> str: going_count = {} for i in paths: [*cities] = i for j in range(len(cities)-1): going_count[cities[j]] = going_count.get(cities[j], 0) + 1 going_count[cities[-1]] = 0 print(going_count) dest = [] for city in going_count: if going_count[city] == 0: dest.append(city) return dest paths = [['a', 'b', 'c', 'd'], ['a', 'c', 'e', 'f'], ['b', 'c', 'f'], ['d', 'j']]#, ['d', 'c']] # should not use dict solution = Solution() solution.destCity(paths)
@infinitygaming7192
@infinitygaming7192 2 года назад
a2 class Solution: def destCity(self, paths: List[List[str]]) -> str: cat = [] dog = [] for i in range(0,len(paths)): cat.append(paths[i][0]) dog.append(paths[i][1]) for i in range(0,len(dog)): if dog[i] not in cat: return(dog[i])
@samvelo
@samvelo Год назад
cool! video
@TheWorpler
@TheWorpler 2 года назад
interesting and tricky little prob: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: result = [] for i in range(len(nums)): targets = [1 for (idx,x) in enumerate(nums) if idx != i and x < nums[i]] result.append(sum(targets)) return result
@31Chris24
@31Chris24 3 года назад
In example 1365 you could've changed: if j != i to: if j < i that would have made it right. Dont ask me why, lol. I cant come up with an answer for that since < is "less than"
@aquabestindian
@aquabestindian 2 года назад
Thanks Kieth for so many insights. However, for LongestPalindrome, I think the solution won't work for input 'aaabbb', i.e., when more than one letter have odd count
@anonviewerciv
@anonviewerciv 2 года назад
18:00 Mistakes are a part of the process.
@karenkazaryan18
@karenkazaryan18 2 года назад
Love the video, but I dont think the longest palindrome problem is fully correct because if you have 2 instances of odd characters, you would need to only add the count of the largest number of a specific odd character. EX: "abbccccddeee" would need to ignore the odd "a" character but instead would include the 3 odd "e" characters.
@mehdihachimi9624
@mehdihachimi9624 2 года назад
Yup and he forgot also to include cases with UpperCase letters (Racecar) will return 7 instead of the correct answer 5. But great video nonetheless !
@yt-1161
@yt-1161 2 года назад
I think when index method won’t override if it encounters the same element in the list. here is my solution to the 3rd problem @25:00 def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: sorted_nums = sorted(nums) d = {} lst = [] for num in sorted_nums: d[num] = sorted_nums.index(num) for num in nums: lst.append(d[num]) return lst
@rob2093124
@rob2093124 Год назад
For the destination question, would that still work if you made the first path last instead? Seems like New York would be set to 0
@patrickdamour1728
@patrickdamour1728 3 года назад
If you aren’t subbed to Keith’s new channel you are missing out on big brain learning
@KeithGalli
@KeithGalli 3 года назад
🙌🙌
@nathanrotlisberger5022
@nathanrotlisberger5022 Год назад
Hey Keith, I'm not sure if you're still active on this channel but wanted to ask a quick question. Prior to hitting record and starting the video, how much of this knowledge do you already know? Like can you just look at a problem and know what to look up or is there more complex problems where it takes a couple searches? I'm fairly new and decided to watch a coding video and was immediately intimidated by my drastic lack of knowledge... is this normal? Great video and I'll be checking out that other channel!
@RadhamaniRamadoss
@RadhamaniRamadoss 2 года назад
Great tutorial..thankyou..how does self.history[0: self.current_index] clears history in the Design browser history problem? This seems to be creating a substring of array from 0 to current_index. Please clarify
@leviluvr7030
@leviluvr7030 2 года назад
confused on how in the example [-1,3,4,6,8,9,12,15,18] if the target is 10, it would break out of the while loop instead of being stuck inside the while loop
@vamshik8093
@vamshik8093 2 года назад
Very informative content. It’d be even more awesome if you could explain a little more in depth for beginners
@fardinahsan2069
@fardinahsan2069 3 года назад
Hi Keith, This is nit picking but in for i in range(len(x)) loops are anti-patterns, for index,value in enumerate(iterable), is the pythonic way to access the index in a for loop
@KeithGalli
@KeithGalli 3 года назад
Thanks for the tip! Using enumerate would have been a better way for me to go about it.
@iamjohnrobot
@iamjohnrobot 2 года назад
By using the clean builtins that singularly achieve the result you want, you might also piggyback on internal optimizations :]
@puddingsimpson5728
@puddingsimpson5728 Год назад
Is this required for backend python developers interview? Thnks
@chrislione950
@chrislione950 2 года назад
Hey Keith or anyone care to explain what is going on in the function parameter(smallerNumberThanCurrent(self,nums ...) I dont understand what is going on there.Thanks
@alirahal5854
@alirahal5854 2 года назад
If we are appending the root to the output array in each iteration. How did we end up having subarrays in the output ? This confused me any help is appreciated
@MattBonk991
@MattBonk991 3 года назад
Do you have any suggestions for the GME thing for me? Im getting started on this crap
@NervylHraje
@NervylHraje 3 года назад
Haha, good one.
@akash-uy3uh
@akash-uy3uh 2 года назад
destination city : (easier way) lazy way :P paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]] dic = dict(paths) origin = list(dic.keys()) destination = list(dic.values()) ans = set(destination).difference(set(origin))
@rohans875
@rohans875 3 года назад
For the binary search question, couldnt you just do something like try: return (nums.index(target)) except: return "-1"
@herp_derpingson
@herp_derpingson 3 года назад
Python's .index method implements linear search
@prathameshvaidya3508
@prathameshvaidya3508 2 года назад
SIMPLE SOLUTION: a=[8,1,2,2,3] m=[] b=0 for i in range(0,len(a)):#0 to 4 for j in range(0,len(a)):#0 to 4 if a[i]>a[j]: b+=1 m.append(b) b*=0 print(m)
@infinitygaming7192
@infinitygaming7192 2 года назад
a3 class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: cat = sorted(nums) lol = [] for i in range(0,len(nums)): lol.append(cat.index(nums[i])) return(lol)
@Ihavetoreturnsomevideotapes
@Ihavetoreturnsomevideotapes 3 года назад
Dude , people are gonna think you are u/deepfuckingvalue . The guy who started the GME🚀🚀🚀
@KeithGalli
@KeithGalli 3 года назад
Lmao yeah, our names are super similar and we're both from the boston area. Can't be just a coincidence. 🚀🚀🚀
@dariuszspiewak5624
@dariuszspiewak5624 Год назад
Another solution (shorter, maybe faster, using generators): class Solution: def destination_city(self, paths: List[List[str]]) -> str: for dest_city in (dest for start, dest in paths): if dest_city not in (start for start, dest in paths): return dest_city or class Solution: def destination_city(self, paths: List[List[str]]) -> str: from itertools import compress starts = set(start for start, dest in paths) destinations = tuple(set(dest for start, dest in paths)) dest_city, *_ = compress( destinations, [dest not in starts for dest in destinations] ) return dest_city or (the solution from the vid "optimized' a bit :) class Solution: def destination_city(self, paths: List[List[str]]) -> str: from collections import defaultdict outgoing_count = defaultdict(int) for path in paths: citya, cityb = path outgoing_count[citya] += 1 outgoing_count[cityb] = outgoing_count[cityb] for city, count in outgoing_count.items(): if count == 0: return city and maybe the shortest... class Solution: def destination_city(self, paths: FlightConnections) -> str: return next(iter( set(e for _, e in paths) .difference(set(s for s, _ in paths)) ))
@chriskeo392
@chriskeo392 2 года назад
He got drake eye brows
@dariuszspiewak5624
@dariuszspiewak5624 Год назад
For the 1365 challenge... Why are you sorting this in the reversed order? If you do it in the ascending order, it'll be so much easier to get the number of numbers smaller than the current one! It'll be just the index of the first occurence of the current number in the sorted list. No need to do any mathematical (and obscure) operations on the indexes. Also, no need to append constantly to an array. You can initialize an array with the right size upfront and then just assign to the slots in it. This will be soooooo much faster as the list will not need to be resized (and copied!). Not a single time. By the way, the runtimes, when submitted, can vary wildly. This probably depends on the workload on the server. I've checked it and the same solution can be the fastest one or... far, far away from the fastest. This is hugely random, which is really sad as there's no consistency here.
@iamjohnrobot
@iamjohnrobot 2 года назад
You miss the use of a lot of simple, easy to read builtins
@dantewizdom5002
@dantewizdom5002 2 года назад
Hi, I tried the sharing between friends question but got no output data = [2, 4, 1, 2, 7, 8] Alice = 0 Roso = 0 Bob =0 def split_data(data, Alice, Roso, Bob): share = len(data)//2 while len(data) != 0: Alice += data[0] Roso += data[1] Bob += data[2] return Alice return Roso return Bob
@tonymudau3005
@tonymudau3005 2 года назад
im not smart enough for this : ( are these really easy?
@KeithGalli
@KeithGalli 2 года назад
Work your way up! They don't start out easy, but get easier & easier as you do more. A good place to start for problems like this is codingbat.com/python
@jacktrainer4387
@jacktrainer4387 Год назад
They assign these in gulags in lieu of hard labor.
@KeithGalli
@KeithGalli Год назад
haha, I also heard that in order to enter Silicon Valley you have to answer several of these questions.
@lucky4the4turtle
@lucky4the4turtle 3 года назад
Did he get a haircut?
@aharongina5226
@aharongina5226 9 месяцев назад
why do you need to see your face on the video?
@abhinavsharma3819
@abhinavsharma3819 2 года назад
Destination City out_city = {path[0] for path in paths} for path in paths: if path[1] in out_city.keys(): out_city.pop(path[1]) The only remaining key would be the answer
@anirudhinamerica8218
@anirudhinamerica8218 Год назад
Problem: LeetCode 1365: leetcode.com/problems/how-man... class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: output = [] for i in nums: count = 0 for j in nums: if i != j and i > j: count += 1 output.append(count) return output
Далее
How I would learn Leetcode if I could start over
18:03
Просмотров 295 тыс.
How to NOT Fail a Technical Interview
8:26
Просмотров 1,3 млн
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
5 Useful F-String Tricks In Python
10:02
Просмотров 271 тыс.
12 Beginner Python Projects - Coding Course
3:00:29
Просмотров 4,3 млн
Physicists Claim They Can Send Particles Into the Past
7:21
Data Analysis with Python for Excel Users - Full Course
3:57:46
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Lecture 1: Introduction to CS and Programming Using Python
1:03:30