Remove Colored Pieces if Both Neighbors are the Same Color - Leetcode 2038 - Python

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 27. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    đŸ„· Discord: / discord
    🐩 Twitter: / neetcode1
    🐼 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: ‱ Two Sum - Leetcode 1 -...
    💡 DYNAMIC PROGRAMMING PLAYLIST: ‱ House Robber - Leetco...
    Problem Link: leetcode.com/problems/remove-...
    0:00 - Read the problem
    0:30 - Drawing Explanation
    5:35 - Coding Solution 1
    8:50 - Coding Solution 2
    leetcode 2038
    #neetcode #leetcode #python

Komentáƙe • 27

  • @juanmacias5922
    @juanmacias5922 Pƙed 10 měsĂ­ci +10

    Man, I need to do more of these, and get better at seeing the pattern, once you spell it out, it's so simple lol

  • @naibgulam2336
    @naibgulam2336 Pƙed 10 měsĂ­ci +14

    I'm a non CS student with a degree in mechanical engineering. I'm trying to make a career shift towards tech and your videos are helping me so much in helping me to learn DS and algo and keeping up with daily leetcode practice in preparation for when I'm ready to start interviewing

    • @adityaparab4314
      @adityaparab4314 Pƙed 10 měsĂ­ci +2

      Good luck with your preparation! You will achieve whatever you are aiming for.

    • @chianlee1381
      @chianlee1381 Pƙed 10 měsĂ­ci

      Hey man I am also studying ME and doing the same thing like you here! The tutorial is really helpful. Cheer up !

  • @UpTwist
    @UpTwist Pƙed 10 měsĂ­ci +8

    I failed a very similar question in an OA and failed it a few days ago. I had an intuition that "a player's move can unlock a move by another player" because of other game theory leetcode questions I have done in the past. I think the word optimally has scarred me into overthinking this type of problem. After understanding the problem in the OA after, I did this daily very quickly.
    I quote a comment in the discussions: "It's actually very simple, if you think about this fact: "There is no move a player can make that will unlock a move by another player". Because unlocking a move by another player would mean for example removing a B in between two As which is impossible. Therefore the number of moves of each player is determined by the initial number of moves they can make."
    This comment is the key take away from this question IMO. Thanks for the daily.

    • @picnicbros
      @picnicbros Pƙed 10 měsĂ­ci +1

      JPMorgan?

    • @UpTwist
      @UpTwist Pƙed 10 měsĂ­ci

      no, i think it was captial one. i still cant forgive myself for letting such an easy problem go. this pattern will never fool me again tho.@@picnicbros

    • @MP-ny3ep
      @MP-ny3ep Pƙed 10 měsĂ­ci

      This is what I thought at first too. Thanks for the explanation , it cleared things out!

  • @MP-ny3ep
    @MP-ny3ep Pƙed 10 měsĂ­ci +1

    Great explanation as always. Love the optimization in the end!

  • @CodingJoySoul
    @CodingJoySoul Pƙed 9 měsĂ­ci

    Glad to know that you are back at making Leetcode solution videos

  • @curesnow6493
    @curesnow6493 Pƙed 9 měsĂ­ci

    Thank you for your beautiful solution Neetcode. I did overthink this problem.

  • @priyanshugupta-zl1st
    @priyanshugupta-zl1st Pƙed 10 měsĂ­ci

    Thanks! Your channel is such a blessing.

  • @proofhundred986
    @proofhundred986 Pƙed 10 měsĂ­ci +1

    Do More Hard Problems, like hard mediums and Hards
    Also Maybe even Consider Vids About the Contests Problems.

  • @yoursandeep
    @yoursandeep Pƙed 10 měsĂ­ci

    Hi neetcode thanks for sharing such beautiful lessons. Need some help. Can you please let me know what code execution engine/framework you used for neetcode site to build the code editor. Thanks again.

  • @SASA_maxillo
    @SASA_maxillo Pƙed 9 měsĂ­ci +1

    leetcode be like:
    first submission: 10000000ms
    second submission: 10ms
    Yes this makes complete sense 😂😂

  • @vs3.14
    @vs3.14 Pƙed 9 měsĂ­ci

    another simplification : omit the second if conditional. just put else (really sorry for that lmao). These type of thinking based problems are my favourite. Not much code to write. As long as I can think properly. Needless to say, I suck at them.

  • @PIRAISUDANB
    @PIRAISUDANB Pƙed 9 měsĂ­ci

    Sir, can i use this code? is it is correct? i used stack method that was thought by u in previous video
    class Solution:
    def winnerOfGame(self, colors: str) -> bool:
    stack=[]
    d={'A':0,'B':0}
    for i in range(len(colors)):
    while len(stack)>=2 and stack[-1]==colors[i] and stack[-2]==colors[i]:
    d[colors[i]]+=1
    stack.pop()
    else:
    stack.append(colors[i])
    return d['A']>d['B']
    Thank u...

  • @Sivadurga726
    @Sivadurga726 Pƙed 10 měsĂ­ci

    I solved using second approach but sliding window looks better

  • @dumbfailurekms
    @dumbfailurekms Pƙed 10 měsĂ­ci +2

    u the best

  • @krateskim4169
    @krateskim4169 Pƙed 10 měsĂ­ci

    Awesome

  • @TheElementFive
    @TheElementFive Pƙed 9 měsĂ­ci

    Leetcode simulating redlining algorithms now 💀

  • @sachinpaul2111
    @sachinpaul2111 Pƙed 9 měsĂ­ci

    My dumbass thought that this was similar to the stone-game II that we had solved a while back in Problem of the Day and was doing some MinMax optimization

  • @brahm_and_coding
    @brahm_and_coding Pƙed 5 měsĂ­ci

    sliding window

  • @phpostrich
    @phpostrich Pƙed 10 měsĂ­ci +1

    I got mine down to 62 ms, and beat 100% python users

    • @phpostrich
      @phpostrich Pƙed 10 měsĂ­ci

      class Solution:
      def winnerOfGame(self, colors: str) -> bool:
      count = [colors.count('A'), colors.count('B')]
      players = [0, 0]
      for i in range(len(colors)-2):
      if (colors[i] == colors[i+1] == colors[i+2]):
      players[colors[i] == 'B'] += 1
      if abs(count[colors[i] == 'B'] - count[colors[i] != 'B']) > 270: # players[1]

  • @rijumondal6876
    @rijumondal6876 Pƙed 9 měsĂ­ci

    why the hell your narration sounds like a horror story