Matrix Rectangular Sums - Facebook Top Interview Questions
Problem Statement :
Given a two-dimensional list of integers matrix and an integer k, return a new matrix M of the same dimensions where M[i][j] is the sum of all numbers sum(matrix[r][c]) for all (i - k ≤ r ≤ i + k, j - k ≤ c ≤ j + k) Constraints n, m ≤ 250 where n and m is the number of rows and columns in matrix Example 1 Input matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] k = 1 Output [ [12, 21, 16], [27, 45, 33], [24, 39, 28] ]
Solution :
Solution in C++ :
int rectangle(vector<vector<int>>& dp, int r1, int c1, int r2, int c2) {
int res = dp[r2 + 1][c2 + 1];
res -= dp[r2 + 1][c1];
res -= dp[r1][c2 + 1];
res += dp[r1][c1];
return res;
}
vector<vector<int>> solve(vector<vector<int>>& matrix, int k) {
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> res(m, vector<int>(n));
vector<vector<int>> dp(m + 1, vector<int>(n + 1));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
dp[i + 1][j + 1] = matrix[i][j] + dp[i][j + 1] + dp[i + 1][j] - dp[i][j];
}
}
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
int r1 = max(0, r - k);
int c1 = max(0, c - k);
int r2 = min(m - 1, r + k);
int c2 = min(n - 1, c + k);
res[r][c] = rectangle(dp, r1, c1, r2, c2);
}
}
return res;
}
Solution in Java :
import java.util.*;
class Solution {
public int[][] solve(int[][] matrix, int k) {
RangeSumMatrix rsm = new RangeSumMatrix(matrix);
int[][] ret = new int[matrix.length][matrix[0].length];
for (int i = 0; i < ret.length; i++) {
for (int j = 0; j < ret[0].length; j++) {
ret[i][j] = rsm.total(i - k, j - k, i + k, j + k);
}
}
return ret;
}
}
class RangeSumMatrix {
int[][] matrix;
public RangeSumMatrix(int[][] matrix) {
this.matrix = matrix;
for (int i = 0; i < matrix.length; i++) {
for (int j = 1; j < matrix[0].length; j++) {
matrix[i][j] += matrix[i][j - 1];
}
}
}
public int total(int row0, int col0, int row1, int col1) {
row0 = Math.max(0, row0);
row1 = Math.min(matrix.length - 1, row1);
col0 = Math.max(0, col0);
col1 = Math.min(matrix[0].length - 1, col1);
int ret = 0;
for (int i = row0; i <= row1; i++) {
ret += matrix[i][col1];
if (col0 != 0) {
ret -= matrix[i][col0 - 1];
}
}
return ret;
}
}
Solution in Python :
class Solution:
def solve(self, A, k):
R, C = len(A), len(A[0])
for r in range(R):
for c in range(C):
if r:
A[r][c] += A[r - 1][c]
if c:
A[r][c] += A[r][c - 1]
if r and c:
A[r][c] -= A[r - 1][c - 1]
def get_sum(top, left, bottom, right):
top = max(top, 0)
left = max(left, 0)
bottom = min(bottom, R - 1)
right = min(right, C - 1)
return (
A[bottom][right]
- (A[top - 1][right] if top else 0)
- (A[bottom][left - 1] if left else 0)
+ (A[top - 1][left - 1] if top and left else 0)
)
return [[get_sum(r - k, c - k, r + k, c + k) for c in range(C)] for r in range(R)]
View More Similar Problems
Super Maximum Cost Queries
Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and
View Solution →Contacts
We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co
View Solution →No Prefix Set
There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio
View Solution →Cube Summation
You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor
View Solution →Direct Connections
Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do
View Solution →Subsequence Weighting
A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =
View Solution →