Maximum Score After Splitting a String - Leetcode 1422 - Python

Sdílet
Vložit
  • čas přidán 23. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
    🥷 Discord: / discord
    🐦 Twitter: / neetcode1
    🐮 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    Problem Link:leetcode.com/problems/maximum...
    0:00 - Read the problem
    0:04 - Drawing Explanation
    4:41 - Coding Explanation
    leetcode 1422
    #neetcode #leetcode #python

Komentáře • 26

  • @krateskim4169
    @krateskim4169 Před 7 měsíci +1

    Awesome solution

  • @MP-ny3ep
    @MP-ny3ep Před 7 měsíci +1

    This was a tricky problem. Thank you for explaining it so beautifully.

  • @NeetCodeIO
    @NeetCodeIO  Před 7 měsíci +21

    Is it just me or have the problems been suspiciously easy this month?

    • @Nvidialimey
      @Nvidialimey Před 7 měsíci +2

      Was wondering that too 😂 big fan btw you've played a big role in bringing me from a 2xer to like a 5xer

    • @akshayiithyd
      @akshayiithyd Před 7 měsíci +1

      I feel this was the right level for an Easy problem, but yesterday's problem was far too easy for Medium

    • @akshayiithyd
      @akshayiithyd Před 7 měsíci +1

      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.

    • @moviesinthelist6551
      @moviesinthelist6551 Před 7 měsíci

      Also in previous month too, there were many simple medium level problem.

    • @JLSXMK8
      @JLSXMK8 Před 7 měsíci

      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!

  • @Espectro4444
    @Espectro4444 Před 7 měsíci +2

    What about the single pass O(n) soln?

    • @navoberoi
      @navoberoi Před 7 měsíci

      Can you explain why we are iterating till 2nd last index not till last index? pls

    • @Espectro4444
      @Espectro4444 Před 7 měsíci +2

      @@navoberoi if we iterate to the last index then we are not dividing the list in half, that’ll just be the entire list

  • @shubhsinha3896
    @shubhsinha3896 Před 7 měsíci

    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.

  • @ianokay
    @ianokay Před 4 měsíci

    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;

  • @anthonya880
    @anthonya880 Před 7 měsíci

    Please creare a list for the new Grind 75 problems.

  • @ViCkY-ft3zt
    @ViCkY-ft3zt Před 7 měsíci +1

    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 ??

    • @sdemji
      @sdemji Před 7 měsíci

      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.....

    • @ViCkY-ft3zt
      @ViCkY-ft3zt Před 7 měsíci +1

      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.

    • @sdemji
      @sdemji Před 7 měsíci

      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.

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Před 7 měsíci

    public int maxScore (String s){
    int ones=0,zeros =0,ans=Integer.MIN_VALUE;
    for(int i=0;i