![Alpha-Code](/img/default-banner.jpg)
- 772
- 107 608
Alpha-Code
เข้าร่วมเมื่อ 16 พ.ค. 2023
Channel for coding interview questions.
LeetCode 592 - Fraction Addition and Subtraction - Java
Solution, explanation, and complexity analysis for LeetCode 593 in Java
Problem Description:
leetcode.com/problems/fraction-addition-and-subtraction/
Code:
github.com/AnatoliySokolov98/LeetCode/tree/main/0592
#java #leetcode #maths #fraction
Problem Description:
leetcode.com/problems/fraction-addition-and-subtraction/
Code:
github.com/AnatoliySokolov98/LeetCode/tree/main/0592
#java #leetcode #maths #fraction
มุมมอง: 223
วีดีโอ
LeetCode 476 - Number Complement - Java
มุมมอง 952 ชั่วโมงที่ผ่านมา
Solution, explanation, and complexity analysis for LeetCode 476 in Java Problem Description: leetcode.com/problems/number-complement Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0476 #leetcode #java #bits #bitmanipulation
LeetCode 664 - Strange Printer - Java
มุมมอง 6094 ชั่วโมงที่ผ่านมา
Solution, explanation, and complexity analysis for LeetCode 664 in Java Problem Description: leetcode.com/problems/strange-printer Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0664 #leetcode #java #dynamicprogramming #printer
LeetCode 1140 - Stone Game II - Java
มุมมอง 4777 ชั่วโมงที่ผ่านมา
Solution, explanation, and complexity analysis for LeetCode 1140 in Java Problem Description: leetcode.com/problems/stone-game-ii Code: github.com/AnatoliySokolov98/LeetCode/tree/main/1140 #leetcode #java #dynamicprogramming
LeetCode 650 - 2 Keys Keyboard - Java
มุมมอง 1429 ชั่วโมงที่ผ่านมา
Solution, explanation, and complexity analysis for LeetCode 650 in Java Problem Description: leetcode.com/problems/2-keys-keyboard/description Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0650
LeetCode 264 - Ugly Number II - Java
มุมมอง 11112 ชั่วโมงที่ผ่านมา
Problem Description: Solution, explanation, and complexity analysis for LeetCode 264 in Java Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0264
LeetCode 1937 - Maximum Number of Points With Cost - Java
มุมมอง 29814 ชั่วโมงที่ผ่านมา
Solution, explanation, and complexity analysis for LeetCode 1937 in Java Problem Description: leetcode.com/problems/maximum-number-of-points-with-cost Code: github.com/AnatoliySokolov98/LeetCode/tree/main/1937 #leetcode #java #array #dynamicprogramming
LeetCode 624 - Maximum Distance in Arrays - Java
มุมมอง 25016 ชั่วโมงที่ผ่านมา
Solution, explanation, and complexity analysis for LeetCode 624 in Java Problem Description: leetcode.com/problems/maximum-distance-in-arrays Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0624 #leetcode #java #array #greedy
LeetCode 860 - Lemonade Change - Java
มุมมอง 4919 ชั่วโมงที่ผ่านมา
Solution, explanation, and complexity analysis for LeetCode 860 in Java Problem Description: leetcode.com/problems/lemonade-change/ Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0860 #leetcode #java #greedy #simulation
LeetCode 719 - Find K-th Smallest Pair Distance - Java
มุมมอง 20421 ชั่วโมงที่ผ่านมา
Solution, explanation, and complexity analysis for LeetCode 719 in Java Problem Description: leetcode.com/problems/find-k-th-smallest-pair-distance Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0719 #leetcode #binarysearch #slidingwindow #java
LeetCode 40 - Combination Sum II - Java
มุมมอง 116วันที่ผ่านมา
Solution, explanation, and complexity analysis for LeetCode 40 in Java Problem Description: leetcode.com/problems/combination-sum-ii/ Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0040
LeetCode 703 - Kth Largest Element in a Stream - Java
มุมมอง 126วันที่ผ่านมา
Solution, explanation, and complexity analysis for LeetCode 703 in Java Problem Description: leetcode.com/problems/kth-largest-element-in-a-stream Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0703
LeetCode 1568 - Minimum Number of Days to Disconnect Island - Java
มุมมอง 628วันที่ผ่านมา
Solution, explanation, and complexity analysis for LeetCode 1568 in Java Problem Description: leetcode.com/problems/minimum-number-of-days-to-disconnect-island/ Code: github.com/AnatoliySokolov98/LeetCode/tree/main/1568
LeetCode 959 - Regions Cut By Slashes - Java
มุมมอง 503วันที่ผ่านมา
Solution, explanation, and complexity analysis for LeetCode 959 in Java Problem Description: leetcode.com/problems/regions-cut-by-slashes Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0959
LeetCode 840 - Magic Squares In Grid - Java
มุมมอง 42714 วันที่ผ่านมา
Solution, explanation, and complexity analysis for LeetCode 840 in Java Problem Description: leetcode.com/problems/magic-squares-in-grid Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0840
LeetCode 885 - Spiral Matrix III - Java
มุมมอง 20414 วันที่ผ่านมา
LeetCode 885 - Spiral Matrix III - Java
LeetCode 273 - Integer To English Words - Java
มุมมอง 38514 วันที่ผ่านมา
LeetCode 273 - Integer To English Words - Java
LeetCode 3016 - Minimum Number of Pushes to Type Word II - Java
มุมมอง 26114 วันที่ผ่านมา
LeetCode 3016 - Minimum Number of Pushes to Type Word II - Java
LeetCode 2053 - Kth Distinct String in An Array - Java
มุมมอง 10214 วันที่ผ่านมา
LeetCode 2053 - Kth Distinct String in An Array - Java
LeetCode 1508 - Range Sum of Sorted Subarray Sums - Java
มุมมอง 12314 วันที่ผ่านมา
LeetCode 1508 - Range Sum of Sorted Subarray Sums - Java
LeetCode 1460 - Make Two Arrays Equal by Reversing Subarrays - Java
มุมมอง 12314 วันที่ผ่านมา
LeetCode 1460 - Make Two Arrays Equal by Reversing Subarrays - Java
LeetCode 2134 - Minimum Swaps to Group All 1's Together - Java
มุมมอง 15321 วันที่ผ่านมา
LeetCode 2134 - Minimum Swaps to Group All 1's Together - Java
LeetCode 2678 - Number of Senior Citizens - Java
มุมมอง 7621 วันที่ผ่านมา
LeetCode 2678 - Number of Senior Citizens - Java
LeetCode 1105 - Filling Bookcase Shelves - Java
มุมมอง 23621 วันที่ผ่านมา
LeetCode 1105 - Filling Bookcase Shelves - Java
LeetCode 1653 - Minimum Deletions to Make String Balanced - Java
มุมมอง 7021 วันที่ผ่านมา
LeetCode 1653 - Minimum Deletions to Make String Balanced - Java
LeetCode 1395 - Count Number of Teams - Java
มุมมอง 9721 วันที่ผ่านมา
LeetCode 1395 - Count Number of Teams - Java
LeetCode Weekly Contest 408 - All Solutions - Python
มุมมอง 5721 วันที่ผ่านมา
LeetCode Weekly Contest 408 - All Solutions - Python
LeetCode 2045 - Second Minimum Time to Reach Destination - Java
มุมมอง 20821 วันที่ผ่านมา
LeetCode 2045 - Second Minimum Time to Reach Destination - Java
LeetCode 2976 - Minimum Cost To Convert String I - Java
มุมมอง 17821 วันที่ผ่านมา
LeetCode 2976 - Minimum Cost To Convert String I - Java
LeetCode 1334 - Find the City With Smallest Amount of Neighbors at a Threshold Distance - Java
มุมมอง 11228 วันที่ผ่านมา
LeetCode 1334 - Find the City With Smallest Amount of Neighbors at a Threshold Distance - Java
Thanks! This solution is by far the clearest one I have seen on TH-cam. But is there any way to build the intuition for those kind of problems? Actually by observation, I can see that for most questions that requires counting subarray, sliding window is basically the way to go. My intuition was sliding window too when I attempted this problem, but I have no idea how did you come up with the 3 pointer approach. Any insights on this?
You have to know what 3 pointer is useful for as opposed to 2 pointer. You can do all sliding window problems with 3 pointers, just usually the third one is redundant and doesn't give any extra information. So once you get how the third one helps, you can start asking yourself do I need it when you do the problems or do 2 give me all the info I need.
@@Alpha-Code Thanks! I will try to delve in into how the third pointer is redundant in most cases but useful in this one. I just have one more question though and this is sth that I have been stuck when dealing with this problem - why is res += (min(lastMin, lastMax) - start + 1)? I don’t quite understand how come sth like (r-l+1) could be the number of subarray to add. How come in this formula we can find the number of subarray that fulfill the condition of the fixed bound? Sorry if this seems a little bit confusing, but I am quite lost about how this formula actually applies. Is there a mathematical way to think about this? And possibly how to apply this in other questions (e.g the scenario)?
@@victor-fo9zf (min(lastMin, lastMax) - l + 1) is used cause you have to include lastMin and lastMax in all valid arrays so you take the min as this is the smallest array that includes both. Then anything that starts from there to start is a valid subarray if they end with r. And r - l + 1 is cause the number of arrays between 2 indices is r - l + 1.
Great video as usual, just a small suggestion, Could you please explain the problem first in detail? I still don't understand the problem at all :(
You're supposed to combine (add or subtract) fractions. Ex: 1/3 + 1/3 = 2/3
Yeah I can spend more time on that sure. Thought this one was pretty clear just add fractions and simplify answer. But I'll spend more time on problem descriptions in the future no worries.
Nice
That's a really great explanation! The only part I'm a bit confused about is why this algorithm correctly handles strings where there are groups of letters in the middle of the string that are the same as the first letter. For example, "abaac".
for that one you look for all characters that match starting one so abaa matches which would go down to aba then that goes down to ab for 0 cost. I explained something similar in the updated video for this problem.
@@Alpha-Code You're absolutely right! Thank you. The reason I couldn't figure it out was that I was building the decision tree incorrectly. If anyone else has the same question I did, try building a decision tree using a simple example.
I was quite close to this solution, just incrementing left pointer sequence was the miss. Thank you for this explanation.
thank you!
Thanks for the video but actually you start at the end because you recurse to the end of the string with the line 27. I think It would be better if you explain from backwards.
in line 27, it's recursing to the next character from the front. From i, j to i+1, j where i is front and j is end. Can do backwards or forwards should be the same so forwards just seems more intuitive if it doesn't matter.
@@Alpha-Code I meant, the line 27 will be executed to the end of the string when i and j are same so you actually split the string from at the end of it. Am I wrong?
@@MehmetDemir-xi3yy You can split from front or end but in this case it splits from front, you can log i and j and see for yourself though.
@@Alpha-Code for input "abcabc" when you add this line below to line 30, "System.out.println(s.substring(i, k+1));" you will get output as cabc bcab abca so you are doing from backward to forward
@@MehmetDemir-xi3yy Put that line at the top of the dp and just print i and j. Here's what I got for "abcabc" but either way result should be same imgur.com/IBOM5hR
398 days is crazy
Please use dark mode next time It is just hurting my eyes
@@vishaalkumaranandan2894 th-cam.com/video/uAraW9iGmRQ/w-d-xo.htmlsi=ML0XFaIiQeUmB7jf new one with dark mode
If you wrote the whole code in the video with the explanation again, It would have been nice and easy to understand it👍👍
Yeah I mostly write the code live. This one wasn't too long so probably could have done it. For the long ones, I prefer to write it ahead of time so the videos aren't like 30+ min.
Very nice explanation! Subscribed
I really like the way you explain this! You basically did a breakdown and then explained each part of your code that shows the breakdown steps and it all took about 15 min! Thanks man, I hope to see more of this! Edit: Also, not many resources do this in java!
I have doubt in our example when we will return hundred as the Alis score. At that time, we are taking minimum of our result and recursion call at that time. Bob will return minimum of hundred and his score. Can you explain how we are ensuring that Alice will get maximum I am a little bit confused. For bob min(result,100) in his case, we will end up with minimum score of bob
The idea is in our function we are never calculating Bob's score. On Bob's turn we are simply returning the minimum for Alice given all the moves Bob can make. And on Alice's turn we are returning the maximum for Alice given all the moves that she can make. The function returns Alice's score given they both play optimally without calculating Bob's score at all, because the less points Alice gets the more Bob gets. So we can just focus on 1 score. So for example 2 you are referring to, the optimal play is Alice takes pile 1, Bob takes pile 2, Alice takes pile 3, Bob takes piles 4 and 5, and Alice takes pile 6. Alice's score is 1 + 3 + 100 from that = 104.
@@Alpha-Code thank bro got it
Bro I understand the solution of dp but fails to create my own solution what should I do to better in dp please reply
You should do easier dp problems until you can create your own solutions then once you can do easier ones yourself consistently try slowly increasing difficulty. Start with some really basic ones where you have some basic choices to make like go down or right or something. Then go to mediums then go to hards.
Medium problem? why not easy?
Since both are playing optimally and there won't be any ties, and alice is starting the game, so he will always win. simply return true
wow, neetcode even got it wrong in this question but you have done a great job !!! thank
I was legit waiting for your video on this, thanks, as usual awesome explanation
only person to explain the question properly ty
amazing solution! Btw what is the website used for simulation? Thanks
excalidraw
@@Alpha-Code Thank you!
Goat
nice explanation !
NICE SUPER EXCELLENT MOTIVATED
Hey can you post contest solutions aswell?
Haven't done contests this week. If I do them later on during the week, I'll post them.
nice explanation!
nice solution!
Keep going
Nice🔥🔥
Try to improve sound
Sound is fine for me is there anything in particular I can improve?
Really loved the explanation,
I can code after watching explanation
Amazing explanation but how would I comup with this soln
You have to recognize what is the same between 2 boxes that are next to each other. That's usually the key to these tricky DP problems, try to think of which dimension you can simplify in some way after coming up with the brute force dp solution. You have to go through every square so it has to be the checking the whole row for every box that has to be simplified Then try to figure out how that's possible. You can try coding up the easy DP solution then look at it and see how you are calculating each thing and the similarities. Might help to actually do a dry run of that just like I did in the picture and then spot the similar values and find a way to avoid recalculations.
Great job figuring out you only needed to keep the prev min/max, everyone else seemed to be using some version of a 2 element heap, and/or checking they weren't from the same arrays.
Thanks Bro
Hi bro whenever I fails to come up with solution I watch your videos keep going and try to upload videos early
Nice solution
Thanks
@@Alpha-Code when you go about hard problems do you just immediately recognize what algorithms to use and then work out the details or does it take a while to think of it
@@CheeseFest- Depends on the difficulty. Some are too hard to figure it out right away, like some LeetCode contest problems are solved in 15-20 min by the winner so they would take me an hour+ probably. Some hards are closer to a medium. Looking at the constraints right away helps to eliminate things though, then you can think what is usable from what is left. Like for this problem you can see that you need nlogn or better so can eliminate n^2 solutions right away. Then also recognizing patterns you've seen before helps, like this type of problem is one I've seen before where you can't calculate all the values but you can binary search. I've done this one before which is the same idea leetcode.com/problems/kth-smallest-number-in-multiplication-table/description/ and a few others like it.
great vids , keep it up!!
Really nice explanation, really appreciate it !
Helped me alot ! Thanks
subbed !
Thank you, your detailed explanations are helping me a lot. Finally understood why counting connections didn't worked.
Glad to hear it. If I miss anything or there's some improvement that cant be made, just let me know.
class Solution: def regionsBySlashes(self, g: List[str]) -> int: n,c,d=len(g),0,[[-1,0],[0,1],[1,0],[0,-1]] a=[[[0]*4 for _ in range(n)]for _ in range(n)] b={ '/':[[3],[2],[1],[0]], '\\':[[1],[0],[3],[2]], ' ':[[1,3],[0,2],[1,3],[0,2]]} def f(y,x,z): if 0<=y<n and 0<=x<n and a[y][x][z]==0: a[y][x][z]=c for i in b[g[y][x]][z]:f(y,x,i) f(y+d[z][0],x+d[z][1],(z+2)%4) for y in range(n): for x in range(n): for z in range(4): if a[y][x][z]==0: c+=1 f(y,x,z) return c
Very nicely explained. Thanks
But there is something I really don't understand why multiply rows and cols by 3? may sound stupid but why not by 2, 4 or 5?
It had to be a square to keep the same connections for one. Can't have 2x2 because of the example I showed here th-cam.com/video/8Rgz15cpTts/w-d-xo.html . 3x3 is the smallest that lets you connect everything by not going diagonally. Maybe 2x2 is doable with more checks I guess but you'd have to go diagonally and have more checks. And I think anything bigger than 3x3 also works, just 3x3 is the smallest.
nice as always, 🙃
saved my streak!!
Same here 😅
nice solution it really help me to understand
space complexity is O(row * col) not O(1)
@@fairoossahfeerulwasihf1139 we only store the result which is row * col and everything else is constant space but result doesn't count for space complexity since we always need it.
very good explanation! Glad I found your video
Thanks, glad you liked it.
not able to understand the solution a bit confusing
It's basically just you always move x steps in a straight line where x is increasing by 1 every 2 times you change direction. And you are out of bounds a lot so just record only when you are in bounds. And directions go in a right -> down -> left -> up -> right loop. Maybe try doing some of the earlier spiral matrix problems first if you don't get this one.
nice one cheers