릿코드(LEETCODE)

1252. Cells with Odd Values in a Matrix

cepiloth 2020. 3. 3. 21:10
반응형

 

https://leetcode.com/problems/cells-with-odd-values-in-a-matrix

 

Wrong Source

문제 잘못이해해서 해당 좌표 주위로 4 방향으로 채우는 문제로 착각..

class Solution {
public:
    int table[51][51];
    int oddCells(int n, int m, vector<vector<int>>& indices) {
        
        for (int i = 0; i < indices.size(); i++) {
            int y = indices[i][0];
            int x = indices[i][1];

            table[y][x]++;
            table[y][x]++;

            if (y - 1 >= 0) table[y - 1][x]++;
            if (y + 1 <= n) table[y + 1][x]++;
            if (x - 1 >= 0) table[y][x - 1]++;
            if (x + 1 <= m) table[y][x + 1]++;
        }

        int sol = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (table[i][j] & 1) sol++;
            }
        }

        return sol;
    }
};

 

Correct Source

가로로 채우고 세로로 채우고 -0- 풀어야 했던문제

 

class Solution {
public:
    int table[51][51];

    int oddCells(int n, int m, vector<vector<int>>& indices) {

        for (int i = 0; i < indices.size(); i++) {
            int y = indices[i][0];
            int x = indices[i][1];

            for (int j = 0; j < m; j++) {
                table[y][j]++;
            }

            for (int j = 0; j < n; j++) {
                table[j][x]++;
            }
        }

        int sol = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (table[i][j] & 1) sol++;
            }
        }

        return sol;
    }
};
반응형