Big fan too, everyday after solving the daily problem, I like to see how neetcode has written the code for it, and hopefully today I can say I have written a better solution, so far it has never happened.
It's not just you. The solutions to both of the last two problems were really easy to understand. This one would make a great demonstration of the "Sliding window" algorithm!
class Solution: def maxScore(self, s: str) -> int: m, l = [0] * 2, [0] * 2 res = 0 for i in s: m[int(i)]+=1 for i in s[:-1]: ch = int(i) l[int(i)]+=1 m[int(i)]-=1 res = max(res, l[0]+m[1]) return res my python solution with linear time and constant space.
Couldn't you just precompute the count, and then as you iterate the string (index inclusive left), you decrease the count when you hit a 1, and increase the count when you hit a 0? for (var i=0; i < s.length; i++) { if (s[i] === "1") count++ } for (var i = 0; i < s.length-1; i++) { // i includes left if (s[i] === "1") count--; else count++; res = Math.max(count, res); } return res;
I came here to understand the leetcode 0 ms solution. And this dude is doing just like me. Am i getting better or this guy is getting bored with making much optimized solution in videos ??
0 ms one ? is it the single pass ? I think it comes from the equation : Score = zeros + ones - onesSeen And if you balance it out without the ones, score - ones = zeros - onesSeen, then you add the ones at the end to have the bestScore, since now you have the missing information. How did he come up with it tho.....
Not really, since rather than counting the number ones initially, we can build up while we traverse. That solution felt a little bit complex and overwhelming to understand by own. Then came here and this guys also does the way I did. I mean this solution is completely fine and it's good for an interview. I just wanted to understand the intuition behind the single pass. Then sat with a pen and paper drawing tables and understood that.
I was asking if the 0ms one is the single pass one, and that's what i was explaining, why it does the score = zero - one at some point. Or score = max(zero - one, score) What did you understood in the end ? What is the that in understood that.
Is it just me or have the problems been suspiciously easy this month?
Was wondering that too 😂 big fan btw you've played a big role in bringing me from a 2xer to like a 5xer
I feel this was the right level for an Easy problem, but yesterday's problem was far too easy for Medium
Big fan too, everyday after solving the daily problem, I like to see how neetcode has written the code for it, and hopefully today I can say I have written a better solution, so far it has never happened.
Also in previous month too, there were many simple medium level problem.
It's not just you. The solutions to both of the last two problems were really easy to understand. This one would make a great demonstration of the "Sliding window" algorithm!
Awesome solution
What about the single pass O(n) soln?
Can you explain why we are iterating till 2nd last index not till last index? pls
@@navoberoi if we iterate to the last index then we are not dividing the list in half, that’ll just be the entire list
class Solution:
def maxScore(self, s: str) -> int:
m, l = [0] * 2, [0] * 2
res = 0
for i in s:
m[int(i)]+=1
for i in s[:-1]:
ch = int(i)
l[int(i)]+=1
m[int(i)]-=1
res = max(res, l[0]+m[1])
return res
my python solution with linear time and constant space.
Couldn't you just precompute the count, and then as you iterate the string (index inclusive left), you decrease the count when you hit a 1, and increase the count when you hit a 0?
for (var i=0; i < s.length; i++) {
if (s[i] === "1") count++
}
for (var i = 0; i < s.length-1; i++) { // i includes left
if (s[i] === "1") count--;
else count++;
res = Math.max(count, res);
}
return res;
This was a tricky problem. Thank you for explaining it so beautifully.
Please creare a list for the new Grind 75 problems.
public int maxScore (String s){
int ones=0,zeros =0,ans=Integer.MIN_VALUE;
for(int i=0;i
I came here to understand the leetcode 0 ms solution. And this dude is doing just like me.
Am i getting better or this guy is getting bored with making much optimized solution in videos ??
0 ms one ? is it the single pass ?
I think it comes from the equation : Score = zeros + ones - onesSeen
And if you balance it out without the ones, score - ones = zeros - onesSeen, then you add the ones at the end to have the bestScore, since now you have the missing information. How did he come up with it tho.....
Not really, since rather than counting the number ones initially, we can build up while we traverse.
That solution felt a little bit complex and overwhelming to understand by own. Then came here and this guys also does the way I did.
I mean this solution is completely fine and it's good for an interview. I just wanted to understand the intuition behind the single pass.
Then sat with a pen and paper drawing tables and understood that.
I was asking if the 0ms one is the single pass one, and that's what i was explaining, why it does the
score = zero - one at some point. Or score = max(zero - one, score)
What did you understood in the end ? What is the that in understood that.