-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetMatrixZero.cpp
176 lines (164 loc) · 4.42 KB
/
setMatrixZero.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include<bits/stdc++.h>
using namespace std;
// class Solution
// {
// public:
// vector<vector <int>> markRow(int i, vector<vector<int>> v)
// {
// int m = v[0].size();
// for(int j = 0; j< m; j++)
// {
// if(v[i][j] != 0)
// {
// v[i][j] = -1;
// }
// }
// return v;
// }
// vector<vector <int>> markColumn(int j, vector<vector <int>> v)
// {
// int n = v.size();
// for(int i = 0; i< n; i++)
// {
// if(v[i][j] != 0)
// {
// v[i][j] = -1;
// }
// }
// return v;
// }
// vector < vector <int> > setZeroToMatrix(vector < vector <int> > arr)
// {
// int n = arr.size();
// int m = arr[0].size();
// for(int i = 0; i< n; i++)
// {
// for(int j = 0; j< m; j++)
// {
// if(arr[i][j] == 0)
// {
// arr = markRow(i, arr);
// arr = markColumn(j, arr);
// }
// }
// }
// for(int i = 0; i< n; i++)
// {
// for(int j =0; j< m ;j++)
// {
// if(arr[i][j] == -1)
// {
// arr[i][j] = 0;
// }
// }
// }
// return arr;
// }
// };
// this is the brute force solution. whose time complexity is o(n*m)+o(n)+o(m) +o(n*m)
// class Solution
// {
// public:
// vector < vector <int> > setZeroToMatrix(vector < vector <int> > arr)
// {
// int n = arr.size();
// int m = arr[0].size();
// vector <int> row, column;
// row.resize(n,0);
// column.resize(n,0);
// // this line means the size of the column is n and it containe all 0s.
// for(int i =0; i< n; i++)
// {
// for(int j = 0; j< m; j++)
// {
// if(arr[i][j] == 0)
// {
// row[i] = 1;
// column[j] = 1;
// }
// }
// }
// for(int i = 0; i< n; i++)
// {
// for(int j = 0; j< m; j++)
// {
// if(row[i] || column[j])
// {
// arr[i][j] = 0;
// }
// }
// }
// return arr;
// }
// };
// the time complexity of this code is o(2*n*m) and the space complexity is o(n)+o(m)
class Solution
{
public:
vector < vector <int> > setZeroToMatrix(vector < vector <int> > arr)
{
int n = arr.size();
int m = arr[0].size();
int col0 = 1;
for(int i = 0; i< n; i++)
{
for(int j = 0; j< m; j++)
{
if(arr[i][j] == 0)
{
arr[i][0] = 0;
if(j != 0)
{
arr[0][j] = 0;
}
else{
col0 = 0;
}
}
}
}
for(int i = 1; i< n; i++)
{
for(int j = 1; j< m; j++)
{
if(arr[i][j] != 0)
{
if(arr[i][0] == 0 || arr[0][j] == 0)
{
arr[i][j] = 0;
}
}
}
}
if(arr[0][0] == 0)
{
for(int i = 0; i< m; i++)
arr[0][i] = 0;
}
if(col0 == 0)
{
for(int j = 0; j< n; j++)
arr[j][0] = 0;
}
return arr;
}
};
// this is the optimal solution whose time complexity is o(n*m)+o(n*m) = o(2*n*m) and space complexity is o(1).
int main()
{
// vector <vector <int> > v = {{1, 1, 1, 1}, {1, 0, 0, 1}, {1,1,0, 1}, {1,1,1,1}};
vector <vector <int> > v = {{1, 1, 1, 1}, {1, 0, 1, 1}, {1,1,0, 1}, {0,1,1,1}};
Solution s;
auto a = s.setZeroToMatrix(v);
int n = a.size();
int m = a[0].size();
for(int i = 0; i< n; i++)
{
for(int j = 0; j< m; j++)
{
cout<< a[i][j] << " ";
}
cout<<endl;
}
return 0;
}