-
Notifications
You must be signed in to change notification settings - Fork 382
Description
LeetCode Username
Peace , id : ecaep
Problem Number, Title, and Link
- Set Matriz Zeroes, https://leetcode.com/problems/set-matrix-zeroes/
Bug Category
Missing test case (Incorrect/Inefficient Code getting accepted because of missing test cases)
Bug Description
A two-pass, in place brute force solution for the "Set Matrix Zeroes" problem that uses a marker value contains a critical flaw. The approach, which marks rows and columns for zeroing by changing elements to a specific number like -214748 and then converting these markers to zero in a second pass, incorrectly assumes this marker value will not be present in the original input. This bug can be reproduced with a custom test case where the input matrix itself contains the marker value, for example, matrix = [[-214748], [2], [3]]. With this input, the algorithm incorrectly changes the original -214748 to 0 during its final pass, leading to an erroneous output of [[0], [2], [3]] when the expected output should be the unchanged matrix.
Language Used for Code
C++
Code used for Submit/Run operation
class Solution {
public:
void setRow(vector<vector<int>>&matrix, int i){
int n = matrix[0].size();
for(int k=0;k<n;k++){
if(matrix[i][k]!=0){
matrix[i][k]=-214748;
}
}
}
void setCol(vector<vector<int>>&matrix, int j){
int m = matrix.size();
for(int k=0;k<m;k++){
if(matrix[k][j]!=0){
matrix[k][j]=-214748;
}
}
}
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
for(int i=0; i<m;i++){
for(int j=0; j<n;j++){
if(matrix[i][j]==0){
setRow(matrix,i);
setCol(matrix,j);
}
}
}
for(int i=0; i<m;i++){
for(int j=0; j<n;j++){
if(matrix[i][j]==-214748){
matrix[i][j]=0;
}
}
}
for(int i=0; i<m;i++){
for(int j=0; j<n;j++){
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
}
};Expected behavior
Although my solution passes all 202 official test cases, it fails on a specific custom test case. For the input [[-214748], [2], [3]], my code incorrectly produces [[0], [2], [3]]. The expected output is [[-214748], [2], [3]], as the matrix should remain unmodified since it contains no zeros.
Screenshots
Additional context
No response