Score After Flipping Matrix - Leetcode 861 - Python

Поділитися
Вставка
  • Опубліковано 12 вер 2024

КОМЕНТАРІ • 28

  • @ay5960
    @ay5960 4 місяці тому +3

    There is a nuance, for the leftmost column we could either flip rows to make it all 1s, or we could flip rows to make it all 0s and then flip the first column to make it all 1s. Now the question is whether the two lead to different answers? The answer is no but this kind of elucidates that why the fact that greedy approach is working is not trivial. How do you know you get the global optimum solution rather than a local optimum one? The answer is that there is only one type of nuance and that is exactly what I mentioned above.
    Another question, what if we remove the fact that rows represent binary numbers and just simply want to maximize the number of 1s? I have no idea how to do this one.

  • @Yaqk
    @Yaqk 4 місяці тому +1

    I hate it when I get so close to the answer but miss some small step🤦‍♂ Thanks for making these videos again, it really helps to hear someone speak and go over the problem

  • @ronbuchanan5432
    @ronbuchanan5432 4 місяці тому

    I gotta say your ability to explain your thought process here was really good !

  • @MP-ny3ep
    @MP-ny3ep 4 місяці тому

    Thank you so much again. Great explanation as always.

  • @rajsh3285
    @rajsh3285 4 місяці тому

    why are we not considering the case where we flip the most significant bit column wise then the we don't have to check the most significant value while calculating the number of Zeros/ones for other columns

  • @skshaheen6309
    @skshaheen6309 4 місяці тому

    I clicked the logic at 5:56 then I paused the video and wrote code to implement the logic. And it was shocking that I perfectly wrote the correct code with 100% beats and readability. And it is recommended to all watchers that don't watch any solution video complete. You should stop watching when you click the logic and try to implement code yourself.

  • @existanz
    @existanz 4 місяці тому

    We can use xor for line 9-10 like: cnt += 1^grid[r][0]^grid[r][c]

  • @tayeblagha484
    @tayeblagha484 3 місяці тому

    thank you

  • @vinayaksuthar2472
    @vinayaksuthar2472 4 місяці тому

    "Thank you, it was helpful."

  • @connies_slice_of_life
    @connies_slice_of_life 4 місяці тому

    thanks for the awesome explanation!

  • @krateskim4169
    @krateskim4169 4 місяці тому

    Thank you for the solution

  • @EranM
    @EranM 4 місяці тому

    yo neet, use a 2^n lookup table and reverse it. That will make the index consistent instead of COL -1 - m or whatever

  • @jhumpadas147
    @jhumpadas147 4 місяці тому

    Great explanation🙏

  • @user-ce6zj1dt4s
    @user-ce6zj1dt4s 4 місяці тому

    why flip rows first not column first?

  • @ahmmedabdullahsaquif559
    @ahmmedabdullahsaquif559 4 місяці тому

    Thank you sir

  • @SanjayB-vy4gx
    @SanjayB-vy4gx 4 місяці тому

    Thanks man❤

  • @mralix9566
    @mralix9566 4 місяці тому

    The NEET Streak Saver 🔥

  • @chien-yuyeh9386
    @chien-yuyeh9386 4 місяці тому

    Nice🎉🎉

  • @priyanshagarwal8490
    @priyanshagarwal8490 3 місяці тому

    2055. Plates Between Candles... Next Please..

  • @swanv951
    @swanv951 4 місяці тому

    🙏🙏 for method 2

  • @aashishbathe
    @aashishbathe 4 місяці тому +2

    I didn't get the intuition at first, but as soon as you said first column check for 0, to flip row, I understood the problem. Anyways, here's my code -
    class Solution:
    def matrixScore(self, grid: List[List[int]]) -> int:
    ROWS, COLS = len(grid), len(grid[0])
    res = (2 ** (COLS - 1)) * ROWS
    for r in range(ROWS):
    if grid[r][0] == 0:
    for c in range(COLS):
    if grid[r][c] == 0:
    grid[r][c] = 1
    else:
    grid[r][c] = 0
    for c in range(1, COLS):
    count1 = 0
    for r in range(ROWS):
    if grid[r][c] == 1:
    count1 += 1
    if count1 > (ROWS // 2):
    res += count1 * (2 ** (COLS - c - 1))
    else:
    res += (ROWS - count1) * (2 ** (COLS - c - 1))
    return res

    • @aashishbathe
      @aashishbathe 4 місяці тому

      This is basically the same way I thought of it, but then damn your use of ternary conditions makes some lines of code so much shorter!

    • @sathishn6708
      @sathishn6708 4 місяці тому

      you can use grid[r][c] ^ 1 one line to flip

    • @aashishbathe
      @aashishbathe 4 місяці тому

      @@sathishn6708 yeah that also works!

  • @pastori2672
    @pastori2672 4 місяці тому

    wow brain go brrr

  • @naveenkumarreddybadduri7775
    @naveenkumarreddybadduri7775 4 місяці тому +1

    Neet you are natu natu 😂

  • @priyanshagarwal8490
    @priyanshagarwal8490 3 місяці тому

    2055. Plates Between Candles... Next Please..