POTD 6 November: Letters Collection
The Postmaster wants to write a program to answer the queries regarding letter collection in a city. A city is represented as a matrix mat of size n*m. Each cell represents a house and contains letters that are denoted by a number in the cell. The program should answer q queries which are of the following types:
- (i j) : To sum all the letters that are at a 1-hop distance from the cell (i,j) in any direction
- (i j) : To sum all the letters that are at a 2-hop distance from the cell (i,j) in any direction
The queries are given in a 2D matrix queries[][].
In one hop distance postmaster can go to any of [(i-1,j-1), (i-1,j), (i-1,j+1), (i,j-1), (i,j+1), (i+1,j-1), (i+1,j), (i+1,j+1)] from (i,j).
Example:
Input: n = 4, m = 5
mat = {{1, 2, 3, 4, 10},
{5, 6, 7, 8, 10},
{9, 1, 3, 4, 10},
{1, 2, 3, 4, 10}}
q = 2
queries = {{1 0 1},
{2 0 1}}
Output:
22 29
Explanation:
For the first query sum is 1+5+6+7+3 = 22.
For the second query sum is 9+1+3+4+8+4 = 29.
Input: n = 6, m = 6
mat = {{ 1, 2, 3, 4, 5, 6},
{ 7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24},
{25, 26, 27, 28, 29, 30},
{31, 32, 33, 34, 35, 36}}
q = 1
queries = {{2 3 2}}
Output:
336
Explanation:
The first query sum is 7+8+9+10+11+17+23+29+35+34+33+32+31+25+19+13 = 336.
Approach:
In this, we will do as mentioned in the problem. We’ll find the sum of values for every query, and for each, we will add only those index values that are valid(i.e. not out of bound).
Step-by-step approach:
- Initialise variables sum equal to 0 and
il
, jl
, ir
, and jr
to represent the indices for every query.
- Initialise a vector result to store the answer.
- Run a loop i from 0 to q.
- update sum equal to 0, and update the variable il equals to (queries[i][1] – queries[i][0]), jl equals to (queries[i][2] – queries[i][0]), ir equals to (queries[i][1] + queries[i][0]), jr equal to (queries[i][2] + queries[i][0]).
- Run a loop i from il to ir.
- If the row i and column jl is valid then add the values at ith row and jlth column from the matrix mat into the sum.
- If the row i and column jr is valid then add the values at ith row and jrth column from the matrix mat into the sum.
- Run a loop i from jl+1 to jr.
- If the row il and column i is valid then add the values at ilth row and ith column from the matrix mat into the sum.
- If the row ir and column i is valid then add the values at irth row and ith column from the matrix mat into the sum.
- Push the value of sum into the result array.
- Return the result array.
Below is the implementation of the approach:
C++
class Solution{
public :
bool isValid( int i, int j, int n, int m){
return i>=0 && i<n && j>=0 && j<m;
}
vector< int > matrixSum( int n, int m, vector<vector< int >> mat, int q, vector< int > queries[])
{
int sum=0;
vector< int >ans;
for ( int i=0;i<q;i++){
int il=(queries[i][1] - queries[i][0]);
int jl=(queries[i][2] - queries[i][0]);
int ir=(queries[i][1] + queries[i][0]);
int jr=(queries[i][2] + queries[i][0]);
for ( int j=il;j<=ir;j++){
if (isValid(j,jl,n,m)){
sum+=mat[j][jl];
}
if (isValid(j,jr,n,m)){
sum+=mat[j][jr];
}
}
for ( int j=jl+1;j<jr;j++){
if (isValid(il,j,n,m)){
sum+=mat[il][j];
}
if (isValid(ir,j,n,m)){
sum+=mat[ir][j];
}
}
ans.push_back(sum);
sum=0;
}
return ans;
}
};
|
Java
class Solution {
static boolean isValid( int i, int j, int n, int m)
{
return i >= 0 && i < n && j >= 0 && j < m;
}
static List<Integer> matrixSum( int n, int m,
int mat[][], int q,
int queries[][])
{
List<Integer> ans = new ArrayList<>();
int sum = 0 ;
for ( int i = 0 ; i < q; i++) {
int il = queries[i][ 1 ] - queries[i][ 0 ];
int jl = queries[i][ 2 ] - queries[i][ 0 ];
int ir = queries[i][ 1 ] + queries[i][ 0 ];
int jr = queries[i][ 2 ] + queries[i][ 0 ];
for ( int j = il; j <= ir; j++) {
if (isValid(j, jl, n, m)) {
sum += mat[j][jl];
}
if (isValid(j, jr, n, m)) {
sum += mat[j][jr];
}
}
for ( int j = jl + 1 ; j < jr; j++) {
if (isValid(il, j, n, m)) {
sum = sum + mat[il][j];
}
if (isValid(ir, j, n, m)) {
sum = sum + mat[ir][j];
}
}
ans.add(sum);
sum = 0 ;
}
return ans;
}
}
|
Python
class Solution:
def is_valid( self , i, j, n, m):
return 0 < = i < n and 0 < = j < m
def matrixSum( self , n, m, mat, q, queries):
ans = []
sum = 0
for i in range (q):
il = queries[i][ 1 ] - queries[i][ 0 ]
jl = queries[i][ 2 ] - queries[i][ 0 ]
ir = queries[i][ 1 ] + queries[i][ 0 ]
jr = queries[i][ 2 ] + queries[i][ 0 ]
for j in range (il, ir + 1 ):
if self .is_valid(j, jl, n, m):
sum + = mat[j][jl]
if self .is_valid(j, jr, n, m):
sum + = mat[j][jr]
for j in range (jl + 1 , jr):
if self .is_valid(il, j, n, m):
sum + = mat[il][j]
if self .is_valid(ir, j, n, m):
sum + = mat[ir][j]
ans.append( sum )
sum = 0
return ans
|
Time Complexity: O(q * (n + m)), where ‘q’ is the number of queries, ‘n’ is the number of rows in the matrix, and ‘m’ is the number of columns in the matrix.
Auxiliary Space: O(q)
Share your thoughts in the comments
Please Login to comment...