Maximum Matrix Sum | Simple Thought Process | Leetcode 1975 | codestorywithMIK

preview_player
Показать описание
This is the 126th Video of our Playlist "Array 1D/2D : Popular Interview Problems" by codestorywithMIK

In this video we will try to solve a good practice 2D Array based problem : Maximum Matrix Sum | Simple Thought Process | Leetcode 1975 | codestorywithMIK

I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.

Problem Name : Maximum Matrix Sum | Simple Thought Process | Leetcode 1975 | codestorywithMIK
Company Tags : will update

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Summary :
The approach to solve the problem involves the following steps:

Iterate Through the Matrix:
Traverse each element in the matrix, calculate the absolute value of each element, and add it to the total sum.

Count Negative Elements:
Keep track of the count of negative numbers in the matrix (countNegatives).

Track the Smallest Absolute Value:
Identify the smallest absolute value in the matrix (smallestAbsoluteValue) to handle cases where an odd number of negatives makes the maximum sum adjustment necessary.

Handle the Negativity Parity:

If the count of negative numbers is even, all elements can be made positive, so the result is simply the total sum.
If the count is odd, one element must remain negative, so subtract twice the smallest absolute value from the total sum to minimize the negative impact.
This ensures the matrix sum is maximized while adhering to the rules.

✨ Timelines✨
00:00 - Introduction

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #coding #programming #100daysofcode #developers #techjobs #datastructures #algorithms #webdevelopment #softwareengineering #computerscience #pythoncoding #codinglife #coderlife #javascript #datascience #leetcode #leetcodesolutions #leetcodedailychallenge #codinginterview #interviewprep #technicalinterview #interviewtips #interviewquestions #codingchallenges #interviewready #dsa #hindi #india #hindicoding #hindiprogramming #hindiexplanation #hindidevelopers #hinditech #hindilearning #helpajobseeker #jobseekers #jobsearchtips #careergoals #careerdevelopment #jobhunt #jobinterview #github #designthinking #learningtogether #growthmindset #digitalcontent #techcontent #socialmediagrowth #contentcreation #instagramreels #videomarketing #codestorywithmik #codestorywithmick #codestorywithmikc #codestorywitmik #codestorywthmik #codstorywithmik #codestorywihmik #codestorywithmiik #codeistorywithmik #codestorywithmk #codestorywitmick #codestorymik #codestorwithmik
Рекомендации по теме
Комментарии
Автор

It is the most underrated channel i have seen even paid courses are not as clear as this videos.

sashankreddy
Автор

Your solutions are so precise and clearly explained. Totally worth of time, unlike other youtube videos.💯💯

Supriya_Raii
Автор

class Solution {
public:
long long matrix) {
int n = matrix.size();
int smallest = INT_MAX;
long long sum = 0;
int negativeCount = 0;
for(int i=0 ; i<n ; i++)
{
for(int j=0 ; j<n ; j++)
{
if(matrix[i][j] < 0) negativeCount += 1;
smallest = min(smallest, abs(matrix[i][j]));
sum += abs(matrix[i][j]);
}
}
if(negativeCount % 2 == 0)
{
return sum;
}
else return sum - (2*smallest);
}
};

best
Автор

I figured out the trick myself today, Im soo happy..literally cured my depression.

ArjunSingh-pmvt
Автор

end sem exams are there and here i m solving daily problems
obsessed hogaya hu roz new sikhna thought process clg ki padhai touch tak nhi kari but hum engineer hai aaj rat bhar baithkr pass hona jitna likhdenge
thnku mik bhaiya for being consistently uploading daily videos bas apki wajah se coding me interest aya hai ..

jain
Автор

4th year hu apke karn DSA karne ka mann hota h thanks an lot

vedantporwal
Автор

It would be helpful if anyone would solve this problem which is a modified version of this :
So the objective of problem is to find the number of swaps to make the entire matrix positive, if not then return -1.
For the first test case that ismatrix = [[1, -1], [-1, 1]] the ans would be 2, but for the 2nd test case the ans would be -1 as it is not possible to make array positive

