Sqrt(x) | LeetCode 69 | Coding Interview Tutorial

Sdílet
Vložit
  • čas přidán 7. 09. 2024

Komentáře • 44

  • @TerribleWhiteboard
    @TerribleWhiteboard  Před 4 lety +13

    If there are any videos you'd like me to make or if you have any ideas on how to optimize this solution, let me know!

  • @arif3012
    @arif3012 Před 4 lety +17

    Played so many videos on the same problem, until I stopped here and understood finally. Awesome!!

  • @willowsongbird
    @willowsongbird Před 3 lety +6

    why don't we use mid as Math.floor((right+left)/2) just like the formula for finding average

  • @MrZiyak99
    @MrZiyak99 Před 4 lety +8

    you're my go-to source for algos. I'm trying to go through your entire playlist of algos. So far you've taught me so much. Thanks a ton the quality vidoes and explanation!

  • @sunnilabeouf
    @sunnilabeouf Před 4 lety +6

    This channel should be renamed to Amazing Whiteboard because this is one of the best algorithmic explanations i've seen lmao (even for such a simple question!). Really hope you can keep these videos coming :)

  • @itech40
    @itech40 Před 4 lety +15

    Well you went far, you have so much patience to do that for something so simple xD! GG Liked.

  • @piyushsharma1638
    @piyushsharma1638 Před 4 lety +15

    Thanks for the easiest explanation, just for doubt why we don't use right = mid - 1 but we used left = mid + 1

    • @TerribleWhiteboard
      @TerribleWhiteboard  Před 4 lety +23

      Remember that if no midpoint ends up being the square root, we're going to return left - 1 (left and right will be the same in that case, so returning right - 1 would also work), meaning that the number left and right end up on is one greater than it should be. The only way this would work is if we don't automatically eliminate the midpoint by making right one less than it. We have to leave open the possibility that the midpoint is the number we're eventually going to have to subtract one from.

    • @piyushsharma1638
      @piyushsharma1638 Před 4 lety +11

      @@TerribleWhiteboard Thanks :)

    • @alxx736
      @alxx736 Před rokem

      @@TerribleWhiteboard I think it should have more sense to eliminate always the midpoint to avoid confusion

  • @Sportsarena8
    @Sportsarena8 Před 4 lety +4

    you're the best. keep making these type of videos. thank you so much for taking the time in creating all this amazing content

  • @darod6098
    @darod6098 Před 4 lety +13

    Thanks for you video! Good explanation.
    Probably Math.floor in line 14 is not necessary because integers are always rounded to floor by default?

    • @TerribleWhiteboard
      @TerribleWhiteboard  Před 4 lety +17

      No problem! You're right that in Java, integers are rounded down, but my videos are in Javascript.

    • @darod6098
      @darod6098 Před 4 lety +14

      @@TerribleWhiteboard Yep, i got it later haha thank you!

  • @callmechocolateboy
    @callmechocolateboy Před 4 lety +7

    good video keep making more on leetocdes

  • @heagandev
    @heagandev Před 2 lety

    Well explained!
    Three suggestions:
    - left = 2 (since we're already not going to consider 0 or 1)
    - right = x / 2 (since the square root will never be greater than half of x)
    - mid = Math.floor((left + right) / 2) (simpler equation)

  • @MS-rw3rh
    @MS-rw3rh Před 2 lety

    before finishing the video, I clicked like because I liked your explanation

  • @snowie030
    @snowie030 Před 2 lety

    Love you!! You explained it so clearly & simple, thanks a lot

  • @mauricioencina6764
    @mauricioencina6764 Před 4 lety

    Thanks for the video. The solution overflows for big int numbers, at least in C#. I worked around that by working with longs instead of ints and then casting the long to int before returning.

  • @Joyddep
    @Joyddep Před 3 lety

    One thing to do to avoid integer overflow for other languages, compare mid with mid / x like " mid < mid / x" or "mid > mid / x". Thanks for the explanation.

  • @DoJoStan
    @DoJoStan Před 4 lety

    I think you can remove the "if(mid * mid === x)" statement on line 16 and make the while loop "while (left

  • @sachinshilwal71
    @sachinshilwal71 Před 2 lety

    such a good explanation. bro you are the best

  • @mohammednishad4055
    @mohammednishad4055 Před 3 lety

    very simple explanation, hope you will upload more leetcode problems.

  • @alonshlider4881
    @alonshlider4881 Před rokem

    THANK YOU FOR SPEAKING NON INDIAN ENGLISH! 🙏

  • @sagardhiman5181
    @sagardhiman5181 Před 3 lety

    Thanks for the explanation.., we can initialize right = x/2 bcz sqrt will never be b/w x/2 to x.

  • @AbhishekSrivastava_ab
    @AbhishekSrivastava_ab Před 2 lety

    Why not start the input array with N/3. We logically know that in N/2, the number near to the middle element of the array can never be the answer. Unless its 4 or -4.

  • @tejasmehta5215
    @tejasmehta5215 Před 2 lety +1

    Why midpoint is not -> (left+right)/2

  • @superoriginalname
    @superoriginalname Před 2 lety

    I don't understand why after eliminating the numbers to the right, you still keep the midpoint but when eliminating numbers to the left, you get rid of it.. why the disparity

  • @IRLVaporwave
    @IRLVaporwave Před rokem

    I think you should rebrand to "Terrific Whiteboard"

  • @ngozikalu6938
    @ngozikalu6938 Před 3 lety

    Can't we make right x/2 since the square root of a number can't ever be bigger than half the number?

  • @jervex-e3514
    @jervex-e3514 Před 2 lety

    thank uu :)

  • @kilikia8939
    @kilikia8939 Před 3 lety

    but if i was programming in C++ i had RE, because i had int overflow (

  • @midnightrun5
    @midnightrun5 Před 3 lety

    You are f- awesome!

  • @29_b1_jain_nishit2
    @29_b1_jain_nishit2 Před 3 lety +1

    Nice

  • @vamsiKrishna96
    @vamsiKrishna96 Před 3 lety

    Hey, which app are you using to give an explanation?

  • @vishal84000
    @vishal84000 Před 2 lety +1

    let me give you a much easier solution
    var mySqrt = function(x) {
    let i = 1;
    while(i*i

    • @aiai52
      @aiai52 Před 2 lety

      His solution is O(log(n)), yours is O(sqrt(n))