Advertisement
playbahn

Leetcode 2661. First Completely Painted Row or Column

Jan 20th, 2025
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.29 KB | Source Code | 0 0
  1. // Leetcode 2661. First Completely Painted Row or Column
  2. // https://leetcode.com/problems/first-completely-painted-row-or-column/description/?envType=daily-question&envId=2025-01-20
  3.  
  4. impl Solution {
  5.     pub fn first_complete_index(arr: Vec<i32>, mat: Vec<Vec<i32>>) -> i32 {
  6.         let mut uncolored_cells_by_rows: Vec<i32> = vec![mat[0].len() as i32; mat.len()];
  7.         let mut uncolored_cells_by_cols: Vec<i32> = vec![mat.len() as i32; mat[0].len()];
  8.         // let mut table = [(0, 0); 100_001];
  9.         let mut table = vec![(0, 0); arr.len() + 1];
  10.        
  11.         for (r, cols) in mat.iter().enumerate() {
  12.             for (c, mat_num) in cols.iter().enumerate() {
  13.                 *unsafe { table.get_unchecked_mut(*mat_num as usize) } = (r, c);
  14.             }
  15.         }
  16.  
  17.         for (index, arr_num) in arr.into_iter().enumerate() {
  18.             let (r, c) = *unsafe { table.get_unchecked(arr_num as usize) };
  19.  
  20.             match unsafe { uncolored_cells_by_rows.get_unchecked_mut(r) } {
  21.                 1 => return index as i32,
  22.                 count => *count -= 1,
  23.             }
  24.  
  25.             match unsafe { uncolored_cells_by_cols.get_unchecked_mut(c) } {
  26.                 1 => return index as i32,
  27.                 count => *count -= 1,
  28.             }
  29.         }
  30.         panic!()
  31.     }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement