본문 바로가기
IT/알고리즘

16] Leetcode 1252. Cells with Odd Values in a Matrix

by 깻잎쌈 2020. 6. 27.
반응형
 

Cells with Odd Values in a Matrix - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

0으로 채워져 있는 배열의 숫자들을 

indices 배열대로 증가시키고 홀수의 개수를 반환하는 문제다.

 

예시를 설명하면 n = 2, m = 3으로 2행 3열의 배열 있고

indices = [[0,1], [1,1]]으로, 첫 번째는 0행과 1열의 모든 숫자 1씩 증가시키고

두 번째는 1행과 1열의 모든 숫자를 증가시켜 [1,3,1], [1,3,1]이 된다.

 

class Solution {
public:
    int array[51][51];
    int oddCells(int n, int m, vector<vector<int>>& indices) {
        for(int i = 0;i<indices.size();i++){
            for(int j = 0;j<m;j++)
                array[indices[i][0]][j]++;
            for(int k = 0;k<n;k++)
                array[k][indices[i][1]]++;
        }
        
        int ans=0;
        for(int i = 0;i<n;i++)
            for(int j = 0;j<m;j++)
                if(array[i][j] %2 ==1)
                    ans++;
   
        return ans;
    }
    
};

그대로 해주면 된다.

행과 열 헷갈리지 않도록 주의한다.

반응형

댓글