第一题:
1252. Cells with Odd Values in a Matrix
Given n and m which are the dimensions of a matrix initialized by zeros and given an array indices where indices[i] = [ri, ci]. For each pair of [ri, ci] you have to increment all cells in row ri and column ci by 1.
Return the number of cells with odd values in the matrix after applying the increment to all indices.
Example 1:
Input: n = 2, m = 3, indices = [[0,1],[1,1]]
Output: 6
Explanation: Initial matrix = [[0,0,0],[0,0,0]].
After applying first increment it becomes [[1,2,1],[0,1,0]].
The final matrix will be [[1,3,1],[1,3,1]] which contains 6 odd numbers.
Example 2:
Input: n = 2, m = 2, indices = [[1,1],[0,0]]
Output: 0
Explanation: Final matrix = [[2,2],[2,2]]. There is no odd number in the final matrix.
Constraints:
1 <= n <= 50
1 <= m <= 50
1 <= indices.length <= 100
0 <= indices[i][0] < n
0 <= indices[i][1] < m
code:
class Solution {
public:
int oddCells(int n, int m, vector<vector<int>>& indices) {
vector<vector<bool>> matrix(n, vector<bool>(m,false));
for(int i = 0; i < indices.size(); i ++)
{
if(indices[i][0]>=0 &&indices[i][0]<= n)
{
for(int j = 0;j < m; j++)
{
matrix[indices[i][0]][j] = !matrix[indices[i][0]][j];
}
}
if(indices[i][1]>=0 &&indices[i][1]<= m)
{
for(int j = 0; j < n; j++)
{
matrix[j][indices[i][1]] = !matrix[j][indices[i][1]];
}
}
}
int sum_of_odd = 0;
for(int i = 0; i < matrix.size(); i ++)
{
for(int j = 0; j <matrix[i].size(); j++)
{
if(matrix[i][j] == true)
{
sum_of_odd +=1;
}
}
}
return sum_of_odd;
}
};
comment:
The reason I use bool vector is each time of increment is by 1. And from 0->1 is even->odd. 1->2 odd->even. 2->3 even->odd....so let bool value false represents even when true does odd.
Another solution:
class Solution {
public:
int oddCells(int n, int m, vector<vector<int>>& indices)
{
vector<bool> row(n,false) , col(m,false); //false is even, true is odd
int odd_row_numbers = 0, odd_col_numbers = 0;
for(auto &it : indices)
{
row[it[0]] = !row[it[0]];
col[it[1]] = !col[it[1]];
//for example, when row[1] change from 2->3, the odd_row_numbers's incresed by 1
if(row[it[0]])
odd_row_numbers ++;
else
odd_row_numbers --;
if(col[it[1]])
odd_col_numbers ++;
else
odd_col_numbers --;
}
/*odd_row_numbers * m + odd_col_numbers * n adds all odd number either in rows or columns
2 * odd_row_numbers * odd_col_numbers removes the overlaps which are even numbers */
int res = odd_row_numbers * m + odd_col_numbers * n - 2 * odd_row_numbers * odd_col_numbers;
return res;
}
};
comment:
This solution is 4ms faster than the last answer(which is 8ms). My annotations are clear enuf.
第二题:
1253. Reconstruct a 2-Row Binary Matrix
Given the following details of a matrix with n columns and 2 rows :
The matrix is a binary matrix, which means each element in the matrix can be 0 or 1.
The sum of elements of the 0-th(upper) row is given as upper.
The sum of elements of the 1-st(lower) row is given as lower.
The sum of elements in the i-th column(0-indexed) is colsum[i], where colsum is given as an integer array with length n.
Your task is to reconstruct the matrix with upper, lower and colsum.
Return it as a 2-D integer array.
If there are more than one valid solution, any of them will be accepted.
If no valid solution exists, return an empty 2-D array.
Example 1:
Input: upper = 2, lower = 1, colsum = [1,1,1]
Output: [[1,1,0],[0,0,1]]
Explanation: [[1,0,1],[0,1,0]], and [[0,1,1],[1,0,0]] are also correct answers.
Example 2:
Input: upper = 2, lower = 3, colsum = [2,2,1,1]
Output: []
Example 3:
Input: upper = 5, lower = 5, colsum = [2,1,2,0,1,0,1,2,0,1]
Output: [[1,1,1,0,1,0,0,1,0,0],[1,0,1,0,0,0,1,1,0,1]]
Constraints:
1 <= colsum.length <= 10^5
0 <= upper, lower <= colsum.length
0 <= colsum[i] <= 2
code:
class Solution {
public:
vector<vector<int>> reconstructMatrix(int upper, int lower, vector<int>& colsum) {
vector<vector<int>> res(2, vector<int>(colsum.size(),0));
for(int i = 0; i < colsum.size(); i ++)
{
if(colsum[i] == 0)
{
res[0][i] = 0;
res[1][i] = 0;
}else if(colsum[i] == 2)
{
res[0][i] = 1;
res[1][i] = 1;
upper --;
lower --;
}else if(colsum[i] == 1)
{
if(upper>=lower)
{
res[0][i] = 1;
res[1][i] = 0;
upper --;
}else{
res[0][i] = 0;
res[1][i] = 1;
lower --;
}
}
}
if(upper!=0||lower!=0)
{
vector<vector<int>> res_null;
return res_null;
}
return res;
}
};
comment:
I should've solved this question. I mean, I solved it in algorithm, but didn't pass the compiler cuz I didn't initialize the vector correctly(during the time in competition I did "vector<vector<int>> res(0, vector());" instead of the correct initialization "vector<vector<int>> res(2, vector<int>(colsum.size(),0));" ).
Nothing else to be commented, this is an easy one.