Arranging Coins - Leetcode 441 - Python

  Рет қаралды 33,531

NeetCode

NeetCode

Күн бұрын

🚀 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 -...
💡 CODING SOLUTIONS: • Coding Interview Solut...
💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
🌲 TREE PLAYLIST: • Invert Binary Tree - D...
💡 GRAPH PLAYLIST: • Course Schedule - Grap...
💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
💡 BINARY SEARCH PLAYLIST: • Binary Search
📚 STACK PLAYLIST: • Stack Problems
Problem Link: leetcode.com/problems/arrangi...
0:00 - Read the problem
1:10 - Drawing Explanation
9:58 - Coding Explanation
leetcode 441
This question was identified as a facebook interview question from here: github.com/xizhengszhang/Leet...
#facebook #interview #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.

Пікірлер: 65
@weaponkid1121
@weaponkid1121 2 жыл бұрын
I think a lot of people would love if you made a video on your process for solving a problem you haven't seen before. How long do you spend on a problem, what do you do when you get stuck, how much do you create your own solution vs read other solutions and digest them, etc. In one of your videos you said you're not some leetcode god, but you definitely come across that way, and I think seeing your process from start to finish would be really motivating!
@mearaftadewos8508
@mearaftadewos8508 2 жыл бұрын
That would be nice. We can learn how to learn if we find a different way than ours.
@_7__716
@_7__716 2 жыл бұрын
Def
@Terracraft321
@Terracraft321 2 жыл бұрын
Clement
@YT.Nikolay
@YT.Nikolay Жыл бұрын
Neet, we need such series, time to make an account on twitch
@shrimpo6416
@shrimpo6416 2 жыл бұрын
Just wanna thank you man... Just got my offer all bc of YOU!
@user-bz9ve2ig5e
@user-bz9ve2ig5e 8 ай бұрын
i did it in O(1) by using the formula i(i+1)//2= n of n natural numbers, then find out the one positive(real) root (by the fomula (-b+(underroot(-b+4ac))//2a) and return int(root)
@LiveType
@LiveType 2 жыл бұрын
I feel smugly accomplished that my many years of math classes allowed me to almost instantly do (-1+ sqrt(1+8*n)))/2 rounded down solve this problem.
@reisturm9296
@reisturm9296 2 жыл бұрын
Hey NeetCode, just wanted to say thank you! I received an offer to Google and couldn't be more excited. I watched your videos as a way to passively ingest some knowledge and it helped tremendously since the last time I was job hunting out of school. Cheers :)
@mearaftadewos8508
@mearaftadewos8508 2 жыл бұрын
Hey Rei, Congrats my bro!! I have interviews at google and amazon paralley. Can you please recommend me some focus areas for google and pretty much everything you noticed and how you have prepared and for which level? Thanks in advance!
@reisturm9296
@reisturm9296 2 жыл бұрын
@@mearaftadewos8508 thanks! I wouldn't say there is a specific topic you should focus on. you can focus on your weak areas if you think you are not particularly good at a topic like DP etc. The stronger your fundamentals, the more ideas you can suggest to the interviewer and you can work with them to make your way to the solution. As your fundamentals grow, you will begin to recognize patterns and what DS/algo you can apply to the problem while working out minor details/edge cases. Good luck! Don't be down about leetcoding. It's easy to feel dumb when you cannot solve a problem. Spend some time on the problem, if you can't solve, then read the solution and try to understand it so you know why they approached it this way and take it forward.
@mearaftadewos8508
@mearaftadewos8508 2 жыл бұрын
@@reisturm9296 Awesome. Thank you!
@oatmeal979
@oatmeal979 4 ай бұрын
@@mearaftadewos8508 bit late but how'd the interviews go?
@mearaftadewos8508
@mearaftadewos8508 2 жыл бұрын
Thank you dear NeetCode. My fav channel.
@vgsuresh
@vgsuresh Жыл бұрын
well you described very easily the formula for sum of first n natural numbers much more intuitively than my maths teacher... thanks much for all the work you put in these videos, helps a lot.
@akhma102
@akhma102 Жыл бұрын
Simply the Best! I have no words!
@mohamedhesham6008
@mohamedhesham6008 2 жыл бұрын
Very clear and great explanation thank you. you have the gift of teaching
@cc-to2jn
@cc-to2jn 2 жыл бұрын
Man i cant appriciate you enough!
@augustshen8018
@augustshen8018 2 жыл бұрын
in Line 13, res = mid will be enough, cuz the next possible mid will always be larger than res.
@durgaprasadreddypeddireddy8740
@durgaprasadreddypeddireddy8740 Жыл бұрын
yes, that is what I was thinking too.
@ayoubalem865
@ayoubalem865 2 жыл бұрын
Hi neetcode here is a simple solution: return int(((0.25 + 2 * n) ** 0.5) - 1/2) Actually 1 + 2 + 3 + 4 ... n = n (n + 1) / 2 is a serie where n is equal to the last term of our serie and also represents the number of terms so all we need is just solve the equation n = i*(i + 1)/2
@frankribery3362
@frankribery3362 2 жыл бұрын
My thoughts exactly
@duanqichuzudefangwu
@duanqichuzudefangwu 7 ай бұрын
was going to say: there is a direct analytical solution
@baap6886
@baap6886 2 жыл бұрын
you can solve it like this too: s=0 for i in range(1,n+1): s+=i if s>n: return i-1 return i
@mearaftadewos8508
@mearaftadewos8508 2 жыл бұрын
The approach that came to my mind was this. Please test it and let me know if it has bugs for certain test inputs def buildstairs(tiles): row = 0 i = 1 while i < tiles: row += 1 i += row print(i, row) return row if abs(tiles - i) == 0 else row - 1 print('stairs: ',buildstairs(9)) # from math import sqrt, floor # return floor(-.5 + sqrt(1+2*tiles)) # works too
@abhinowyt8564
@abhinowyt8564 2 жыл бұрын
Hey Neetcode, how much time do you spend coding practice in a day? Before job and now
@ilihaspatel399
@ilihaspatel399 Жыл бұрын
the formula you are talking about is sum of n natural numbers which is taught in class 5 but still I appreciate the way you use the formula in binary search ... Thanks for the Solution
@sankhadip_roy
@sankhadip_roy 10 ай бұрын
when you said u consider it as a medium level problem , I got relieved.
@pabloarkadiuszkalemba7415
@pabloarkadiuszkalemba7415 Жыл бұрын
I think you don't need to use Gauss formula to solve this. You use a semi-square, so te elements until that value will be row*col/2 and because its not a Perfect semi-square you need to add the stair (row/2). So the formula to get the values is (row*2+col)/2. And with that you can binarny search
@Chirayu19
@Chirayu19 10 ай бұрын
Hey @NeetCode, I think the time complexity of the brute force soution should be sqrt(N) instead of O(n) since the loop is starting at 1 and will not go upto N. It will go upto Please do correct me if I am wrong. Thanks!
@yfjsdgzjdnfn
@yfjsdgzjdnfn 9 ай бұрын
Same thoughts
@sushantrocks
@sushantrocks 2 жыл бұрын
Return the positive root of the quadratic eqn: k(k+1)/2 = n return int(math.sqrt(1+8*n)-1)//2
@arjunreddy2647
@arjunreddy2647 Жыл бұрын
W
@ChandraShekhar-by3cd
@ChandraShekhar-by3cd 2 жыл бұрын
Thanks a lot for such a great and clear explanation! How is things going at Google?
@indonline
@indonline 2 жыл бұрын
O(1) algo comes to mind immediately including the rounding part ....
@rupambasu9722
@rupambasu9722 2 жыл бұрын
Hi, I really like ur problem solving approach. Can you please solve account merge problem
@Y1Rayn
@Y1Rayn Жыл бұрын
my issue with binary search problems is that for the while loop I never know whether to use l < r or l
@practicefirsttheorylater
@practicefirsttheorylater 11 ай бұрын
hi, i am not that good but this is how i think about it. take just n = 1, so our left = right = 1 when we start. so if we used condition l < r, we will never check the array. thus, we must use l
@harshavardhanveerannanaval8605
@harshavardhanveerannanaval8605 2 жыл бұрын
A cleaner easy to understand solution, def arrangeCoins(self, n): """ :type n: int :rtype: int """ def getCoins(stairs): return(stairs*(stairs+1)/2) l=0 r=n ans=None while(l
@He_ze
@He_ze 6 ай бұрын
I originally tried doing this with prefix sum + binary search. I got memory limit exceeded, but it looked like i was kinda on the right approach
@maamounhajnajeeb209
@maamounhajnajeeb209 Жыл бұрын
There is no need to use max(). I try this solution and it works on leet code with over 1300 test case. the solution: class Solution: def arrangeCoins(self, n : int): start, end = 0, n while start n: end = mid-1 elif guess
@oreoshake6287
@oreoshake6287 Жыл бұрын
Isn't the Tim complexity of the 1st method so log(n)?as we are exiting from the loop when the number of coins becomes negative
@poonamgulwane6734
@poonamgulwane6734 Жыл бұрын
This can also work : def arrangeCoins(self, n): """ :type n: int :rtype: int """ row = 0 i = 0 while n > 0: if n >= (i + 1): row += 1 n = n - (i + 1) i += 1 return row
@call_me_lazarus
@call_me_lazarus Жыл бұрын
Is there a typo somewhere in this solution? I've been breaking it down, and comparing it with what I have typed up, but I keep getting a few failed test cases.
@MrArpit1234
@MrArpit1234 Ай бұрын
n(n+1)/2 is summ of all natural numbers, which is nothing but AP, basic class 7 mathematics
@darkmatter2958
@darkmatter2958 2 жыл бұрын
i saw there was an o(1) solution it was return (sqrt(8*n+1)-1)/2
@darkmatter2958
@darkmatter2958 2 жыл бұрын
can you explain it how it was derived
@pojunechen2617
@pojunechen2617 11 ай бұрын
Just wanted to let you know your code doesn't pass the test cases. Line 8 should be changed to mid*(mid+1)/2
@rahulprasad2318
@rahulprasad2318 2 жыл бұрын
U can calculate it in O(1) N = floor(-.5 + sqft(1+2n))
@JJKK-nj1vb
@JJKK-nj1vb 2 жыл бұрын
sqrt is logn
@rahulprasad2318
@rahulprasad2318 2 жыл бұрын
@@JJKK-nj1vb still it's more readable and maintainable.
@rppt
@rppt 2 жыл бұрын
@@JJKK-nj1vb On the ARM Cortex M4, the assembly instruction VSQRT takes exactly 14 cycles for any 32 bit floating point. SQRT() being O(log-n) is only if you're assuming potentially arbitrarily large integers or arbitrarily precise floating points.
@santysayantan
@santysayantan 2 жыл бұрын
I don't understand why you didn't implement the math solution? Breaking that quadratic equation would make it R = -1 + sqrt(1 + 4.2.n))/2 - > quadratic equation. Basically if we can write return (1 + int((1 + 8*n)**(1/2))/2) That would directly solve the problem. I think it says easy because it is school grade mathematics.
@mearaftadewos8508
@mearaftadewos8508 2 жыл бұрын
I thougth of that tho.
@oreoshake6287
@oreoshake6287 Жыл бұрын
Can you please explain how you derived the formula
@aabhishek4911
@aabhishek4911 2 жыл бұрын
Who has gone through school without learning gauss formula ? It is one of the very basic ones
@masternobody1896
@masternobody1896 2 жыл бұрын
nice
@vadimkokielov2173
@vadimkokielov2173 Жыл бұрын
I can't believe they marked the "math" solution O(1)?! Has rigor gone completely out of algorithm analysis?!!! The algorithm for computing square roots is Newton's Method -- which amounts to the same bisection in the case of square roots!! And it has the same logarithmic runtime??
@NeetCode
@NeetCode Жыл бұрын
Yeah, I was wondering the same thing. It seems any "math" solution is marked O(1) but it's technically not the case.
@pradiptasarma4867
@pradiptasarma4867 2 жыл бұрын
Arithmatic Progression.
@prabhatracherla3098
@prabhatracherla3098 Жыл бұрын
How was this easy question then? I mean code is easy, but the thought process was not
@thangnguyenduc9969
@thangnguyenduc9969 Ай бұрын
just return right instead
@flatmapper
@flatmapper 4 ай бұрын
hard
@HarvinderSandhuEsq
@HarvinderSandhuEsq 2 жыл бұрын
Math formula is easier
Delete and Earn - Dynamic Programming - Leetcode 740 - Python
19:09
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 311 М.
КАРМАНЧИК 2 СЕЗОН 7 СЕРИЯ ФИНАЛ
21:37
Inter Production
Рет қаралды 520 М.
3M❤️ #thankyou #shorts
00:16
ウエスP -Mr Uekusa- Wes-P
Рет қаралды 13 МЛН
HOW DID HE WIN? 😱
00:33
Topper Guild
Рет қаралды 37 МЛН
The child was abused by the clown#Short #Officer Rabbit #angel
00:55
兔子警官
Рет қаралды 24 МЛН
Insert Delete GetRandom O(1) - Leetcode 380 - Python
13:27
NeetCode
Рет қаралды 41 М.
Arranging Coins (LeetCode) | Binary Search Algorithm Explanation
10:25
Jyotinder Singh
Рет қаралды 1,6 М.
Move Zeroes - Leetcode 283 - Python
8:15
NeetCode
Рет қаралды 67 М.
Why Good Programmers FAIL Coding Interviews
8:15
Sahil & Sarra
Рет қаралды 338 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 273 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 614 М.
Binary Search - Leetcode 704 - Python
9:40
NeetCode
Рет қаралды 135 М.
Successful Pairs of Spells and Potions - Leetcode 2300 - Python
14:28
❌ Don't Run Behind 500 LEETCODE Problems ❌ Focus on QPCD
8:31
Largest Number - Leetcode 179 - Python
10:17
NeetCode
Рет қаралды 52 М.
ОБСЛУЖИЛИ САМЫЙ ГРЯЗНЫЙ ПК
1:00
VA-PC
Рет қаралды 1,3 МЛН
Самый дорогой кабель Apple
0:37
Romancev768
Рет қаралды 302 М.
تجربة أغرب توصيلة شحن ضد القطع تماما
0:56
صدام العزي
Рет қаралды 2,1 МЛН
Опыт использования Мини ПК от TECNO
1:00
Андронет
Рет қаралды 686 М.
Хотела заскамить на Айфон!😱📱(@gertieinar)
0:21
Взрывная История
Рет қаралды 5 МЛН