/* * @lc app=leetcode id=1252 lang=rust * * [1252] Cells with Odd Values in a Matrix * * https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/description/ * * algorithms * Easy (77.95%) * Total Accepted: 25.3K * Total Submissions: 32.5K * Testcase Example: '2\n3\n[[0,1],[1,1]]' * * Given n and m which are the dimensions of a matrix initialized by zeros and * given an array indices where indices[i] = [ri, ci]. For each pair of [ri, * ci] you have to increment all cells in row ri and column ci by 1. * * Return the number of cells with odd values in the matrix after applying the * increment to all indices. * * * Example 1: * * * Input: n = 2, m = 3, indices = [[0,1],[1,1]] * Output: 6 * Explanation: Initial matrix = [[0,0,0],[0,0,0]]. * After applying first increment it becomes [[1,2,1],[0,1,0]]. * The final matrix will be [[1,3,1],[1,3,1]] which contains 6 odd numbers. * * * Example 2: * * * Input: n = 2, m = 2, indices = [[1,1],[0,0]] * Output: 0 * Explanation: Final matrix = [[2,2],[2,2]]. There is no odd number in the * final matrix. * * * * Constraints: * * * 1 <= n <= 50 * 1 <= m <= 50 * 1 <= indices.length <= 100 * 0 <= indices[i][0] < n * 0 <= indices[i][1] < m * * */ impl Solution { pub fn odd_cells(n: i32, m: i32, indices: Vec>) -> i32 { let (mut row, mut col) = (vec![false; n as usize], vec![false; m as usize]); let (mut r, mut c) = (0, 0); for p in &indices{ let (x, y) = (p[0] as usize, p[1] as usize); row[x] ^= true; col[y] ^= true; r += if row[x] { 1 } else { -1 }; c += if col[y] { 1 } else { -1 }; } r * m + c * n - 2 * r * c } } // pub structSolution; use std::collections::HashMap; use std::collections::HashSet; use std::fmt::Debug; use std::hash::Hash; use std::iter::FromIterator; // use std::collections::VecDeque; // use std::collections::BTreeMap; #[allow(dead_code)] pub fn print_map(map: &HashMap) { for (k, v) in map.iter() { println!("{:?}: {:?}", k, v); } } #[allow(dead_code)] pub fn say_vec(nums: Vec){ println!("{:?}", nums); } #[allow(dead_code)] pub fn char_frequency(s: String) -> HashMap { let mut res:HashMap = HashMap::new(); for c in s.chars(){ *res.entry(c).or_insert(0) += 1; } res } #[allow(dead_code)] pub fn vec_counter(arr: Vec) -> HashMap { let mut c = HashMap::new(); for n in arr { *c.entry(n).or_insert(0) += 1; } c } #[allow(dead_code)] pub fn vec_to_hashset(arr: Vec) -> HashSet { HashSet::from_iter(arr.iter().cloned()) } #[allow(dead_code)] pub fn int_to_char(n: i32) -> char { // Convert number 0 to a, 1 to b, ... assert!(n >= 0 && n <= 25); (n as u8 + 'a' as u8) as char }