更新 Q 查询的行和列后给定方阵中的空单元格计数
给定 一个大小为NxN的二进制矩阵,最初用 0 填充 和Q查询 这样:
- 每个查询都是 (r, c) 类型,其中 r 和 c 分别表示行号和列号。
- 将第 r 行和第 c 列的所有 0 更改为 1。
任务是在给定矩阵中执行每个查询后找到 0 的计数。
例子:
Input: N = 3, Q = 3, queries = {{2, 2}, {2, 3}, {3, 2}}
Output: 4 2 1
Explanation: After 1st Operation, all the 2nd row and all the 2nd column will be filled by 1. So remaining cell with value 0 will be 4.
After 2nd Operation, all the 2nd row and all the 3rd column will be filled by 1. So remaining cell with value 0 will be 2.
After 3rd Operation cells having value 0 will be 1.
Input: N = 4, Q = 3, queries = {{3, 1}, {3, 3}, {4, 2}}
Output: 9 6 3
Explanation: After 1st operation, all the 3rd row and all the 1st column will be filled by 1. So, remaining cell with value 0 will be 9.
After 2nd Operation, all the 3rd row and all the 3rd column will be filled by 1. So, remaining cell with value 0 will be 6.
After 3rd Operation cells having value 0 will be 3.
朴素方法:解决此问题的基本方法是针对查询中提到的行和列中的元素更新矩阵的元素,然后遍历整个矩阵以找到剩余零的计数。将对所有 Q 查询重复此过程。
时间复杂度: O(Q*(N+N 2 )) = O(Q*(N 2 ))
辅助空间: O(1)
高效方法:上述方法可以通过在散列的帮助下消除遍历矩阵进行 Q 查询的需要来优化。
- 创建一个哈希数组来存储扫描的行和列。
- 声明变量r和c以存储行和列中存在的 0 计数,并声明 ans 并存储NxN 。
- 现在开始遍历给定的数组,并且在每次迭代中:
- 检查当前行和列是否已经存在于哈希数组中。
- 如果当前行不存在,则从 ans 中减去 r 并将 c 的值减 1。
- 如果当前列不存在,则从 ans 中减去给定的 c,并将 r 的值减 1。
- 将 ans 推入向量中。
插图:
Consider the matrix:
N=3
0 0 0
0 0 0
0 0 0
Initially ans=n*n=9, r=3(Number of zeros in row) and c=3(Number of zeros in column)
Traverse through each given query
for e.g queries = {{2, 2}, {2, 3}, {3, 2}}
Select 1st query row=2 and column=2, check whether this row or column is already converted to one or not using hashmap.
Here row=2 is not visited, so convert all the zeros present in that row to one i.e ans-=r, due to this number of zeros in each column decremented by 1 i.e c–.
0 0 0
1 1 1
0 0 0
Now ans=9-3=6, r=3(Number of zeros in row) and c=2(Number of zeros in column)
Here col=2 is not visited in 1st query, so convert all the zeros present in that column to one i.e ans-=c, due to this number of zeros in each row decremented by 1 i.e r–.
0 1 0
1 1 1
0 1 0
Now ans=6-2=4, r=2(Number of zeros in row) and c=2(Number of zeros in column)
So after execution of 1st query number of remaining zeros is 4.
Select 2nd query row=2 and column=3, check whether this row or column is already converted to one or not using hashmap.
Here row=2 is already visited, So just continue.
Here col=3 is not visited in 1st query, so convert all the zeros present in that column to one i.e ans-=c, due to this number of zeros in each row decremented by 1 i.e r–.
0 1 1
1 1 1
0 1 1
Now ans=4-2=2, r=1(Number of zeros in row) and c=2(Number of zeros in column)
So after execution of 2nd query number of remaining zeros is 2.
Repeat the process for all query
下面是上述方法的实现。
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)