Created
February 6, 2022 14:13
-
-
Save HarshKumarChoudary/d4cafa102198993ec384e9d933b1f645 to your computer and use it in GitHub Desktop.
Given a matrix N*N, and an integer K. The matrix initially contains 0, for each i in [1,k] you are given two integers in vectors of size 2 each. These are row and columns respectively. in each time you can update that cells with that row and col to 1. And for each k you have to find the number of cells having 0; IN O (1).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
map<long long int,long long int>mr,mc; | |
long long int cntc = 0; | |
long long int cntr = 0; | |
long long int i = 0; | |
long long int val = (long long int)(n*n); | |
vector<long long int>ans; | |
while(k--){ | |
long long r = arr[i][0]; | |
long long c = arr[i][1]; | |
++i; | |
cout<<val<<endl; | |
if(mr.find(r)==mr.end()){ | |
val -= n; | |
val += cntc; | |
++cntr; | |
mr[r] = 1; | |
} | |
if(mc.find(c)==mc.end()){ | |
val -= n; | |
val += cntr; | |
++cntc; | |
mc[c] = 1; | |
} | |
ans.push_back(val); | |
} | |
return ans; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment