Two Sum | LeetCode 1 | JavaScript | Easy

Ғылым және технология

In painstaking detail, I go through my entire thought process for solving Two Sum from first principles. We'll start with the essential insights that lead us to the brute-force solution. From there, we'll optimize to get various performant O(n) time approaches. Finally, we'll discuss the pros/cons of our performant approaches.
My teaching work: watchandcode.com
Two sum problem: leetcode.com/problems/two-sum/
0:00 Two sum problem statement
0:21 Initial thought process
1:14 Brute-force implementation & analysis
3:03 Optimizing our initial approach
7:28 Implementing a faster algorithm
7:50 What about the one-pass approach?
8:20 Coming up with the one-pass
9:55 Why the one-pass is so awkward
12:03 The most important part

Пікірлер: 38

  • @germain1984
    @germain198410 ай бұрын

    I am a visual learner and have been a software engineer for over 10 years. This is by far the most clear (and beautiful) explanation of any algorithm I've ever seen. I will be anticipating with excitement your next videos. Please keep this format!

  • @GordonZhu

    @GordonZhu

    10 ай бұрын

    Thank you Germain! I really like how this turned out and I'm excited for what's next too!

  • @learnerSarfaraz

    @learnerSarfaraz

    3 ай бұрын

    you expressed the thought so well, i m even pleased

  • @jim.....
    @jim.....10 ай бұрын

    To teach this way requires not only meticulous technique, but also a high dose empathy. Great work as always

  • @GordonZhu

    @GordonZhu

    10 ай бұрын

    Thank you Jim! Yeah I'm always trying to get inside the mind of my students. Makes all the difference.

  • @shantanu925
    @shantanu9254 ай бұрын

    Very underrated channel. Very creative way to teach! You're going to go big in future for sure!

  • @GordonZhu

    @GordonZhu

    4 ай бұрын

    Thanks Shantanu. Appreciate the support :)

  • @gyrozepelix
    @gyrozepelix9 ай бұрын

    Love your video mate, wish you luck with your future videos

  • @ranga8152
    @ranga81529 ай бұрын

    This was really excellent. The animation for the brute force solution was such a useful and concrete way of holding the solution in my head. It made it much easier to follow each of your questions, and the systematic way you approached improving on your initial solution, all while being assured of correctness. The take-aways for me: - look for insights to solve the problem by diagramming - use prompts to improve your solution e.g. perfect information - don't code until you're certain of your approach - use simple examples to test out ideas

  • @janelledement
    @janelledement10 ай бұрын

    Wow! I love how you explain how the one pass solution is actually not great (for clarity and mental overhead). Excellent video!

  • @GordonZhu

    @GordonZhu

    10 ай бұрын

    Glad you liked that part specifically :) It had to be said!

  • @cryptoaddict6715
    @cryptoaddict67153 ай бұрын

    The best breakdown I have ever came across! EVER. Thank you for the intense amount of work that went into this video.

  • @GordonZhu

    @GordonZhu

    3 ай бұрын

    Thanks so much! Haha yeah it was a sh**load of work.

  • @GordonZhu

    @GordonZhu

    3 ай бұрын

    If you liked this, you'll also like my other LeetCode videos. There are just a few now but I'm planning to add more: kzread.info/head/PLViRFZPCqDBekVh0L_cyg6DLl2fPYbAJM.

  • @Mahfy3w
    @Mahfy3w9 ай бұрын

    Dude, this is very good. I hope your channel grows

  • @GordonZhu

    @GordonZhu

    9 ай бұрын

    Thank you!

  • @anthonyxie4550
    @anthonyxie45509 ай бұрын

    Amazing video! I love that you begin with the simplest brute force approach and improve on it incrementally. The questions you ask yourself during the problem solving process are really useful. They give me a clue on how I could come up with the solutions on my own. Other Two Sum videos I watched skipped straight from brute force to the one pass approach. I like that you provided the two pass solution and also explained that it's arguably the better approach in comparison to the one pass.

  • @GordonZhu

    @GordonZhu

    9 ай бұрын

    Thanks Anthony! Yeah prompting yourself with these types of questions is the absolute key to coming up with the solutions on your own. Glad you noticed this. I tried as hard as I could to emphasize this message. It's like using the socratic method with yourself.

  • @Timeus80
    @Timeus80Ай бұрын

    Great job! Thank you so much! Love the mindset and the realization of the video. Subscribed right away :)

  • @GordonZhu

    @GordonZhu

    Ай бұрын

    Thank you @Timeus80 :). Check out my full playlist of similar videos here (I just added a new one today): kzread.info/head/PLViRFZPCqDBekVh0L_cyg6DLl2fPYbAJM. Also very cool to see that you're a professional musician. My students that are also professional musicians have said that my approach to programming reminds them a lot of music (importance of fundamentals, attention to nuances in method/technique, ability to find weaknesses objectively and correct them, importance of quality feedback, etc).

  • @AleySoundz
    @AleySoundz4 ай бұрын

    Thank you for the visual learning!

  • @GordonZhu

    @GordonZhu

    4 ай бұрын

    Glad you enjoyed it!

  • @KennyChowPD
    @KennyChowPD10 ай бұрын

    Wonder if they make sense; 1) numberToIndex[num[i]] = i has to go last: Avoid returning the same index when num[i] is exactly half the target. 2) numberToIndex[numberNeeded] !== i is not needed anymore: When numberToIndex[num[i]] = i goes last, the current index i will not be in the map YET. So in the case of two elements(different indexes) having the same value(exactly half the target), index i and the index stored at numberToIndex[numberNeeded] will always be different and correct. Production value is great Gordon! Love the simple yet very functional animations in terms of visualizing the thought process.

  • @GordonZhu

    @GordonZhu

    9 ай бұрын

    It's great that you thought more about this!(I'm assuming you're talking about the two prompts I mentioned around 9:21.) For your first point, assume that the target pair is two of the same number (e.g. [5, 5], target = 10). If you put `numberToIndex[numberNeeded] !== i` at the top of the loop, when you examine the second 5, you'll wipe out information about the first 5, making it impossible to ever return the correct pair. Your reasoning implies that there may be a selection issue that you could somehow fix it if you avoided reusing the same index. I think this is a subtle but important distinction. So what you're saying is true, but obfuscates the key issue.

  • @Lauchmeister

    @Lauchmeister

    9 ай бұрын

    @@GordonZhu About the first point: Would we ever reach the second 5 though? If we are at the first 5, add it to the map & then run the check, it should return the index twice (once as variable i, once as value of key 5). Or did your question assume that we first only move the mapping of the number/index to the end but still keep the 2nd condition in the if clause? Because then what you described (overwriting first occurence) would happen. It looks like Kenny & I both assumed that we make both changes at once.

  • @GordonZhu

    @GordonZhu

    9 ай бұрын

    ​@@Lauchmeister Sure, that's what would happen in the situation you described, but that's not what I was describing. Note the specific sequence I followed in the video. 1) First, why do you need to write to the map at the bottom and not at the top? At that time, the index check was present. 2) Second, now that you're writing to the map at the bottom (because you know you have to), why is the index check no longer necessary?

  • @subinaypanda9936
    @subinaypanda99369 ай бұрын

    Woo that so much clear

  • @LPnotes
    @LPnotes9 ай бұрын

    Great work! 👏

  • @GordonZhu

    @GordonZhu

    9 ай бұрын

    Thanks Linda!

  • @babyboie20
    @babyboie203 ай бұрын

    Holy crap! Best visuals for thinking about algorithms wrt ds+a, really helps me visualize the solution. I don’t know how I didn’t find your channel sooner but please continue putting out bangers like this! Funny pat is I had already solved this problem before and I actually solved it using the last solution because that was the first thing I thought of. I never thought of the one pass solution but the part I think I need to practice and learned the most from was the questions you asked to come up to the solutions. I never thought or the idea of “perfect information”, was that something you came up with yourself or a concept formalized somewhere where I can learn more?

  • @GordonZhu

    @GordonZhu

    3 ай бұрын

    Thanks Anthony! That means a lot.

  • @alifrahman7099
    @alifrahman709920 күн бұрын

    thanks

  • @sujith9435
    @sujith9435Ай бұрын

    subscribed

  • @GordonZhu

    @GordonZhu

    Ай бұрын

    Thank you!

  • @user-xh4ec2hz7d
    @user-xh4ec2hz7d29 күн бұрын

    One question, from a non CS graduate, why this solution doesn’t also jump to the next element if bigger or equal to the target sum? Thus reducing the map size and evaluation of previous elements, considering no negative numbers. Second the one pass solution also has a different effect, might not guarantee that you have chosen the bigger index for the value, as you might not have encountered it before finding the solution. Also the two pass might have an issue if you need two identical values for the target, with two different indexes.

  • @GordonZhu

    @GordonZhu

    27 күн бұрын

    1. You can't assume no negative numbers. The question says "integers". 2. I exhaustively explain why the 1-pass is guaranteed to work at 9:58 - 11:30. 3. I directly answer your last question at 6:18.

Келесі