Astroid Collision

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ย. 2024
  • For business inquiries email partnerships@k2.codes SOCIAL
    ----------------------------------------------------------------------------------------------------------------
    Instagram: / kevinnaughtonjr
    Twitter: / kevinnaughtonjr
    Patreon: / kevinnaughtonjr
    Merch: teespring.com/...
    GitHub: github.com/kdn251
    MUSIC
    ----------------------------------------------------------------------------------------------------------------
    kanye west ft. lil pump - "i love it" (lofi remix by ofey) by ofey
    / i-love-it

ความคิดเห็น • 71

  • @KevinNaughtonJr
    @KevinNaughtonJr  4 ปีที่แล้ว +10

    LYFT > UBER FIGHT ME IN THE COMMENTS BELOW
    instagram: instagram.com/kevinnaughtonjr/
    twitter: twitter.com/KevinNaughtonJr
    patreon: www.patreon.com/KevinNaughtonJr
    merch: teespring.com/stores/kevin-naughton-jrs-store

    • @varungupta2045
      @varungupta2045 4 ปีที่แล้ว +9

      I live in India, we don't have lyft here. Case closed. 😂

  • @albertreyes2167
    @albertreyes2167 4 ปีที่แล้ว +21

    I was never interviewed by lyft nor uber. I just started driving after background check 24 hours later

  • @jak0495
    @jak0495 4 ปีที่แล้ว +10

    Lyft's most asked question is "how many coupons will it take to get someone to use lyft over uber?"

  • @ashishsingh-ty6kf
    @ashishsingh-ty6kf 4 ปีที่แล้ว +2

    i am not able to understand if you are only pushing positive value in the stack than why checks for negative in the stack?

  • @surendra9935
    @surendra9935 4 ปีที่แล้ว +8

    Oh my gosh, I have to learn a lot from you...Lots of Determination, Dedication, Discipline. You are consistent on what you are doing and there is no Friday nights fun. Great dude keep it going, I will follow you 🤗

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว

      Surendra thanks really appreciate the kind words and your support!!!

  • @JM_utube
    @JM_utube 4 ปีที่แล้ว +1

    using a stack is pretty intuitive, but the nuances are a little complicated. this deserves medium difficulty because of the repeated collisions from a new asteroid. good job on this video.

  • @lings628
    @lings628 4 ปีที่แล้ว +1

    Thank you very. much. I like how to justify which data structure to use with some explanation, instead of just saying let's go ahead and use a stack for this problem. Huge fan of your channel. Keep up the fantastic work.

  • @chickendolo4437
    @chickendolo4437 4 ปีที่แล้ว +12

    How do you come up with solutions to the hard or medium questions on leetcode? And keep it going dude ur channel is amazing 😊.

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว +7

      Chicken Dolo lots of practice and thanks :)

  • @N1KH1L_777
    @N1KH1L_777 4 ปีที่แล้ว +5

    OMG I was literally on Lyft's questions and was doing this.. lol. Thanks Kevin. Can you also do Oracle's most asked question - #425 - Word Squares (hard)? Thanks in advance man

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว +4

      Nikhil Mathur haha no way that’s amazing I’ll put it on my list

  • @mostinho7
    @mostinho7 4 ปีที่แล้ว +4

    When you asked us to pause the video and think of a solution, I thought of solving this by starting from the left to right and detecting the first collision (the first positive asteroid that is followed by a negative one), then we “collide them”, see which one survives and pass the array after the collision recursively to the same function. With the base condition being that we detect no more collisions in the array. (Or a for loop instead of recursion to save on memory of call stack)
    I wanted to ask you, first is this a crap solution? And second the time complexity of such solution would be O(n^2)?

    • @neslzkusfep
      @neslzkusfep 4 ปีที่แล้ว

      You're right. The time complexity is definitely O(n^2) cause Kevin is using a nested loop.

    • @mannyruiz7880
      @mannyruiz7880 ปีที่แล้ว

      @@neslzkusfep a monotonic stack is O(n)

    • @neslzkusfep
      @neslzkusfep ปีที่แล้ว

      @@mannyruiz7880 Yep I was wrong about the time complexity 2 years ago. Kevin's solution is O(n) because we are only pushing and popping each asteroid at most once. Access and search operations for a monotonic stack are O(n), but push and pop operations are O(1). The time complexity of the solution is O(n) because we are iterating through the entire array of asteroids once and then we are pushing and popping each asteroid at most once in O(1) time.
      So O(n) (array iteration) + O(1) (stack operations) = O(n).

  • @HornOKPlease
    @HornOKPlease 3 ปีที่แล้ว

    why does his drip go so hard

  • @SomeshwarJ
    @SomeshwarJ 2 ปีที่แล้ว

    I just amazed for this simple solution 😃

  • @amontokoro9357
    @amontokoro9357 4 ปีที่แล้ว +2

    Thanks for an amazing video! This might be dumb, but I still do not get why [-2,-1,1,2] returns itself. I thought it would return an empty array. In fact, [-3,-2,2,3] returns an empty array.

    • @pratikchowdhury9210
      @pratikchowdhury9210 4 ปีที่แล้ว

      did you mean [-2,-1,1,2] and [3,2,-2,-3] ?

    • @amontokoro9357
      @amontokoro9357 4 ปีที่แล้ว

      @@pratikchowdhury9210 Sorry, yes. Fixed

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว +2

      Not dumb! I'm assuming you mean [-2,-1,1,2] and it's because the first two (-2, and -1) are moving left (and will never collide with each other) and the other 2 (1 and 2) are moving right (and will never collide with each other). This example of the equivalent of us (representing an asteroid each) putting our backs against each other walking opposite directions i.e. we'll never collide

    • @amontokoro9357
      @amontokoro9357 4 ปีที่แล้ว

      @@KevinNaughtonJr Thank you for your reply Kevin! I understand what you are saying. However, based on your explanation, is [-3,-2,2,3], asI mentioned above, supposed to return itself because there is no collide?

    • @stonecoldcold2941
      @stonecoldcold2941 4 ปีที่แล้ว +1

      @@amontokoro9357 it should

  • @rjose705
    @rjose705 4 ปีที่แล้ว +5

    subscribed to you! I like interview questions on my timeline and i love how your channel is dedicated to this. Also can u answer my other comment.

  • @joseph615
    @joseph615 4 ปีที่แล้ว +1

    What is the difference between stack.push() and stack.add()?

  • @darod6098
    @darod6098 4 ปีที่แล้ว +1

    Niiiiiiice problem. Didnt think in a Stack! Using it makes the problem more easy than medium. Thank you :)
    btw: do you know if it can be odne in place? seems like yes

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว +2

      Anytime and in place meaning modifying the original array?

    • @darod6098
      @darod6098 4 ปีที่แล้ว

      @@KevinNaughtonJr yes! I mean, not creating the stack

  • @bambingbang
    @bambingbang 4 ปีที่แล้ว +1

    Awesome bro! It is really helpful for me to learn how to explain our solution to our interviewer!

  • @kitch3876
    @kitch3876 2 ปีที่แล้ว

    Heya, no idea if you do this already or not but it would be really helpful to go over space and time complexity as well at the end :) Thanks for the video!

  • @annawilson3824
    @annawilson3824 3 ปีที่แล้ว

    Thanks for the video! All great, regarding the final part, is it just me or the explanation why we need 'remaining' array populated backwards was kind of unclear? In Python, we would just simply return stack. Here, we populate the 'remaining' array from the end since stack returns items in this order: last, last-1, last-2, so to convert it into remaining, we also start with populating last item of remaining, then last-1, etc.

  • @brijeshtiwari1357
    @brijeshtiwari1357 3 ปีที่แล้ว +1

    Thanks man, It was seriously helpful :)

  • @qazyhn94
    @qazyhn94 2 ปีที่แล้ว

    Isn't complexity is
    On^2?

  • @rohitjaiswal4121
    @rohitjaiswal4121 4 ปีที่แล้ว

    Please make a series on Uber and Lyft interview questions! Thanks for your efforts. You're doing great!

  • @satyamgaba
    @satyamgaba 3 ปีที่แล้ว

    Seemed like a divide and conquer problem to me

  • @atrulyrandomwalk
    @atrulyrandomwalk 2 ปีที่แล้ว

    Between line 10-11, shouldn’t it be - Math.abs(aesteroids[i]). As we are trying to compare the stack value vs absolutely value of potential collision element. So while(….&& stack.peak() < Math.abs(aesteroids[i]))
    @kevin

  • @darthvader_
    @darthvader_ 3 ปีที่แล้ว

    Haha! The sound at the last

  • @saravanansarangan7035
    @saravanansarangan7035 4 ปีที่แล้ว

    Great explanation Kevin!

  • @SR-we1vl
    @SR-we1vl 3 ปีที่แล้ว +1

    Why these kinda solutions never come in my mind!

  • @santasingh9045
    @santasingh9045 3 ปีที่แล้ว

    great solution Kevin!

  • @goodwish1543
    @goodwish1543 4 ปีที่แล้ว

    735. Asteroid Collision. Excellent! Thanks.

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว

      anytime!

    • @goodwish1543
      @goodwish1543 4 ปีที่แล้ว

      Just completed a similar problem, 821. Shortest Distance to a Character

  • @rjose705
    @rjose705 4 ปีที่แล้ว +2

    This O(n^2) implementation? I see 2 loops

    • @hacker-7214
      @hacker-7214 4 ปีที่แล้ว

      just cuz u see 2 loops doesnt automatically mean n^2.

    • @sahilkalamkar5332
      @sahilkalamkar5332 4 ปีที่แล้ว +4

      That's O(N) because each element is not processed N times. Each element is processed at most twice(pushing in and popping out), which results in linear time complexity.

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว +2

      Careful 2 loops doesn’t mean n^2 you have to think about what each of the loops are doing

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว

      Thanks for the explanation

  • @lalitborse
    @lalitborse 4 ปีที่แล้ว

    i would like to see the explanation for course schedule I and II from leetcode

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว +1

      lalit borse I’ll see what I can do

  • @mandeepbahal23
    @mandeepbahal23 4 ปีที่แล้ว

    Would you do basic calculator I, II and III from leetcode

  • @laxatives
    @laxatives 4 ปีที่แล้ว +5

    Congrats we've now deleted this interview from our question bank. But keep studying!

  • @goodwish1543
    @goodwish1543 4 ปีที่แล้ว

    Watched 5 minutes. ha-ha, your design is using Stack, it's much simpler then mine. Thanks for sharing.
    I'll do it again with Stack.
    class Solution:
    def asteroidCollision(self, A: List[int]) -> List[int]:
    # stack
    s = []
    for v in A:
    if not s:
    s.append(v)
    elif s[-1] < 0:
    s.append(v)
    elif s[-1] > 0:
    if v > 0:
    s.append(v)
    else:
    while s and s[-1] > 0 and s[-1] < abs(v):
    s.pop()
    if not s or s[-1] < 0:
    s.append(v)
    elif s[-1] == abs(v):
    s.pop()
    return s

  • @srinish1993
    @srinish1993 4 ปีที่แล้ว

    The last elseif case of in can be merged

  • @switch2650
    @switch2650 3 ปีที่แล้ว

    Good content

  • @chinmayswaroopsaini7890
    @chinmayswaroopsaini7890 4 ปีที่แล้ว +1

    I almost solved the problem my code was a mess. Yours is way too clean.

  • @HenggaoCai
    @HenggaoCai 4 ปีที่แล้ว

    Asteroid*

  • @pratikchowdhury9210
    @pratikchowdhury9210 4 ปีที่แล้ว

    we dont have lyft here in India XD

  • @rasmith87
    @rasmith87 2 ปีที่แล้ว

    Hitting submit without run? Psychopath.