Daily Dose of LeetCode: Two Sum
Vložit
- čas přidán 15. 06. 2024
- Hello everyone, this is YOUR Daily Dose of LeetCode (Definitely didn't copy @DailyDoseOfInternet )
In this video, we're tackling the popular Two Sum problem. We'll not only discuss the key takeaways but also delve into understanding the best possible runtime, and explore both brute force and optimized solutions.
Mastering problems like Two Sum is a critical step in your journey as a software developer and in acing that coding interview.
Remember to like, comment, and subscribe to keep up with our LeetCode problem-solving guides, and let me know in the comments if you have any other problems you'd like me to cover.
(Had to reupload since there was a mistake in the brute force solution, whoops)
// TIMESTAMPS //
00:00 - Intro
00:19 - What are the key takeaways?
00:43 - What's the best runtime that we can do?
01:00 - Brute Force Solution
01:35 - Optimized Solution
// SOCIALS //
Twitter: / thecodingsloth1
TikTok: / thecodingsloth
#leetcode #twosum #python #codinginterview #algorithm #programming #coding #pythonprogramming #codingtutorials #computerscience
the funny thing is, two loops is actually faster, because checking and adding to a hashmap is an expensive step. So even if it only took one loop, you are doing a lot more work and taking more space in memory in each iteration which demonstrates that O notation is a flawed way of evaluating solutions.
is that true for c++ as well?
@@zalanborbely9177 yes, you are at least doubling the memory usage, creating a runtime map and adding an additional check. you might think you only looped once, but in each iteration you are going through the hashmap that's already there, adding to it in the loop as well.
saying 1 loop is better than 2 loops doesn't take into account that one of them is an fat or expensive loop while the other is a slim loop.
@@amrrahmy123 thats really interesting, but in interviews they still want you to do the hash map solution i assume
I loved this format of video. Keep it up man
Why you have just 539 subscribers? Underrated, for no reason.
woah he blew up in a month, 16.2k rn
Right now - 87.1k
like that you are at top of my search and its grate for you as a starter on you tube. I also liked your style of video can you tell me what and how you do that. like those subtitle and sketches.
Thanks! I'm using Adobe Premiere for all my editing, subtitles, and sketches
they assigned me this problem during my interview but I had to find all pairs and not include dups
Seems easy just keep a hast table that is pair to bool which keeps track of if the pair is part of answer or not. Did you solve it?
@@Trizzi2931 yeah and gott the job :)
Bro is there a good resource for DSA in python. Most resources are in c++/java. Stuck between learning DSA first in C++ then thinking to switch to python. But it's all time consuming.
Neetcode
keep it up bro see if you can use c++
Thanks bro, I'll try to add multiple languages in other videos, currently thinking of Python, C++, and Java
the space complexity can be eliminated if you just use 2 pointers.
while (arr[a] + arr[b] != target)
if arr[a] + arr[b} > target; b b--
else a++;
That being said though, space complex solutions are usually prettier. Basically every recursive function always has a o(n) space complexity and it sucks because they're so beautifully simple.
That assumes that the list is sorted, which it might not be.
@@tianchengteh1695
That's fair, any of the actual test cases would have rekt that strategy (now that I've actually looked)
@@Snollygoster- yeah lol
had to watch it in 0.5X haha
i'm cooked
Bruh Can do better we can have O(N) Time and no space comlexity use 2 pointers