package com.算法专练.力扣.奇数值单元格的数目;
/**
* @author xnl
* @Description:
* @date: 2022/7/12 21:36
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
int[][] indices = {{0,1},{1,1}};
System.out.println(solution.oddCells(2, 3, indices));
}
/**
* 暴力解法
* @param m
* @param n
* @param indices
* @return
*/
public int oddCells2(int m, int n, int[][] indices) {
int[][] dp = new int[m][n];
int x = indices.length;
for (int i = 0; i < x; i++){
int x1 = 0, y1 = 0;
int x2 = indices[i][0];
int y2 = indices[i][1];
while (x1 < n){
dp[x2][x1]++;
x1++;
}
while (y1 < m){
dp[y1][y2]++;
y1++;
}
}
int res = 0;
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
if (dp[i][j] % 2 != 0){
res++;
}
}
}
return res;
}
/**
* 数组优化
*
* @param m
* @param n
* @param indices
* @return
*/
public int oddCells(int m, int n, int[][] indices) {
int[] row = new int[m];
int[] col = new int[n];
int res = 0;
for (int[] index : indices) {
row[index[0]]++;
col[index[1]]++;
}
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
if (((row[i] + col[j]) & 1) != 0){
res++;
}
}
}
return res;
}
}