1.문제
간단히 해석하면 ri, ci에 해당하는 행, 열 모두 1씩 증가 시킨다. return 값은 1씩 모두 증가시키고 난 뒤 행렬 안에 odd value counting 값 이다.
Example 1
Input: m = 2, n = 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 is [[1,3,1],[1,3,1]], which contains 6 odd numbers.
Example 2
Input: m = 2, n = 2, indices = [[1,1],[0,0]]
Output: 0
Explanation: Final matrix = [[2,2],[2,2]]. There are no odd numbers in the final matrix.
Constraints:
- 1 <= m, n <= 50
- 1 <= indices.length <= 100
- 0 <= ri < m
- 0 <= ci < n
2.풀이
- m X n 사이즈의 행렬이므로 m 사이즈의 열 배열 하나 , n 사이즈의 행 배열 하나를 만든다.
- indices 배열에서 각 행과 열에 1씩 더해준다.
- 행렬의 곱셈은 각 요소들의 합이므로 행과 열 배열의 각 요소들을 더한 값을 가지고 홀수 판별을 해준다.
/**
* @param {number} m
* @param {number} n
* @param {number[][]} indices
* @return {number}
*/
const oddCells = function (m, n, indices) {
let count = 0;
let row = new Array(m).fill(0); //하나의 행
let column = new Array(n).fill(0); //하나의 열
for (let i = 0; i < indices.length; i++) {
row[indices[i][0]]++; // 각 행의 요소들에 1씩 증가
column[indices[i][1]]++; // 각 열의 요소들에 1씩 증가
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if ((row[i] + column[j]) % 2 !== 0) { //행 + 열 의 값이 홀수이면 count 1 증가
count++;
}
}
}
return count;
};
3.결과