코린이의 소소한 공부노트

[LeetCode/Easy] 1252. Cells with Odd Values in a Matrix 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 1252. Cells with Odd Values in a Matrix

무지맘 2022. 12. 24. 01:06

1. Input

1) 정수 m

2) 정수 n

3) 1 증가시킬 행과 열을 담은 2차원 배열 indices

 

2. Output

1) mn열 매트릭스의 요소가 모두 0일 때, indices에 따라 행과 열을 1씩 증가시킨 후, 그 중 홀수가 몇개인지 반환

2) indices의 요소는 [r, c]이고, r은 증가시킬 행의 번호, c는 증가시킬 열의 변호이다.

 

3. Constraint

1) 1 <= m, n <= 50

2) 1 <= indices.length <= 100

3) 0 <= r < m

4) 0 <= c < n

 

4. Example

Input: m = 2, n = 3, indices = [[0,1],[1,1]] -> Output: 6

설명:

- 홀수는 총 6개이므로 6을 반환한다.

 

5. Code

1) 첫 코드(2022/06/10)

int[][] matrix = new int[m][n];

// indice에 따라 1씩 더하기
int r=0, c=0;
for(int i=0 ; i<indices.length ; i++){
    r = indices[i][0];
    c = indices[i][1];
    for(int j=0 ; j<n ; j++)
        matrix[r][j]++;
    for(int j=0 ; j<m ; j++)
        matrix[j][c]++;
}

// 홀수 찾기
int count = 0;
for(int i=0 ; i<m ; i++)
    for(int j=0 ; j<n ; j++)
        if(matrix[i][j]%2 != 0)
            count++;

return count;