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

  Рет қаралды 10,880

NeetCodeIO

NeetCodeIO

Күн бұрын

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
🐦 Twitter: / neetcode1
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
Problem Link: leetcode.com/problems/find-th...
0:00 - Read the problem
1:00 - Queue Explanation
6:33 - Coding Queue
8:08 - Recursive Explanation
14:10 - Coding Recursive
15:38 - Coding Iterative
leetcode 1823
#neetcode #leetcode #python

Пікірлер: 47
@manishmalepati9675
@manishmalepati9675 9 күн бұрын
I could do the queue, but couldn't think of the recursive solution. I was looking at soo many KZbin 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!!
@Billigently
@Billigently 9 күн бұрын
Sometimes it’s hard to wrap my head around recursive solutions but you explained it so perfect and clear! Thank you!
@parthkansara552
@parthkansara552 9 күн бұрын
Would you ask this in an interview? Till what level of optimization would you expect the candidate to solve?
@NeetCodeIO
@NeetCodeIO 9 күн бұрын
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 9 күн бұрын
Fml. I got determine the winner of Babylon problem if all players play optimally in an interview.
@patelvrukshal5102
@patelvrukshal5102 9 күн бұрын
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.
@fraserdab
@fraserdab 9 күн бұрын
u explained the recursive approach fast and properly, just what i needed
@fancypants6062
@fancypants6062 7 күн бұрын
I had no clue why the modulo solutions worked until watching this video, now it makes perfect sense. Thank you.
@xx1slimeball
@xx1slimeball 9 күн бұрын
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 7 күн бұрын
So much clearer now! You did a great job explaining this question. Love your videos!
@sri_harsha_dv
@sri_harsha_dv 9 күн бұрын
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 9 күн бұрын
Thank you so much ! All 3 explanations were great
@julianelmasry9556
@julianelmasry9556 9 күн бұрын
was not able to have the queue neuron fire today. I was having trouble trying to represent the problem
@greatfate
@greatfate 9 күн бұрын
you did a good job explaining last approach broski
@YashGupta-ty2hn
@YashGupta-ty2hn 9 күн бұрын
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]
@opuchakraborty
@opuchakraborty 8 күн бұрын
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 9 күн бұрын
very good explanation
@Munchen888
@Munchen888 8 күн бұрын
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]?
@dhaanaanjaay
@dhaanaanjaay 9 күн бұрын
simply Genius
@tirasjeffrey2002
@tirasjeffrey2002 9 күн бұрын
coming with A solution is good, but blud has 2 3 solutions up his sleeve, straight up monster
@shubhamkhatter2196
@shubhamkhatter2196 9 күн бұрын
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.
@B-NikhilRichhariya
@B-NikhilRichhariya 9 күн бұрын
It was great !
@shub2726
@shub2726 9 күн бұрын
26 seconds ago is amazing
@corrogist692
@corrogist692 9 күн бұрын
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)
@hhh555jjj
@hhh555jjj 8 күн бұрын
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😂
@venkateshnaidu2133
@venkateshnaidu2133 9 күн бұрын
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.
@DeathSugar
@DeathSugar 9 күн бұрын
Tried to code up wikipedia's general formula and it looks like invalid
@reggiehurley1479
@reggiehurley1479 9 күн бұрын
i think the iterative version is a bit more inuitive than the recursive one
@TWEEDOriginal
@TWEEDOriginal 9 күн бұрын
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
@GunnerJayx
@GunnerJayx 8 күн бұрын
Why not just use a linked list?
@yogeshchauhan9401
@yogeshchauhan9401 9 күн бұрын
Simulation is easy to come up with but recursive is 🔥🔥
@pastori2672
@pastori2672 9 күн бұрын
einstein wouldnt waste a millisecond of his life solving leetcode sadly 😭
@NeetCodeIO
@NeetCodeIO 9 күн бұрын
you never know, he loved day dreaming and solving hard problems by running thought experiments in his head
@bhavyajainnd
@bhavyajainnd 9 күн бұрын
Even the hints just tell you to simulate maaaan 😭 what is this question?
@pat777b
@pat777b 9 күн бұрын
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 9 күн бұрын
there is kzbin.info/www/bejne/q3TWdWaQfN-Anac
@saruhasan9145
@saruhasan9145 9 күн бұрын
There is
@fabricio5p
@fabricio5p 9 күн бұрын
there is for fixed size values of k: en.wikipedia.org/wiki/Josephus_problem
@EduarteBDO
@EduarteBDO 9 күн бұрын
just for k = 2 and k = 3, people discovered.
@SanoyBoby
@SanoyBoby 9 күн бұрын
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_ 9 күн бұрын
Still space: O(n)
@saruhasan9145
@saruhasan9145 9 күн бұрын
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]
Distribute Coins in Binary Tree - Leetcode 979 - Python
17:41
NeetCodeIO
Рет қаралды 15 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 338 М.
마시멜로우로 체감되는 요즘 물가
00:20
진영민yeongmin
Рет қаралды 29 МЛН
КАК ДУМАЕТЕ КТО ВЫЙГРАЕТ😂
00:29
МЯТНАЯ ФАНТА
Рет қаралды 6 МЛН
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
Merge Nodes in Between Zeros - Leetcode 2181 - Python
11:24
NeetCodeIO
Рет қаралды 6 М.
10 Math Concepts for Programmers
9:32
Fireship
Рет қаралды 1,8 МЛН
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 574 М.
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 429 М.
Samsung Galaxy 🔥 #shorts  #trending #youtubeshorts  #shortvideo ujjawal4u
0:10
Ujjawal4u. 120k Views . 4 hours ago
Рет қаралды 7 МЛН
iPhone 15 Pro в реальной жизни
24:07
HUDAKOV
Рет қаралды 351 М.
Cheapest gaming phone? 🤭 #miniphone #smartphone #iphone #fy
0:19
Pockify™
Рет қаралды 2,9 МЛН