- Видео 806
- Просмотров 123 568
Alpha-Code
Добавлен 16 май 2023
Channel for coding interview questions.
LeetCode 962 - Maximum Width Ramp - Java
Solution, explanation, and complexity analysis for LeetCode 962 in Java
Problem Description:
leetcode.com/problems/maximum-width-ramp
Code:
github.com/AnatoliySokolov98/LeetCode/tree/main/0962
Problem Description:
leetcode.com/problems/maximum-width-ramp
Code:
github.com/AnatoliySokolov98/LeetCode/tree/main/0962
Просмотров: 166
Видео
LeetCode 1813 Sentence Similarity III - Java
Просмотров 9012 часов назад
Solution, explanation, and complexity analysis for LeetCode 1813 in Java Problem Description: leetcode.com/problems/sentence-similarity-iii/ Code: github.com/AnatoliySokolov98/LeetCode/tree/main/1813
LeetCode 1590 - Make Sum Divisible By P - Java
Просмотров 43819 часов назад
Solution, explanation, and complexity analysis for LeetCode 1590 in Java Problem Description: leetcode.com/problems/make-sum-divisible-by-p Code: github.com/AnatoliySokolov98/LeetCode/tree/main/1590
LeetCode 1497 - Check If Array Pairs Are Divisible by k - Java
Просмотров 156День назад
Solution, explanation, and complexity analysis for LeetCode 1497 in Java Problem Description: leetcode.com/problems/check-if-array-pairs-are-divisible-by-k Code: github.com/AnatoliySokolov98/LeetCode/tree/main/1497
LeetCode 432 - All O' one Data Structure - Java
Просмотров 597День назад
Solution, explanation, and complexity analysis for LeetCode 432 in Java Problem Description: leetcode.com/problems/all-oone-data-structure Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0432
LeetCode 731 - My Calendar II - Java
Просмотров 8914 дней назад
Solution, explanation, and complexity analysis for LeetCode 731 in Java Problem Description: leetcode.com/problems/my-calendar-ii Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0731
LeetCode 2416 - Sum of Prefix Scores of Strings - Java
Просмотров 7714 дней назад
Solution, explanation, and complexity analysis for LeetCode 2416 in Java Problem Description: leetcode.com/problems/sum-of-prefix-scores-of-strings/ Code: github.com/AnatoliySokolov98/LeetCode/tree/main/2416 #leetcode #java
LeetCode 2707 - Extra Characters in a String - Java
Просмотров 20714 дней назад
Solution, explanation, and complexity analysis for LeetCode 2707 in Java Problem Description: leetcode.com/problems/extra-characters-in-a-string Code: github.com/AnatoliySokolov98/LeetCode/tree/main/2707
LeetCode 440 - Kth Smallest in Lexicographical Order - Java
Просмотров 26314 дней назад
Solution, explanation, and complexity analysis for LeetCode 440 in Java Problem Description: leetcode.com/problems/k-th-smallest-in-lexicographical-order Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0440 #leetcode #java
LeetCode 386 - Lexicographical Numbers - Java
Просмотров 15914 дней назад
Solution, explanation, and complexity analysis for LeetCode 386 in Java Problem Description: leetcode.com/problems/lexicographical-numbers/ Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0386
LeetCode 214 - Shortest Palindrome - Java
Просмотров 18614 дней назад
Solution, explanation, and complexity analysis for LeetCode 214 in Java Problem Description: leetcode.com/problems/shortest-palindrome Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0214 #leetcode #java #palindrome #hashing
LeetCode 241 - Different Ways to Add Parentheses - Java
Просмотров 10821 день назад
Solution, explanation, and complexity analysis for LeetCode 241 in Java Problem Description: leetcode.com/problems/different-ways-to-add-parentheses Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0241 #leetcode #java #math
LeetCode 179 - Largest Number - Java
Просмотров 16621 день назад
Solution, explanation, and complexity analysis for LeetCode 179 in Java Problem Description: leetcode.com/problems/largest-number Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0179
LeetCode 539 - Minimum Time Difference - Java
Просмотров 13221 день назад
Solution, explanation, and complexity analysis for LeetCode 539 in Java Problem Description: leetcode.com/problems/minimum-time-difference Code: github.com/AnatoliySokolov98/LeetCode/tree/main/0539 #leetcode #java #time #sort
LeetCode 1371 - Find the Longest Substring Containing Vowels in Even Counts - Java
Просмотров 25221 день назад
Solution, explanation, and complexity analysis for LeetCode 1371 in Java Problem Description: leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/ Code: github.com/AnatoliySokolov98/LeetCode/tree/main/1371 #leetcode #java #bits #strings #vowels
LeetCode 2419 - Longest Subarray With Maximum Bitwise And - Java
Просмотров 7621 день назад
LeetCode 2419 - Longest Subarray With Maximum Bitwise And - Java
LeetCode 1310 - XOR Queries of a Subarray - Java
Просмотров 9028 дней назад
LeetCode 1310 - XOR Queries of a Subarray - Java
LeetCode 2220 - Minimum Bit Flips To Convert Number - Java
Просмотров 75Месяц назад
LeetCode 2220 - Minimum Bit Flips To Convert Number - Java
LeetCode 2807 - Insert Greatest Common Divisors in Linked List - Java
Просмотров 71Месяц назад
LeetCode 2807 - Insert Greatest Common Divisors in Linked List - Java
LeetCode 725 - Split Linked List In Parts - Java
Просмотров 137Месяц назад
LeetCode 725 - Split Linked List In Parts - Java
LeetCode 1367 - Linked List in Binary Tree - Java
Просмотров 111Месяц назад
LeetCode 1367 - Linked List in Binary Tree - Java
LeetCode 3217 - Delete Nodes From Linked List Present in Array - Java
Просмотров 112Месяц назад
LeetCode 3217 - Delete Nodes From Linked List Present in Array - Java
LeetCode 2028 - Find Missing Observations - Java
Просмотров 173Месяц назад
LeetCode 2028 - Find Missing Observations - Java
LeetCode 874 - Walking Robot Simulation - Java
Просмотров 377Месяц назад
LeetCode 874 - Walking Robot Simulation - Java
LeetCode 1945 - Sum of Digits of String After Convert - Java
Просмотров 153Месяц назад
LeetCode 1945 - Sum of Digits of String After Convert - Java
LeetCode 1894 - Find The Student That Will Replace The Chalk - Java
Просмотров 307Месяц назад
LeetCode 1894 - Find The Student That Will Replace The Chalk - Java
LeetCode 2022 - Convert 1D Array into 2D Array - Java
Просмотров 93Месяц назад
LeetCode 2022 - Convert 1D Array into 2D Array - Java
LeetCode 2699 - Modify Graph Edge Weights - Java
Просмотров 1,9 тыс.Месяц назад
LeetCode 2699 - Modify Graph Edge Weights - Java
LeetCode 947 - Most Stones Removed With Same Row or Column - Java
Просмотров 373Месяц назад
LeetCode 947 - Most Stones Removed With Same Row or Column - Java
LeetCode 1905 - Count Sub Islands - Java
Просмотров 165Месяц назад
LeetCode 1905 - Count Sub Islands - Java
thanks for discussing all the possible solutions. it helps to see the problems from different sides.
goated
Thank youu
The solution is straightforward, but I could never have come up with it myself.😢
nice explanation
Very good explanation. I was having a hard time understanding the prefix sum thing, thank you. I love that you use Excalidraw, it makes it very beautiful and readable. One note: mod still works fine for negative numbers. k mod p = k + n*p mod p, so adding or subtracting p is essentially the same as adding 0.
I think this is language dependent, could be wrong. Mod of negative numbers gives different results in different languages I think.
Mod works fine. The language does not.
Probably not works in Java then, I tested in Python and it works fine. But nice to know that I should be careful with that.
@@arthurtancredi python negative mods work fine
Explained this code very well
loved the explanation 👍
awesome video :)
thanks dude
Way better solution than neetcode!
the solution is amazing , you are doing a great job . I am following you for a while.🙌
thx, glad to hear it.
dude voice too low.
Thank you, this is very clever implementation. PS Using the Integer[] cache will result in an impressive 6ms execution time.
Great explanation man 👌, Loved it!
This is the best and most cleaned and well-explained, Thank you so much for the excellent content. Subscribed
it was really impressive solution
The dedication is impressive. Keep up the good work.
Great explanation
doing a great job man , helping a. noobie like me .
Subscribed. This is brutal! I can barely understand and follow the algorithm, even with chatgpt, the solution and a video explanation I'm still having difficulties understanding how to apply this approach to another problem like counting all occurrences of lowercase characters (a-z) in a given string. Masking is extremely powerful and is a must-have tool, so thank you for the video!
Man, you rock! Thanks for all you're doing!
nice solution!
Thank you, you are a legend.
Can you explain contest questions aswell?
Gonna take a break from doing videos for those and easy problems I think. Easies don't really need a video and contests is just too many weekly problems with the work I currently have.
@@Alpha-Code i see nevertheless i appreciate your consistency in uploading the videos everyday for free
I just realised, you use Ai to generate your thumbnails right?
yes
I had an interesting problem where I accidentally derived this algorithm. The question was to place each number of an input array into one of 2 arrays as you go, depending on which of the 2 arrays had more elements strictly greater than the current number at the time. I tracked a sorted version of each array, comparing the bisect.bisect_right index relative to the size of the array to get the strictly greater than count and then using bisect.insort to insert into the appropriate sorted array. The funny thing is I did that recently and didn't see the application it had to this problem haha
Love your videos, please make videos daily
Arriving at that intuition is key. I started with a subset based solution, got TLE, did early pruning and returns with memoisation if we already have a result. Still got TLE. I guess this is more like a math observation question
Yeah, but its like basic math. I can see how going down a dp route would make sense but then you need to keep track of an index and a total which is probably n^2 so would fail.
Nice video
Voice too low. Can't even hear on full volume
I created a if-else hell for directions it works just as the simulation says, but I wonder how what can we do if the inputs were large ? Binary search or something ?
Would definitely be interesting if that were the case, we would basically have to figure out if we can go from a to b with no obstacle in the path in log(n) or better time. Something I'm thinking of is like maybe have a prefix sum of obstacles in the grid or something row by row and column by column that we can probably bin search with. So if you check obstacles at b vs obstacles at a we should have the same sum if there are none in the path then you can bin search through that space to find first place where it differs maybe. This would work if the obstacle numbers aren't too big but we have a lot of commands and the commands aren't 1-10 but like 1-10^4 or something.
What's the proof of it if we replace one by one every -1 weight to positive weight and at some point its less than equal to target is our result?
Proof can be thought of in a couple steps: a. If we replace every single -1 with a 1 and we don't achieve this, then it is impossible and we are done. b. provided a is possible, if we replace one edge at a time we will eventually get to a point where the weight of the shortest path <= target. c. when we achieve b, we know for sure the last edge we added was necessary in order to do so (since without it we were not able to) and every edge we have not yet added is not required anymore. d. if we increase the weight of the last edge we added to make the path == target, there can be no shorter path because this edge has to be included in the shortest path from c and every path with this edge will increase equally. e. we can then make every edge not used = infinity to make sure this remains the shortest path.
12:15 for this subproblem shouldn't the result of this be 3 (just write e's then replace with T and D)? If you remove the first e then split it into TE and DE woudln't that return 2 + 2 which is 4? please correct me if i am wrong
Yeah ur right should be 3. Splitting the way I showed in the video will be 4 but optimal is 3. Best split is getting rid of the last character right away then you have TEDE which then can split into TED and empty string so we get 3.
Why are we turning on only one at a time?
@@tanishgotti3659 to know exactly which nodes turned on produce a valid shortest path.
Great job
What if do something like this : We convert all -1 nodes into 1 and we try to save the shortes path. When we get the shortes path, we will travers it backwards and check if any of the edges is -1. We can add the difference between the shortest path and target to the first edge that was originally -1 For the rest we can update them with the MAX
I did something similar in my Python solution. you can do that also. Save all paths during the Djikstra and sort by number of -1's then the path length. Then make one of the -1's in the path the difference and MAX the rest. Since n is only 100 its fine to save entire paths in the heap as you are finding shortest path. Don't need to go backwards just make every -1 into a 1 and add them all to graph and store (#of negative ones, dist, nodes in path) in the heap and use a min heap.
You can't pick the first edge that was originally -1 every time and expect it to satisfy. Picking different edges in the backwards traversal might give a different shortest path because other nodes in that path will be 1 now. See this test case: "edges = [[0,1,-1],[2,0,2],[3,2,6],[2,1,10],[3,0,-1]] source=1 destination =3 target =12" "[[0,1,1],[2,0,2],[3,2,6],[2,1,10],[3,0,11]]" is wrong "[[0,1,11],[2,0,2],[3,2,6],[2,1,10],[3,0,1]]" is correct.
@@tanishgotti3659 I did same mistake in my code
@@hittarthgoyal6689 this was a tricky corner case.
@@tanishgotti3659 that's why you set the last -1 value you turned on to be target - shortest path to make sure it is the same once you get one smaller than target i.imgur.com/lyzG1U3.png and every node outside the path to MAX. Seems fine to me either way, your values were just wrong I think. In your first set of edges you just set the -1's incorrectly.
You can also use for loop and scanner class to solve it... If it is possible
Thanks, Good explanation
Had some background noise towards the end, I'll fix that up in the future.
Thanks for the video, but You can use grid2 instead of creating new visited array
@@berserk.4121 yup I mentioned that in the video and said I don’t want to modify inputs
Excellent explanation !! Hats off to your consistency.
Thanks for explaning why we need 3*3 grid, because once you figure that out its basically number of island problem.
Just my intuition from what you have explained so far correct me if I am wrong. So for printer(i, j) it returns the minimal number of operations needed to print characters from [i, j]. In the for loop you are splitting whenever we see a matching character with our start character in the remaining string form [i + 1, j]. In that case we know that we could possibly achieve a lower number of operations for printing the first part [i, k]. The reason why we pass in dp(i, k - 1) is because when we have matching characters we are assuming that at index i we are already printing a sequence of similar character from i until index k inclusive. (i.e. "aba", when 0 == 2, at that instance we assume that we printed 'aaa' already, we actually only really consider the cost of printing 'ab' -> when we print 'a' -> 'aaa' and then when we print 'b' we are actually replacing the middle 'a' -> 'aba'.
@@chrisgu7991 yup sounds right to me
I think pre/in/post traversal just clicked in my mind haha I wasn't sure why I always used pre for dfs xD
Can use pre sometimes just need to understand the traversal order that you need for a particular problem.
I think it would be a good add on to show why a greedy method won't work here. That being just match the first occurrence with the last occurrence of the character trying to cover as many same characters as possible. Apart from that, excellent explanation
AlphaCode!!!
Very hard to come up with a solution if you have never seen this problem before
yea, ive seen a few like this before but hard to come up with all the edge cases. I think for some other ones I could build all palindromes and binary search through them or something.
Quite a bit of edge cases in this problem, I was able to get to the reversing the first n/2 digits and subtracting and adding 1 to the significant bits but eventually gave up on trying to search for measures that would take care of these 101 and 99 cases. Glad I was able to come across your video! Great explanation, I wish you all the best in everything that you are doing. Thank you tons for posting content that helps people like us!
Nice solution These types of problems can be so annoying
Yeah this one was pretty annoying :)