📌  相关文章
📜  更新 Q 查询的行和列后给定方阵中的空单元格计数

📅  最后修改于: 2022-05-13 01:56:05.022000             🧑  作者: Mango

更新 Q 查询的行和列后给定方阵中的空单元格计数

给定 一个大小为NxN二进制矩阵,最初用 0 填充 Q查询 这样:

  • 每个查询都是 (r, c) 类型,其中 r 和 c 分别表示行号和列号。
  • 将第 r 行和第 c 列的所有 0 更改为 1。

任务是在给定矩阵中执行每个查询后找到 0 的计数。

例子:

朴素方法:解决此问题的基本方法是针对查询中提到的行和列中的元素更新矩阵的元素,然后遍历整个矩阵以找到剩余零的计数。将对所有 Q 查询重复此过程。

时间复杂度: O(Q*(N+N 2 )) = O(Q*(N 2 ))
辅助空间: O(1)

高效方法:上述方法可以通过在散列的帮助下消除遍历矩阵进行 Q 查询的需要来优化。

  • 创建一个哈希数组来存储扫描的行和列。
  • 声明变量rc以存储行和列中存在的 0 计数,并声明 ans 并存储NxN
  • 现在开始遍历给定的数组,并且在每次迭代中:
    • 检查当前行和列是否已经存在于哈希数组中。
    • 如果当前行不存在,则从 ans 中减去 r 并将 c 的值减 1。
    • 如果当前列不存在,则从 ans 中减去给定的 c,并将 r 的值减 1。
    • 将 ans 推入向量中。

插图:

下面是上述方法的实现。

C++
// C++ code to implement the above approach.
#include 
using namespace std;
 
vector countZero(
    int n, int k,
    vector >& arr)
{
    long long int ans = n, r = n, c = n;
    // for declaring n*n matrix
    ans *= ans;
    vector row(n + 1, true), col(n + 1, true);
    vector v;
    for (int i = 0; i < k; i++) {
 
        // If current row is not present,
        // subtract r from ans
        if (row[arr[i][0]]) {
            ans -= r;
            c--;
            row[arr[i][0]] = false;
        }
 
        // If current column is not present,
        // subtract c from ans and
        // decrement value of r by 1.
        if (col[arr[i][1]]) {
            ans -= c;
            r--;
            col[arr[i][1]] = false;
        }
        v.push_back(ans);
    }
    return v;
}
 
// Driver code
int main()
{
    int N = 3, Q = 3;
    vector > arr
        = { { 2, 2 }, { 2, 3 }, { 3, 2 } };
    vector ans = countZero(N, Q, arr);
 
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << " ";
    }
    cout << endl;
 
    return 0;
}


Java
// Java code to implement the above approach.
import java.util.*;
class GFG{
 
  static Vector countZero(
    int n, int k,
    int [][]  arr)
  {
    int ans = n, r = n, c = n;
 
    // for declaring n*n matrix
    ans *= ans;
    boolean []row = new boolean[n+1];
    Arrays.fill(row, true);
    boolean []col = new boolean[n+1];
    Arrays.fill(col, true);
    Vector v = new Vector();
    for (int i = 0; i < k; i++) {
 
      // If current row is not present,
      // subtract r from ans
      if (row[arr[i][0]]) {
        ans -= r;
        c--;
        row[arr[i][0]] = false;
      }
 
      // If current column is not present,
      // subtract c from ans and
      // decrement value of r by 1.
      if (col[arr[i][1]]) {
        ans -= c;
        r--;
        col[arr[i][1]] = false;
      }
      v.add(ans);
    }
    return v;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int N = 3, Q = 3;
    int [][] arr
      = { { 2, 2 }, { 2, 3 }, { 3, 2 } };
    Vector ans = countZero(N, Q, arr);
 
    for (int i = 0; i < ans.size(); i++) {
      System.out.print(ans.get(i)+ " ");
    }
    System.out.println();
 
  }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program for the above approach
def countZero(n, k, arr) :
     
    ans = n
    r = n
    c = n
     
    # for declaring n*n matrix
    ans *= ans
    row = [True] * (n + 1)
    col = [True] * (n + 1)
    v= [[]]
    for i in range(k):
 
        # If current row is not present,
        # subtract r from ans
        if (row[arr[i][0]]) :
            ans -= r
            c -= 1
            row[arr[i][0]] = False
         
 
        # If current column is not present,
        # subtract c from ans and
        # decrement value of r by 1.
        if (col[arr[i][1]]) :
            ans -= c
            r -= 1
            col[arr[i][1]] = False
         
        v.append(ans)
     
    return v
 
# Driver code
N = 3
Q = 3
arr = [[ 2, 2 ], [ 2, 3 ], [ 3, 2 ]]
ans = countZero(N, Q, arr)
 
for i in range(1, len(ans)):
   print(ans[i], end = " ")
     
# This code is contributed by code_hunt.


C#
// C# code to implement the above approach.
using System;
using System.Collections;
 
class GFG {
 
  static ArrayList countZero(int n, int k, int[, ] arr)
  {
    int ans = n, r = n, c = n;
 
    // for declaring n*n matrix
    ans *= ans;
    ArrayList row = new ArrayList(n + 1);
    ArrayList col = new ArrayList(n + 1);
 
    for (int i = 0; i < n + 1; i++) {
      row.Add(1);
      col.Add(1);
    }
 
    ArrayList v = new ArrayList();
    for (int i = 0; i < k; i++) {
 
      // If current row is not present,
      // subtract r from ans
      if ((int)row[arr[i, 0]] == 1) {
        ans -= r;
        c--;
        row[arr[i, 0]] = 0;
      }
 
      // If current column is not present,
      // subtract c from ans and
      // decrement value of r by 1.
      if ((int)col[arr[i, 1]] == 1) {
        ans -= c;
        r--;
        col[arr[i, 1]] = 0;
      }
      v.Add(ans);
    }
    return v;
  }
 
  // Driver code
  public static void Main()
  {
    int N = 3, Q = 3;
    int[, ] arr = { { 2, 2 }, { 2, 3 }, { 3, 2 } };
 
    ArrayList ans = countZero(N, Q, arr);
 
    for (int i = 0; i < ans.Count; i++) {
      Console.Write(ans[i] + " ");
    }
    Console.WriteLine();
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出:
4 2 1

时间复杂度: O(K)
空间复杂度: O(N)