Score After Flipping Matrix - Leetcode 861 - Python

  Рет қаралды 10,102

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 27
@ay5960
@ay5960 6 ай бұрын
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 6 ай бұрын
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 6 ай бұрын
I gotta say your ability to explain your thought process here was really good !
@skshaheen6309
@skshaheen6309 6 ай бұрын
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.
@MP-ny3ep
@MP-ny3ep 6 ай бұрын
Thank you so much again. Great explanation as always.
@existanz
@existanz 6 ай бұрын
We can use xor for line 9-10 like: cnt += 1^grid[r][0]^grid[r][c]
@vinayaksuthar2472
@vinayaksuthar2472 6 ай бұрын
"Thank you, it was helpful."
@EranM
@EranM 6 ай бұрын
yo neet, use a 2^n lookup table and reverse it. That will make the index consistent instead of COL -1 - m or whatever
@connies_slice_of_life
@connies_slice_of_life 6 ай бұрын
thanks for the awesome explanation!
@mralix9566
@mralix9566 6 ай бұрын
The NEET Streak Saver 🔥
@krateskim4169
@krateskim4169 6 ай бұрын
Thank you for the solution
@rajsh3285
@rajsh3285 6 ай бұрын
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
@jhumpadas147
@jhumpadas147 6 ай бұрын
Great explanation🙏
@ahmmedabdullahsaquif559
@ahmmedabdullahsaquif559 6 ай бұрын
Thank you sir
@priyanshagarwal8490
@priyanshagarwal8490 6 ай бұрын
2055. Plates Between Candles... Next Please..
@SanjayB-vy4gx
@SanjayB-vy4gx 6 ай бұрын
Thanks man❤
@MegaZz-j9c
@MegaZz-j9c 6 ай бұрын
why flip rows first not column first?
@chien-yuyeh9386
@chien-yuyeh9386 6 ай бұрын
Nice🎉🎉
@swanv951
@swanv951 6 ай бұрын
🙏🙏 for method 2
@aashishbathe
@aashishbathe 6 ай бұрын
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 6 ай бұрын
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 6 ай бұрын
you can use grid[r][c] ^ 1 one line to flip
@aashishbathe
@aashishbathe 6 ай бұрын
@@sathishn6708 yeah that also works!
@pastori2672
@pastori2672 6 ай бұрын
wow brain go brrr
@naveenkumarreddybadduri7775
@naveenkumarreddybadduri7775 6 ай бұрын
Neet you are natu natu 😂
@priyanshagarwal8490
@priyanshagarwal8490 6 ай бұрын
2055. Plates Between Candles... Next Please..
Path with Maximum Gold - Leetcode 1219 - Python
14:53
NeetCodeIO
Рет қаралды 9 М.
Lucky Numbers in a Matrix - Leetcode 1380 - Python
16:21
NeetCodeIO
Рет қаралды 8 М.
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 21 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 8 МЛН
Задание 13.2 с нуля - ОГЭ по информатике 2025
1:04:45
Информатика. Global_EE. Владимир Ульшин
Рет қаралды 25
Distribute Coins in Binary Tree - Leetcode 979 - Python
17:41
NeetCodeIO
Рет қаралды 17 М.
ZIG | Ep1 | Introduction
20:18
Code Guild
Рет қаралды 10 М.
Smallest String Starting From Leaf - Leetcode 988 - Python
9:23
Rotating the Box - Leetcode 1861 - Python
15:14
NeetCodeIO
Рет қаралды 6 М.
Flipping the Matrix | HackerRank | Solution Explained - Python
11:38
Tech and Navid
Рет қаралды 34 М.
Minimum Cost to Hire K Workers - Leetcode 857 - Python
19:01
NeetCodeIO
Рет қаралды 14 М.
Largest Submatrix With Rearrangements - Leetcode 1727 - Python
16:30
Student Attendance Record II - Leetcode 552 - Python
27:10
NeetCodeIO
Рет қаралды 9 М.
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 21 МЛН