Тёмный

I Really Enjoyed This Google Interview Question :) | Unique Paths - Leetcode 62 

Greg Hogg
Подписаться 204 тыс.
Просмотров 49 тыс.
50% 1

Опубликовано:

 

28 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 51   
@GregHogg
@GregHogg 8 месяцев назад
Subscribe so that 3 + 3 == 9!
@ruddra4328
@ruddra4328 8 месяцев назад
It happens ❤ But goot effort I learn a lot from your questions ❤
@sirumbra366
@sirumbra366 8 месяцев назад
You already answered the follow up question: "proof that 3 + 3 === 9"
@user-jn4sw3iw4h
@user-jn4sw3iw4h 8 месяцев назад
Well there are a few options with moving the !
@Rugg-qk4pl
@Rugg-qk4pl 8 месяцев назад
3+3 most certainly doesn't equal 362,880...
@arsheyajain7055
@arsheyajain7055 8 месяцев назад
Hahhahaha
@MordechaiZbaps
@MordechaiZbaps 8 месяцев назад
I think you can just do some combinatorics and find the general formula is n among (n+m)
@Rozenkrantzz
@Rozenkrantzz 7 месяцев назад
It's the binomial coefficients
@ottik5
@ottik5 7 месяцев назад
Almost... (n-1) among (m+n-2)
@MordechaiZbaps
@MordechaiZbaps 7 месяцев назад
@@ottik5 yeah I got a little too excited here lol
@robertpaluba163
@robertpaluba163 7 месяцев назад
There are m+n-2 steps to make and choosing the positions of (m-1) steps down (or n-1 steps right) uniquely determines the path, so the answer is always (m+n-2) choose (m-1)
@popkornking
@popkornking 8 месяцев назад
The number of paths basically form a rotated Pascals triangle so theres an analytic solution once you transform the grid coordinates into the appropriate inputs for the binomial coefficients equation.
@ChickenMaster7
@ChickenMaster7 7 месяцев назад
Isn't the binomial coefficients equation: C(n,k)=k!(n−k)!n!​ We know n, the number of squares but what is k in this case?
@kobesinclair9982
@kobesinclair9982 8 месяцев назад
Trying to get into coding self taught currently in pre calculus. Thank you for the tips!
@GregHogg
@GregHogg 3 месяца назад
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@manish3217
@manish3217 7 месяцев назад
your code needs an additional elseif statement, "elseif j==0: ret[i][j] = ret[i-1][j]" correct me if wrong
@roelsfotoos
@roelsfotoos 8 месяцев назад
@maxencegitton377: The only correct answer👍 Any path from start to finish is a combination of (m-1) D's and (n-1) R's where D means a move down and R means a move right, e.g. DRDR. So, the answer is 6, not 9. Apart from that, this is a math problem rather than a coding problem.
@AsianCurls
@AsianCurls 8 месяцев назад
Um, I don't think @maxencegitto377 will see your comment since it's disjointed and could be lost in the other replies. You could use the reply function so the system sends him a notification of your reply.
@roelsfotoos
@roelsfotoos 8 месяцев назад
@@AsianCurls My replies to _most_ posts are regarded as a new post by YT when using my mobile phone, I _did_ reply to maxencegitton377. On my pc any post or reply is rejected. Anyone?
@goforward684
@goforward684 7 месяцев назад
"Because the robot is stupid..." The robot: 😢
@ajitsdeshpande
@ajitsdeshpande 7 месяцев назад
@GregHogg - From which condition of your code would it enter to mark ret[1][0] = 1 How did you write ret[1][0] as 1 in as shown in the video ? if it enters the else: part ret[1][0] = ret[1][-1] (left element)+ ret[0][0] (top element) so to calculate ret[1][0] code is referring to ret[1][-1] which is not yet available! This same thing happens for every ret[i][0] , basically every element in the 1st column for each row.
@ajitsdeshpande
@ajitsdeshpande 7 месяцев назад
It works because we have set the whole 2D array ret to zeroes at start, but i would think this case of out of bound should be avoided or addressed in a cleaner way.
@Decklun
@Decklun 7 месяцев назад
I also frequently see 3 and 3 and think 9 regardless of operation
@Temporalus
@Temporalus 8 месяцев назад
an important consideration to talk through is whether you can achieve each of the time complexities and WHY OR WHY NOT! you started with an O( N * M ) solution, can we find a solution that is linear in the side lengths or a closed form O(1)? no matter what the optimal solution you need to understand and articulate why further improvements are not possible. If you know it mentioning a proof is great.
@maxencegitton377
@maxencegitton377 8 месяцев назад
(n-1+m-1)!/((m-1)!*(n-1)!)
@howhello354
@howhello354 8 месяцев назад
What about he can make one step or two steps at a time? What's the formula now
@roelsfotoos
@roelsfotoos 8 месяцев назад
@@howhello354 Same answer since a double step is the same as 2 single steps ...
@howhello354
@howhello354 8 месяцев назад
@roelsfotoos i mean after each move, he can choose 1 or 2 steps to move forward. It's not likely moving with 1 step completely and moving with two steps completely.
@howhello354
@howhello354 7 месяцев назад
When i ran the test cases, I found the dynamic programming (tabulation) based approach faster than this factorial based formula. So, yes, programming approach matters even over matchmatical simple formula!
@maxencegitton377
@maxencegitton377 7 месяцев назад
@@howhello354 Another problem with the factorial solution is that it goes to really big numbers above 2^31 quickly and that can create bugs. Thank you.
@HR-pz7ts
@HR-pz7ts 7 месяцев назад
There are many clever ways you could solve this problem. You can use a 2D array to use dynamic programming. But the most optimal way is to make your way not from the start but from the end. I forgot the solution but I remember solving it.
@rikthecuber
@rikthecuber 7 месяцев назад
Let A indicate going down, and B indicate going to the right. m rows means m-1 A's needed. n cols means n-1 B's needed. Now we need the total number of distinct arrangements of such A's and B's. That is (m+n-2)!/((m-1)!*(n-1)!).
@_Loki__Odinson_
@_Loki__Odinson_ 8 месяцев назад
Finally a worthy opponent for 3^2 = 6
@GregHogg
@GregHogg 8 месяцев назад
Yeah I was definitely thinking squared lol
@hola-hola-2523
@hola-hola-2523 7 месяцев назад
Wait, I think you should handle the case j == 0 and i > 0. It will throw an error in your case, as it will be handled in the else clause
@soykiokin
@soykiokin 8 месяцев назад
It's similar to the BFS pathfinding algorithm!
@csec0565
@csec0565 7 месяцев назад
U can make this out using math also....
@carrot0013
@carrot0013 7 месяцев назад
Pascal's triangle let's goooo!
@akarshpahariya6155
@akarshpahariya6155 7 месяцев назад
Best free course just to learn dynamic programming in data structures and algorithms
@BattlewarPenguin
@BattlewarPenguin 7 месяцев назад
that's pretty awesome
@LifeofArmoney
@LifeofArmoney 8 месяцев назад
Oh boy, this one is math-ey
@extrams0
@extrams0 8 месяцев назад
The answer fails by matter of the constraints - namely the question notes the answer will be less then or equal to 2E9 , and is quite slow. a = max(m,n) b = min(m,n) answer = a * (a+1)/2 * .... * (a+b-2)/(b-1) this can be implemented with 13 bytes memory - int64 (8 bytes) for intermediate result - int32 (4 bytes) for a - byte (1 byte) for b (as max=17, ref below) - byte (1 byte) for a counter up to b timing: at most 15 multiplications and 15 divisions
@Itsayu7106
@Itsayu7106 8 месяцев назад
This example is from DP, I guess
@tobemaguire7389
@tobemaguire7389 7 месяцев назад
```python def robot_path(w=3, h=3): m = [] for r in range(h): row = [] for c in range(w): if r == 0 or c == 0: row.append(1) else: row.append(row[-1] + m[r-1][c]) m.append(row) return m width, height = map(int, input().split()) path = robot_path(width, height) for row in path: print(*row) ```
@JohnBoboIII
@JohnBoboIII 7 месяцев назад
Chomp?
@sushmanagupta1749
@sushmanagupta1749 7 месяцев назад
3 + 3 = 6
@xzex2609
@xzex2609 7 месяцев назад
this is 2d dynamic programming ,and your solution is simply wrong , you must solve it bottom-up , you find that how many ways there is to reach the destination and you find the first row will always be one and you solve it the other way. both climbing stair (1d dynamic ) and this is totally wrong
@xzex2609
@xzex2609 7 месяцев назад
I don't know why after all these times , you didn't learn that the solutions to dynamic programing is always from bottom-up , you always case the smallest amount to solve your way from front , you did that both on climbing stairs and this problem , no this is not the way you solve it , you better see the neet-code channel 2d dynamic programming solutions and you will understand that your way is simply wrong
Далее
How I would learn Leetcode if I could start over
18:03
Просмотров 637 тыс.
I Solved 100 LeetCode Problems
13:11
Просмотров 174 тыс.
Programming Languages Tier List 2024
16:18
Просмотров 7 тыс.
The LeetCode Fallacy
6:08
Просмотров 555 тыс.
Advice from the Top 1% of Software Engineers
10:21
Просмотров 3,3 млн
My 10 “Clean” Code Principles (Start These Now)
15:12