As usual, amazing animation work. I was initially really confused about the "start - 1" logic but after few hours of scratching my head, now it makes sense as it is used to find the beginning of a sequence. Thank you.
The first method is actually incorrect! It will only work for some of the roman strings but not for all. For example : Lets say the string is ILM or VDM. Using the first method, the output would be 949 and 495 respectively. But the correct answer here would be 1051 and 1505. This is because it does not consider the 6 cases that are mentioned, it just checks for the greater number to compare. Although the seond method is actually correct. Please correct me if I am wrong. Appreciate your help @AlgoEngine
Your example is correct, but in the LeetCode problem description, one of the given constraints is "It is guaranteed that s is a valid Roman numeral". In the Roman numeral system, the 6 cases mentioned are the *only* cases where a smaller number is allowed to precede a larger number, so ILM and VDM are *not* valid Roman numerals and would never be an input (there would also be no "correct answer"). To make them valid, you would need to write them as MLI and MDV. So because of that constraint, we don't need to worry about the scenario you gave, but I agree that if that constraint *wasn't* given, then we would have to implement error checking.
Every time I think about your videos and your visual explantations, it makes me smile and want to solve more leetcode problems. Thanks for your effort in creating these amazing animations. Thank you for your hard work; it truly makes a difference.
solved it with just 1 while loop def count_last_word_len(s): total = 0 last_space = True i = len(s) - 1 while i>=0: if (s[i] == ' ' and (last_space == True)): i-=1 elif(s[i] != ' '): last_space = False total+=1 i-=1 else: break return total print(count_last_word_len(" fly me to the moon "))
class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: i = 0 length = len(nums) j = length - 1 left = list([1]);right = list([1]) for i in range(length-1): left.append( left[i] * nums[i] ) right.append( right[i] * nums[j] ) # backwards j -= 1 right.reverse() out = [ i * j for i, j in zip(left, right) ] return out
bro till now in my life i followed neetcode solution,today i was not satisfied for his solution and hopping to drop this problem but by chance leetcode recommended your solution and i am totally astonished by your explaination and you are doing great work brother keep it up ❤❤
Can't we just append the newInterval to intervals and sort it. Theres no restriction limiting us to do that. After sorting we can apply merge interval algorithm which merges overlapping intervals.
as a bigneer in python development i don't understand how did you access l1.val , l2.val and l1.next ,l2.next . please help me to understand it. I am getting AttributeError: 'list' object has no attribute 'val' error on my system.
I love your solution, most easy and simple for the code. However, the test case is num1 = [2,7,11,15,3,6], target1 = 9, it will return [0,1], not [0, 1, 4, 5]
Your explanation is really good but when u fill up the left and right, ur red line of box is not easy to understand. Instead of that, just say multiply every left side or multiply every right side elements. Not like one by one. For example, when we calculate every left side of 5, say 2*3*4. And I think until 4:12 mins are enough to understand this question. Thank you for your explanation.
Is it possible to solve by: mid = left + right) / 2; if ((mid * mid <= x) { left = mid; } else if (mid * mid >= x) { right = mid; left = 0; } This yields results like x=9, left = 2.99999... right = 3.00001 but there is no way to decide if the final return is 2 or 3 because it infinitely closes to the actual value but how far left to the exact answer VS right to the exact answer cannot be decided. Is it possible to build a solution based on above logic? Below is the return condition I have: if (left * left < x && right * right > x && right - left <= 0.01) { return (int) Math.floor(right); } which does not work no matter how small the epsilon value is.
Start with learning/reviewing all the basic algorithms and data structures and understanding their time and space complexities. After you have the basics down, the best way to learn is to practice! I'd recommend setting aside 20-30 min for each problem (depends on how much time you have), and if you don't get it by then, just look at the solution but make sure you understand it. Then after a few days, revisit the problem and try to solve it without looking at the solution.
a different approach I came up with, since we know there are specific exception conditions we can add them to dict itself. Seems to work! dict= { 'I' : 1, 'V' : 5, 'X' : 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000, 'IV' : 4, 'IX' : 9, 'XL' : 40, 'XC' : 90, 'CD' :400, 'CM' :900 } res=0 i=0 #index while (i< len(s)): if i != len(s)-1 and \ s[i] + s[i+1] in ['IV','IX','XL','XC','CD','CM']: res+=dict[s[i]+ s[i+1]] i+=1 #skip next character since we found the exception pair elif s[i] in dict: res+=dict[s[i]] i+=1 return res
oh lol. i saw this video, thought id solve it before watching . this exactly how i did it. i think this would be the most common solution i guess The ohnly addition I would say. dicts have a `get` function that can return a default. You can do ``` my_list = group.get(sorted_word, []) my_list.append(word) my_list[sorted_word] = my_list ```` if sorted_word is not seen so far, it will return an empty list as default
This is an O( N * M log M ) solution , where N is the number of strings and M is the length of a single string . You can do better . Avoid sorting and create an array of size 26 , every string will have a unique signature . Mapping using that concept will lead to the solution to be O ( N * M ) , with 26 * O ( N * M ) space Also, using a defaultdict( list) datastructure is python can make it's implementation simpler