Find the Winner of the Circular Game - Leetcode 1823 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ธ.ค. 2024

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

  • @manishmalepati9675
    @manishmalepati9675 5 หลายเดือนก่อน +19

    I could do the queue, but couldn't think of the recursive solution.
    I was looking at soo many TH-cam videos trying to explain this Josephus problem, but none of them could show the intuition.
    You are the only one who showed how the recursive logic worked, and how we can take a step from queue logic to the recursive one.
    Thank you so much!!

  • @JR_GameDev
    @JR_GameDev 21 วันที่ผ่านมา +1

    thank you my bro!

  • @fancypants6062
    @fancypants6062 5 หลายเดือนก่อน +2

    I had no clue why the modulo solutions worked until watching this video, now it makes perfect sense. Thank you.

  • @Billigently
    @Billigently 5 หลายเดือนก่อน +2

    Sometimes it’s hard to wrap my head around recursive solutions but you explained it so perfect and clear! Thank you!

  • @fraserdab
    @fraserdab 5 หลายเดือนก่อน +4

    u explained the recursive approach fast and properly, just what i needed

  • @gnaneswarilolugu2323
    @gnaneswarilolugu2323 3 หลายเดือนก่อน

    Greatest explanation ever. Not everyone will have such skills. I really appreciate you choosing this as a career. Thank you for changing lives!

  • @xx1slimeball
    @xx1slimeball 5 หลายเดือนก่อน +1

    I think u did a great job explaining it!
    The way that help me understand the subproblems was by visualizing like this
    "
    n = 5
    k = 2
    f(n) = [1, 2, 3, 4, 5]
    f(n - 1) = [3, 4, 5, 1]
    f(n - 2) = [5, 1, 3]
    f(n - 3) = [3, 5]
    f(n - 4) = [3]
    "
    its similar to how you did except, instead of appending the nums, each function call can be viewed as its own list, where each i = 0 is the prev k + 1, thus the relation f(n) = (f(n - 1) + k) % n, can be seen (where f(n) is the index of the correct person in the current list). But jeez, this problem was pretty crazy.
    It's also cool that we never care about which person that have to leave, bc we know that the basecase person will be safe and we only recover its original index,
    Great video!!

  • @gary1614
    @gary1614 5 หลายเดือนก่อน +1

    So much clearer now! You did a great job explaining this question. Love your videos!

  • @pk2468-z2w
    @pk2468-z2w 5 หลายเดือนก่อน +18

    Would you ask this in an interview? Till what level of optimization would you expect the candidate to solve?

    • @NeetCodeIO
      @NeetCodeIO  5 หลายเดือนก่อน +25

      definitely not, if you do it's unlucky, like 0.1% unlucky. the queue solution is approachable with hints but not the recursive one imo

    • @nikhil199029
      @nikhil199029 5 หลายเดือนก่อน +1

      Fml. I got determine the winner of Babylon problem if all players play optimally in an interview.

    • @patelvrukshal5102
      @patelvrukshal5102 5 หลายเดือนก่อน +4

      I was asked this in Splunk OA and I solved it with queue logic. Didn't hear back from them so some people might have solved with the recursive intuition I bet.

    • @rishabh7215
      @rishabh7215 4 หลายเดือนก่อน +2

      @@patelvrukshal5102 that sucks. don't think its possible to come up with that approach in an interview unless you have seen it before

  • @sri_harsha_dv
    @sri_harsha_dv 5 หลายเดือนก่อน +2

    I had to watch the 2nd approach twice to understand. It's just the shifting of indices is new to me and I was concentrating solely on that rather than overall solution.
    Sidenote: This might be obvious to some or completely irrelevant to others. I was wondering what might be the usecase for this crazy solution. There might be a better one (let me know if there is), but to my surprise I found it in "hide and seek" game. There are many ways to determine who is the seeker. One of those is by counting using set of phrases (this will differ most likely, but we used "inky pinky ponky" if that rings a bell to few of you). So there is a solution for that mini-game. After finding out the usecase, I checked the title of the problem and thought this might be reference for the problem.

  • @MP-ny3ep
    @MP-ny3ep 5 หลายเดือนก่อน +1

    Thank you so much ! All 3 explanations were great

  • @YashGupta-ty2hn
    @YashGupta-ty2hn 5 หลายเดือนก่อน +2

    A Very simple code in python
    class Solution:
    def findTheWinner(self, n: int, k: int) -> int:
    queue = deque(list(range(1, n + 1)))
    while len(queue) > 1:
    queue.rotate(-k)
    queue.pop()
    return queue[0]

  • @Asdelatech
    @Asdelatech 3 หลายเดือนก่อน +1

    Thank you so much

  • @julianelmasry9556
    @julianelmasry9556 5 หลายเดือนก่อน +3

    was not able to have the queue neuron fire today. I was having trouble trying to represent the problem

  • @shubhamkhatter2196
    @shubhamkhatter2196 5 หลายเดือนก่อน

    NGL, i code my solutions in C++ just bcoz i started that way, even though i am fluent with python as well.
    but when i first saw deque approach i was kind disheartened to see that, coz in the solutions board, everyone was using that recursive approach, and i thought that you were using different approach, so i started for looking at other videos, and they were really not doing a good job at explaining it, and when i thought that it doesnt matter if your solution is not the one i was looking for, but atleast i am able to understand its intuition well, you came up with the recursive approach later in the video, so what i am trying to say is, you explained it well.
    TLDR: you explained it well and next time just tell at the start of video that you have more than one solution, ik you always do that, but i am not used to it T_T.
    thanks btw.

  • @TWEEDOriginal
    @TWEEDOriginal 5 หลายเดือนก่อน +1

    Linked list solution JS
    var findTheWinner = function (n, k) {
    function ListNode(val, next) {
    this.val = (val === undefined ? 0 : val)
    this.next = (next === undefined ? null : next)
    }
    let head = new ListNode(1)
    let prev = head
    for (let i = 2; i

  • @greatfate
    @greatfate 5 หลายเดือนก่อน

    you did a good job explaining last approach broski

  • @corrogist692
    @corrogist692 5 หลายเดือนก่อน

    for the iterative one you can start from 2 since everything %1 is 0 (of course, doesn't really matter the time consumed is insignificant)

  • @opuchakraborty
    @opuchakraborty 5 หลายเดือนก่อน

    You did good while explaining the recursive and bottom up approach, but it's still difficult to wrap my head into the solution. I could do the Queue though.

  • @zetianjin5414
    @zetianjin5414 5 หลายเดือนก่อน

    very good explanation

  • @shub2726
    @shub2726 5 หลายเดือนก่อน +3

    26 seconds ago is amazing

  • @hhh555jjj
    @hhh555jjj 5 หลายเดือนก่อน

    Thank you so much! This one is greatly helpful!
    I have been thinking "why gpt said adding k is for adjustment??" for 2 days and got nothing done😂

  • @dhaanaanjaay
    @dhaanaanjaay 5 หลายเดือนก่อน

    simply Genius

  • @tirasjeffrey2002
    @tirasjeffrey2002 5 หลายเดือนก่อน

    coming with A solution is good, but blud has 2 3 solutions up his sleeve, straight up monster

  • @Munchen888
    @Munchen888 5 หลายเดือนก่อน

    Am I understand right that in recursion we create an arr= list(range(1, n + 1)) and in function we pop k - 1 and solve subproblem (arr).
    When the length of arr is 1 return arr[0]?

  • @B-NikhilRichhariya
    @B-NikhilRichhariya 5 หลายเดือนก่อน

    It was great !

  • @venkateshnaidu2133
    @venkateshnaidu2133 5 หลายเดือนก่อน

    I did this iteratively (using array) and then optimized it using queue. (Looking at the constraints, I knew these would pass)
    Could not at all come up with the recursive approach.

  • @saruhasan9145
    @saruhasan9145 5 หลายเดือนก่อน +1

    My code i took me 2 hours to come up with
    class Solution:
    def findTheWinner(self, n: int, k: int) -> int:
    players = [i for i in range(1, n + 1)]
    length = len(players)
    i = 0
    while length != 1:
    i = (i + k - 1) % length
    players.pop(i)
    length -= 1
    return players[0]

  • @reggiehurley1479
    @reggiehurley1479 5 หลายเดือนก่อน

    i think the iterative version is a bit more inuitive than the recursive one

  • @yogeshchauhan9401
    @yogeshchauhan9401 5 หลายเดือนก่อน +1

    Simulation is easy to come up with but recursive is 🔥🔥

  • @DeathSugar
    @DeathSugar 5 หลายเดือนก่อน

    Tried to code up wikipedia's general formula and it looks like invalid

  • @pastori2672
    @pastori2672 5 หลายเดือนก่อน +10

    einstein wouldnt waste a millisecond of his life solving leetcode sadly 😭

    • @NeetCodeIO
      @NeetCodeIO  5 หลายเดือนก่อน +10

      you never know, he loved day dreaming and solving hard problems by running thought experiments in his head

  • @GunnerJayx
    @GunnerJayx 5 หลายเดือนก่อน

    Why not just use a linked list?

  • @bhavyajainnd
    @bhavyajainnd 5 หลายเดือนก่อน

    Even the hints just tell you to simulate maaaan 😭 what is this question?

  • @brianwkinyua
    @brianwkinyua 2 หลายเดือนก่อน

    ur explanation is great.
    regarding other resources, I found this one to be helpful.
    th-cam.com/video/8uFWG6xfkuc/w-d-xo.html
    not that it is better than yours but a helpful compliment to understanding the whole concept better.
    I couldn't understand his until I watched yours, and I couldn't understand yours without watching his.
    I really appreciate your work.
    All the best.

  • @pat777b
    @pat777b 5 หลายเดือนก่อน

    I suspect there is a clever O(1) time and O(1) space solution. I haven't figured out such a solution yet but my gut tells me there might be one.

    • @ahlamabugosh9596
      @ahlamabugosh9596 5 หลายเดือนก่อน

      there is
      th-cam.com/video/uCsD3ZGzMgE/w-d-xo.html

    • @saruhasan9145
      @saruhasan9145 5 หลายเดือนก่อน +1

      There is

    • @fabricio5p
      @fabricio5p 5 หลายเดือนก่อน +1

      there is for fixed size values of k: en.wikipedia.org/wiki/Josephus_problem

    • @EduarteBDO
      @EduarteBDO 5 หลายเดือนก่อน +1

      just for k = 2 and k = 3, people discovered.

  • @SanoyBoby
    @SanoyBoby 5 หลายเดือนก่อน

    class Solution:
    def findTheWinner(self, n: int, k: int) -> int:
    s=0
    total=n*(n+1)//2
    l=list(range(1,n+1))
    i=0
    while len(l)!=1:
    if i+k-1>=n:
    i=((i+k-1)%n)
    else:
    i+=k-1
    print(l,i)
    s+=l.pop(i)
    n-=1
    return total-s
    no queue,simple list

    • @irfansari_
      @irfansari_ 5 หลายเดือนก่อน

      Still space: O(n)