-
1252. Cells with Odd Values in a Matrix릿코드(LEETCODE) 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; } };
반응형'릿코드(LEETCODE)' 카테고리의 다른 글
202. Happy Number (0) 2020.04.09 804. Unique Morse Code Words (0) 2020.03.05 1207. Unique Number of Occurrences (0) 2020.03.03 1365. How Many Numbers Are Smaller Than the Current Number (0) 2020.03.03 1366. Rank Teams by Votes (0) 2020.03.03