it's better to use array instead of a string at the end uou join the res array with an empty string, you know each time you append a new letter to the res string you are actually creating a new one that has a negative impact on your time complexity.
@@CosmicRays123 On space No because we will end up with the same result, but in term of time yes because strings are immutable so each time you are trying to add a new letter to the string the compiler create another string so he should iterate over the whole string and add to it the new letter, but with the array approach he just append the new letter to the end of the array ! I hope I could explain it well to you !
interestingly, I tried updating my code to make the result a list and it didn't affect the runtime much. Not sure how accurate leetcode is in this regard. Memory usage however was much higher. I wonder if it's something to do with how python implements strings
I came up with this solution except for the edge case where one of the inputs is zero. Thank you so much for your videos. You're the reason for most of the coding skills I have acquired.
I am super excited I literally solved this problem myself but the code is identical! all the same except I have 3 if statements at the beginning instead of putting 3 input arguments into a list and then loop over the list. I feel super motivated, @NeetCode thank you very much! You can't believe how the community is thankful!
Just wondering, for a variable input scenario where I needed to use a maxHeap and I am using Javascript as my coding language. Not sure if telling the interviewer that I intend to use a heap suffice considering I will not have enough time to implement a heap from scratch.
@@rishabhhsingh7762 That is not actually why he is asking that. I am wondering too, because of that heap push at the start, it uses O(nlogn) instead of O(n) to build the heap. The first init should be using heapify. However, since in the worst case he is going to pop everything from the heap, it would be O(nlogn) anyways.
Hi, great explanation and great video. Just want to point out that string is immutable in Python, so better start the res with a list, and “”.join(res) before return.
The string concatenation works perfectly fine. No need to have list. Strings are immutable, but string concatenation will assign new memory to existing string.
To be honest your solutions are usually pretty unreadable, I think readability should be prioritized over conciseness or the usage of "tricks" but that might just be me
this pb really reminds me of that quote from steve jobs "Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple.", and yep using sorting instead of heaps works just fine cause the size is just 3, i get a faster than 95% with this code : def longestDiverseString(self, a: int, b: int, c: int) -> str: mapping = [["a", a],["b", b],["c", c],] z = a+b+c+1 res = [] while z>0: mapping.sort(key=lambda x : x[1], reverse=True) t0,c0 = mapping[0] t1,c1 = mapping[1] if c0 and res[-2:]!=[t0,t0]: res.append(t0) mapping[0][1]-=1 elif c1: res.append(t1) mapping[1][1]-=1 z-=1 return "".join(res)
Thank you! I am watching at least 5 videos from you a day they’re awesome!
3:03 I was over complicating this problem the whole day. Thank you Neetcode.
I don't know how you do it man
Every single video of yours is explained so clearly and makes it so easy to understand.
Thanks for making these :)
never knew how to use heapq in the first place. Thank you
it's better to use array instead of a string at the end uou join the res array with an empty string, you know each time you append a new letter to the res string you are actually creating a new one that has a negative impact on your time complexity.
Yeah better to use a list as a stringbuilder.
Good point, i've used that pattern in previous problems but it slipped my mind.
@Ayoub @neetcode Does it impact on space complexity or time complexity?
@@CosmicRays123 On space No because we will end up with the same result, but in term of time yes because strings are immutable so each time you are trying to add a new letter to the string the compiler create another string so he should iterate over the whole string and add to it the new letter, but with the array approach he just append the new letter to the end of the array !
I hope I could explain it well to you !
interestingly, I tried updating my code to make the result a list and it didn't affect the runtime much. Not sure how accurate leetcode is in this regard. Memory usage however was much higher. I wonder if it's something to do with how python implements strings
I came up with this solution except for the edge case where one of the inputs is zero. Thank you so much for your videos. You're the reason for most of the coding skills I have acquired.
This is the exact prob I wanted you to do THANK YOU
You are simply the BEST! Thanks a ton!
Great video! Would you consider covering #68: Text Justification?
Ditto
I am super excited I literally solved this problem myself but the code is identical! all the same except I have 3 if statements at the beginning instead of putting 3 input arguments into a list and then loop over the list. I feel super motivated, @NeetCode thank you very much! You can't believe how the community is thankful!
hello from october challenge
Just wondering, for a variable input scenario where I needed to use a maxHeap and I am using Javascript as my coding language. Not sure if telling the interviewer that I intend to use a heap suffice considering I will not have enough time to implement a heap from scratch.
Thank you so much! Amazing content!
Please explain this problem : 1799. Maximize Score After N Operations in your next video.
Appreciated bro✅✅✅✅
I swear you will receive such a generous donation once its all said and done
Can u explain for this a=0 b=8 c=11?I tried in java !
Hi, Can you please solve "Find Closest Palindrome" I am struggling with that question for a long time.
Why don't we heapify?
heappush pushes the element to the heap and then heapifies. Without heapification, inserting an element to the heap is of no use.
@@rishabhhsingh7762 That is not actually why he is asking that. I am wondering too, because of that heap push at the start, it uses O(nlogn) instead of O(n) to build the heap. The first init should be using heapify. However, since in the worst case he is going to pop everything from the heap, it would be O(nlogn) anyways.
@@orellavie6233 bruv n is 3 calm down
Hi, great explanation and great video. Just want to point out that string is immutable in Python, so better start the res with a list, and “”.join(res) before return.
The string concatenation works perfectly fine. No need to have list. Strings are immutable, but string concatenation will assign new memory to existing string.
why don't we heapify
Itadakimassu
To be honest your solutions are usually pretty unreadable, I think readability should be prioritized over conciseness or the usage of "tricks" but that might just be me
this pb really reminds me of that quote from steve jobs "Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple.", and yep using sorting instead of heaps works just fine cause the size is just 3, i get a faster than 95% with this code :
def longestDiverseString(self, a: int, b: int, c: int) -> str:
mapping = [["a", a],["b", b],["c", c],]
z = a+b+c+1
res = []
while z>0:
mapping.sort(key=lambda x : x[1], reverse=True)
t0,c0 = mapping[0]
t1,c1 = mapping[1]
if c0 and res[-2:]!=[t0,t0]:
res.append(t0)
mapping[0][1]-=1
elif c1:
res.append(t1)
mapping[1][1]-=1
z-=1
return "".join(res)