mr_stranger
Автор

Everything is temporary but the pressure cooker sound in the video is permanent😄

m.baliramgupta
Автор

Definitely, things take time, and I am ready to give my time.

crazygamerrohan
Автор

class Solution {
public:
long long matrix) {
int n =matrix.size();
long long positiveSum=0, negativeSum=0, cntNegatives=0, mini=INT_MAX;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if( abs(matrix[i][j]) < mini) mini = abs(matrix[i][j]);
if( matrix[i][j] >= 0 ) positiveSum+=matrix[i][j];
else {
negativeSum += matrix[i][j];
cntNegatives++;
}
}
}
if(cntNegatives%2==0)
{
return positiveSum+ abs(negativeSum);
}
else{
return positiveSum + abs(negativeSum) - 2*mini;
}
}
};

unknown
Автор

Hey i think you left out the case where, there is present at least one zero in matrix, then the pairity of the number of negatives wont matter, cuz this zero can be used as dumping ground for all negatives, so the sum will be just like even case....

varadthube
Автор

Thnx for approach man, coding it was easy. Gonna stop looking at topics when i get stuck because of that i kept thinking of greedy approaches when the simplest approach was right in front of me

Aditya-qxnf
Автор

wow the thought process blown my mind
thnku so much
aisa intutuion pata nhi kab build hoga

deeep_j
Автор

awesome explanation you are my one of the favorite youtuber thanks a lot🧡

sumithitsyt
Автор

Sometimes i can solve medium problem very easily but some time I can't intuit a easy problem😔Feels like I'm of no worth.

rickdutta
Автор

solve by own
more than 3 months I following mik it was very helpful for me thanku🤞🤞
this is my approach
class Solution {
public:
long long matrix) {
long long ans=0;
int minval=INT_MAX;
int countneg=0;
for(int i=0;i<matrix.size();i++){
for(int j=0;j<matrix[0].size();j++){
if(matrix[i][j]<0){
countneg++;
ans+=matrix[i][j]*-1;
minval=min(minval, matrix[i][j]*-1);
}
else{
ans+=matrix[i][j];
minval=min(minval, matrix[i][j]);
}
}
}
if(countneg%2==0){
return ans;
}
return ans-=minval*2;
}
};

basu
Автор

What if there is a zero in the matrix? Even if the negative number count is odd, we can still make all numbers positive. Is this actually an edge case or am I just overthinking? I wrote this solution:
class Solution {
public:
long long matrix) {
int n=matrix.size();
bool iszero=false;
int count=0;
int element_to_delete=INT_MAX;
long long ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(matrix[i][j]==0) iszero=true;
if(matrix[i][j]<0) count++;
element_to_delete=min(element_to_delete, abs(matrix[i][j]));
ans+=(long long)(abs(matrix[i][j]));
}
}
if(iszero || count%2==0) return ans;
return ans-2*(element_to_delete);
}
};

ritikgupta
Автор

class Solution {
public:
long long matrix) {
long long sum = 0;
int negCount = 0, minNum = INT_MAX, n = matrix.size();
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
sum += abs(matrix[i][j]);
if(matrix[i][j] <= 0)
negCount += 1;
minNum = min(abs(matrix[i][j]), minNum);
}
return negCount % 2 == 0 ? sum : sum - 2 * minNum;
}
};

I came up with my own solution this time and our thought process is almost the same.

rushabhlegion
Автор

Sir, , please post solutions of leetcode biweekly as well as weekly contest, ,,it would be of great use for all of us in terms of learning through those problems...

Codingstreak
Автор

when i first saw it i though greedy can't work, backtrack will take too much time, did n't knew how DP can work so i got that there would be any clever approach that i can't myself think of It happens many times with me So the main problem comes that we don't have right example to think, like you had examples diff. than what were given in leetcode testcases so how can we think of right examples which can help find the needed logic
?

ty

Diljit_dosanjh_
welcome to shbcf.ru