✏️ Have problem suggestions you would like me to solve? Feel free to comment them below :~)
@vijaychikkannavar50602 жыл бұрын
last line should be return res[::-1] to reverse it back to original format
@sagargulati6392 жыл бұрын
@@vijaychikkannavar5060 Yes you are right.
@DeeBall2 жыл бұрын
@@vijaychikkannavar5060 no
@subhashreesahoo6125 Жыл бұрын
Diagonal Matrix problem
@aaa_17119 ай бұрын
Why use ord?? Since we know it can either be 0 or 1. Why not just a string comparison and assign. Like, if it's "0", then digitA is 0, else 1 Same for digitB Good implementation though
@shakhzod_shermatovАй бұрын
Your way of speaking is astonishing to be honest, without repetition, concise, and very structured. It's difficult even for native speakers to explain at this level, I guess. Thanks for sharing your knowledge. Huge respect for you! You deserve it.❤
@yoshi49803 жыл бұрын
an easy problem that can get boggled down in edge cases, really like some the tricks you used. great video as always!
@srinadhp3 жыл бұрын
Love the way you write the code. Concise. Love the loop of max of lengths. Great explanation as usual
@sunnilabeouf3 жыл бұрын
Can't we just use int(a[i]) to cast it to an integer?
@NeetCode3 жыл бұрын
Yeah, you're right, I just remembered that. Thanks for pointing it out.
@surbhirohilla51392 жыл бұрын
I was thinking the same and thougth what's the meaning of ord(0) which I can't understand😂lol, it was a mistake
@stove.d2 жыл бұрын
@@surbhirohilla5139 ord() converts each string value to the integer representation of the unicode value (i.e. ASCII table), so subtracting ord("0") from ord("n") where 'n' is any number 0-9 will return that number. In ASCII, "0"=48 and "9"=57, so in that case `ord("9") - ord("0")` = 57 - 48 = 9
@muzikjay2 жыл бұрын
"Let's go back to elementary school and add these together. What happens when we add 1 and 1?.....0?" 😂
@EmilyHopeGrove9 ай бұрын
this...
@sanjayp70273 жыл бұрын
how is this a easy problem :|
@LetCode966665 ай бұрын
It's an easy but long problem for me. I solved it my way without any math 😅
@strawberriesandcream28632 жыл бұрын
thanks, you're a lifesaver when it comes to leetcode
@rachnaramkumar2 жыл бұрын
Isn't the time complexity quadratic since the addition of the 'char' string to the 'res' string copies all the contents and creates a new string for every loop since strings in python are immutable?
@ohyash2 жыл бұрын
Yes. I'd append to the result string. And then reverse the string once in the end. Or push the result values to a stack and then form a result string in the end popping out from the stack.
@airysm3 жыл бұрын
Hi at 5:10, couldnt you use int() instead of ord() to cast the char?
@blaiserodrigues29903 жыл бұрын
Yes. you can.
@akhileshb58592 жыл бұрын
You are a good teacher. Keep up the good work. Thank you!
@beldam942 жыл бұрын
This code is simply beautiful. Thanks.
@BurhanAijaz Жыл бұрын
we can use the same code for the question : leetcode 413(Add strings),just replace 2 by 10
@BurhanAijaz Жыл бұрын
code: class Solution: def addStrings(self, a: str, b: str) -> str: ans="" carry=0 a,b=a[::-1],b[::-1] for i in range(max(len(a),len(b))): digitA = ord(a[i])-ord('0') if i
@vikasz2 Жыл бұрын
def addBinary(self, a: str, b: str) -> str: a = int(a,2) b = int(b,2) res = a+b return bin(res)[2:]
@suriya1132 ай бұрын
Bro can you explain to me this code please
@sagargulati6392 жыл бұрын
The returned res should be reversed to get the original output. return res[::-1]
@pranavsharma74792 жыл бұрын
nah see how is appending
@sriramkarthikakella2033 Жыл бұрын
For converting it to integer, can we not do int(a[i]) and int(b[i]) instead of ord(a[i])-ord("0") and ord(b[i])-ord("0")?
@fadsa342 Жыл бұрын
that worked for me in my solution
@cedriczhang612912 күн бұрын
def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ # Convert binary strings to integers num1 = int(a, 2) num2 = int(b, 2) # Compute the sum result = num1 + num2 # Convert the sum back to binary and remove the '0b' prefix return bin(result)[2:]
@MaxFung Жыл бұрын
Youre an amazing teacher mr. neet!
@clara84903 жыл бұрын
Amazing video and nice explanation!!!
@karthikkumaresan10913 жыл бұрын
What is the time complexity of the string concats in python(eg. char + res in the example above)
@XzcutioneR2 Жыл бұрын
Why not just do bin(int(a, 2) + int(b, 2))[2:] ?
@seenumadhavan4451 Жыл бұрын
There is no time complexity and space complexity mentioned in the problem. This should be fine i guess
@ShubhamShekhar-cm6rs Жыл бұрын
Convert the array into non non-decreasing array by replacing elements with its subarray sum if necessary. for example, input=[3, 8, 5, 2, 10], output=[3,8,17] input=[1,2,3,4,5],output=[1,2,3,4,5] input=[5,4,3,2,1],output=[5,10] input=[],output=[] Please solve this problem.
@RohanVenkateshGupta2 жыл бұрын
Wonderful Explanation 👍👍👍
@gagansuneja70292 жыл бұрын
we could have appended 0* (a.size - b.size) to the shorter string, saving time in reversal of both the strings. Later reversing the final string
@andrewromanenkov69293 жыл бұрын
Answer in 2 lines of code: c = bin(int(a,2) + int(b,2)) return c[2:]
@willd98072 жыл бұрын
print(eval())
@ohyash2 жыл бұрын
Interviewer: _Pikachu face_
@Levelord922 жыл бұрын
yeah, put 9223372036854775807 and 9223372036854775807 as summands and see what happens, genius
@malh85111 ай бұрын
Great Explanation! Might be cleaner to use the divmod() function instead of % and //. Eg. char, carry = divmod(total, 2) res = str(char) + res
@triplestrikee8753 жыл бұрын
Very well explained, but don't we need to return res[::-1]?
@triplestrikee8753 жыл бұрын
Never mind, I thought res = char + res in the loop is same as res += char. My bad.
@ohyash2 жыл бұрын
@@triplestrikee875 not the same.. Prepending char in the beginning of the string makes python have to copy all the result values everytime. It's better to append chars throughout and then reverse before returning in the end
@torontonian772 жыл бұрын
one thing I never knew before is that you can use f-strings to convert int to binary. def binary_conversion(num): return f'{num:b}' so binary_conversion(1000) will get a result of 1111101000
@RobertMcHalffey Жыл бұрын
Python solution converted to JavaScript: var addBinary = function(a, b) { let totalSum = ''; let carry = 0; a = a.split('').reverse().join(''); b = b.split('').reverse().join(''); for (let i = 0; i < Math.max(a.length, b.length); i++){ const digitA = parseInt(i < a.length ? a[i] : 0); const digitB = parseInt(i < b.length ? b[i] : 0); const total = digitA + digitB + carry; const currentSum = total % 2; totalSum = currentSum + totalSum; carry = Math.floor(total / 2); } return carry ? 1 + totalSum : totalSum; };
@jankeshchakravarthy9389 Жыл бұрын
Nice explanation. Did you forget to reverse the result??
@ShivangiSingh-wc3gk2 ай бұрын
I was thinking that the interviewer would want the bit manipulation solution for this
@geraldakorli11 ай бұрын
Why didn't you use int function on the values a[i] and b[i]?
@antoinenijhuis45010 ай бұрын
Why are we usig "ord" instead of "int(str)" ?? @NeetCode
@alexgolomb36310 ай бұрын
Thank you for explaining what happens when you add "11" and "11". I was completely lost but not anymore thanks to you!
@Salah-YT2 жыл бұрын
thank u bro it is done and passed but I don't understand what I'm doing so what should I do to be better in Leetcode for everything I must find a tutorial so the tutorial killing me bro please help me out so more than a year I did a lot of HTML CSS javascript and 1000 of course and now python, why I can't, make a project and why I can't solve one easy problem in Leetcode or code war so I don't know bro and I fill I know everything but I can't code by my self must follow someone bro thx
@willd98072 жыл бұрын
I havent run the code, but did you get a character short each time from your reversing method. a,b = reversed(a), reversed(b)
@mruduladdipalli54172 жыл бұрын
I implemented same approach for Dart, it gave Time Limit Exceeded, but for java it worked :(
@aaa_17119 ай бұрын
Why use ord?? Since we know it can either be 0 or 1. Why not just a string comparison and assign. Like, if it's "0", then digitA is 0, else 1 Same for digitB Good implementation though
@abisheknair35452 жыл бұрын
int(a[i]) worked succesfully. so why did u use ord?
@duncanproctor71602 жыл бұрын
Why not just pad the shorter string with leading zeros?
@Levelord922 жыл бұрын
interesting...
@fadsa342 Жыл бұрын
that's what I did. I guess technically by padding you're taking up more space and the operation isn't free since you're creating a new string and iterating over the existing one.
@mehmetnadi89302 жыл бұрын
is it normal to find "easy" questions hard? i mean i don't think most of the easy question are actually easy ngl :///
@NeetCode2 жыл бұрын
Absolutely, I struggled a lot with easy questions when I started. Eventually you will make progress!
@pcccmn2 жыл бұрын
yeah same feeling. I'm doing the Blind 75 Easy questions. Out of 20 questions I have attempted, I don't know how solve any :) It is what it is. I'm not smart, but that's ok. Traditional hardwork and repetition has never failed me. Good luck to you bro, for whatever reasons you're studying DSA.
@huhuboss82742 жыл бұрын
@@pcccmn how is it going? Did you improve?
@chamahge35184 ай бұрын
i have a question for that carry = total//2 what if we add only 1 and 0 this still will give me a carry even though the result will be just 1 can anyone please help me with this?
Why not bin to int -> add 2 ints-> int to bin and return
@NeetCode3 жыл бұрын
Yeah, I think that's also a valid solution
@stan88513 жыл бұрын
I don't think the interviewer want to see this solution, they want to see how you deal with the edge cases of this problem
@roshni9623 жыл бұрын
@@NeetCode Wont work for INT_MAX
@aniruddh34232 жыл бұрын
idk if this counts as cheating but return str(bin(int(a,2)+int(b,2)))[2:]
@FaizanShaikh-zg9to Жыл бұрын
Hello I hava a doubt can we use inbuild function like bin class Solution: def addBinary(self, a: str, b: str) -> str: return bin(int(a, 2) + int(b, 2))[2:] the code works pretty efficiently but is it the right way?