Two Sum | LeetCode 1 | JavaScript | Easy

Поделиться
HTML-код
  • Опубликовано: 26 янв 2025

Комментарии • 56

  • @willbdev
    @willbdev 9 месяцев назад +5

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

    • @GordonZhu
      @GordonZhu  9 месяцев назад

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

    • @GordonZhu
      @GordonZhu  9 месяцев назад

      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: ruclips.net/p/PLViRFZPCqDBekVh0L_cyg6DLl2fPYbAJM.

  • @shantanu925
    @shantanu925 10 месяцев назад +9

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

    • @GordonZhu
      @GordonZhu  10 месяцев назад

      Thanks Shantanu. Appreciate the support :)

  • @germain1984
    @germain1984 Год назад +32

    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  Год назад +1

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

    • @learnerSarfaraz
      @learnerSarfaraz 9 месяцев назад

      you expressed the thought so well, i m even pleased

  • @jim.....
    @jim..... Год назад +23

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

    • @GordonZhu
      @GordonZhu  Год назад +1

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

  • @janelledement
    @janelledement Год назад +4

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

    • @GordonZhu
      @GordonZhu  Год назад +1

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

  • @antonionelson2525
    @antonionelson2525 20 дней назад +1

    I loved this... subscribed and liked!!!!

  • @gyrozepelix
    @gyrozepelix Год назад +3

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

  • @ranga8152
    @ranga8152 Год назад +1

    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

  • @Mahfy3w
    @Mahfy3w Год назад +3

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

  • @anthonyxie4550
    @anthonyxie4550 Год назад +1

    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  Год назад

      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.

  • @grace.newlife
    @grace.newlife 6 дней назад

    Thank you so much for the detailed analysis. I just started LeetCode and struggled with it, but now I think I can make it! Thank you for your guidance❤

    • @GordonZhu
      @GordonZhu  5 дней назад

      You're welcome Grace! Keep working at it!

  • @existentialism_01
    @existentialism_01 3 месяца назад

    this channel is so underrated, today i discovered Two Sum problem and decided to know more about it, thanks for this video

    • @GordonZhu
      @GordonZhu  3 месяца назад +1

      Appreciate this a lot and you're welcome!

  • @Fido1hn
    @Fido1hn 5 месяцев назад

    This is the most clear explanation of an algorithm I have ever seen. Awesome, subscribing now. Hoping to see more. Thank you for this beautiful explanation it helps a lot.

    • @GordonZhu
      @GordonZhu  5 месяцев назад

      You're welcome and thank you! I have a full playlist with more videos like this and am working to add more:
      ruclips.net/p/PLViRFZPCqDBekVh0L_cyg6DLl2fPYbAJM

  • @AllanRolle
    @AllanRolle 3 месяца назад

    Incredible !
    I need to get my algorithmic logic together in order to pass interview tests. It's my main weakness. I very good with html and css but not abstract thinking. And you are right when you say that if you observe and focus on the questions/hints, it takes you to the solution.

    • @GordonZhu
      @GordonZhu  3 месяца назад +1

      Thanks Allan! I'm glad you noticed how important the line of reasoning is (represented by the question path). Most people don't understand the significance and think it's just a visual flourish.

  • @KennyChowPD
    @KennyChowPD Год назад +1

    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  Год назад +2

      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 Год назад

      @@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  Год назад +1

      ​@@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?

  • @micaoszczakiewicz4980
    @micaoszczakiewicz4980 4 месяца назад

    awesome visual representation!!

  • @Timeus80
    @Timeus80 7 месяцев назад

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

    • @GordonZhu
      @GordonZhu  7 месяцев назад +1

      Thank you @Timeus80 :). Check out my full playlist of similar videos here (I just added a new one today): ruclips.net/p/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).

  • @thelogicalladka5866
    @thelogicalladka5866 2 месяца назад

    Best explanation on the internet 🎉

    • @GordonZhu
      @GordonZhu  2 месяца назад

      Thank you Pranav!

  • @babyboie20
    @babyboie20 9 месяцев назад

    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  9 месяцев назад

      Thanks Anthony! That means a lot.

  • @HHJoshHH
    @HHJoshHH 4 месяца назад

    Awesome video bro! 😎

  • @AleySoundz
    @AleySoundz 10 месяцев назад

    Thank you for the visual learning!

    • @GordonZhu
      @GordonZhu  10 месяцев назад

      Glad you enjoyed it!

  • @subinaypanda9936
    @subinaypanda9936 Год назад +1

    Woo that so much clear

  • @LPnotes
    @LPnotes Год назад

    Great work! 👏

  • @alifrahman7099
    @alifrahman7099 6 месяцев назад

    thanks

  • @NetFolg
    @NetFolg Месяц назад

    I like the explanation! But also, I'm confused a little. Can you explain why you take the average for the inner loop (n/2) but not the (n-1)? Thank you🙏

    • @BurrritoYT
      @BurrritoYT 13 дней назад

      It doesn’t matter in big O notation. As long as there isn’t a growing multiplicative discrepancy between two algorithms, their complexity is the same.

  • @sujith9435
    @sujith9435 7 месяцев назад

    subscribed

  • @PedroLeite-q6q
    @PedroLeite-q6q 6 месяцев назад

    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  6 месяцев назад +2

      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.