Abhishek Kumar Singh’s Post

View profile for Abhishek Kumar Singh, graphic

CodeJam || KickStart || Facebook-HackerCup || Full-Stack Developer

Reverse revision of Blind 75 🚀 Just cracked the problem 60 of LeetCode's Blind 75 series! 🎉 🔍 Problem: Set Matrix Zeroes 🔗 Problem link: https://lnkd.in/d72YF3r8 💡 Solution: 🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴 class Solution { public:     void setZeroes(vector<vector<int>>& matrix) {         //brute force         int m=matrix.size(),n=matrix[0].size();         vector<vector<int>>zeros(m+1,vector<int>(n+1,-1));         for(int i=0;i<m;i++)         {             for(int j=0;j<n;j++)             {                 if(matrix[i][j]==0)                 {                     int new_i=0,new_j=0;                     while(new_i<m) zeros[new_i++][j]=0;                     while(new_j<n) zeros[i][new_j++]=0;                 }             }         }         for(int i=0;i<m;i++)         {             for(int j=0;j<n;j++)             {                 if(zeros[i][j]==-1)                 {                     zeros[i][j]=matrix[i][j];                 }             }         }         for(int i=0;i<m;i++)         {             for(int j=0;j<n;j++)             {                 matrix[i][j]=zeros[i][j];             }         }         //better approach         int m=matrix.size(),n=matrix[0].size();         vector<int>rows(m+1,0),columns(n+1,0);         for(int i=0;i<m;i++)         {             for(int j=0;j<n;j++)             {                 if(matrix[i][j]==0)                 {                     rows[i]=1;                     columns[j]=1;                 }             }         }         for(int i=0;i<m;i++)         {             for(int j=0;j<n;j++)             {                 if(rows[i]==1||columns[j]==1)                 {                     matrix[i][j]=0;                 }             }         }         //optimal approach         int m=matrix.size(),n=matrix[0].size();         int row0=0,col0=0;         for(int i=0;i<m;i++)         {             for(int j=0;j<n;j++)             {                 if(i==0 && matrix[i][j]==0) row0=1;                 if(j==0 && matrix[i][j]==0) col0=1;             }         }         for(int i=1;i<m;i++)         {             for(int j=1;j<n;j++)             {                 if(matrix[i][j]==0)                 {                     matrix[0][j]=0;                     matrix[i][0]=0;                 }             }         }         for(int i=1;i<m;i++)         {             for(int j=1;j<n;j++)             {                 if(matrix[i][0]==0 || matrix[0][j]==0)                 {                     matrix[i][j]=0;                 }             }         }         for(int i=0;i<n;i++)             if(row0==1)                 matrix[0][i]=0;         for(int i=0;i<m;i++)             if(col0==1)                 matrix[i][0]=0;     } }; 🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴 Excited to dive deeper into the Blind 75 series and tackle more challenges! 💪 #LeetCode #Blind75 #CodingJourney #Programming #CPP

  • text

To view or add a comment, sign in

Explore topics