Skip to content

Commit 4388f75

Browse files
committed
Runtime: 1888 ms (Top 33.3%) | Memory: 33.75 MB (Top 100.0%)
1 parent d2ba50e commit 4388f75

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
class Solution:
2-
def rangeAddQueries(self, n: int, queries: List[List[int]]) -> List[List[int]]:
3-
# initialize the matrix with all zeroes
4-
mat = [[0] * n for _ in range(n)]
1+
# Runtime: 1888 ms (Top 33.3%) | Memory: 33.75 MB (Top 100.0%)
2+
3+
class Solution(object):
4+
def rangeAddQueries(self, n, queries):
5+
mat = [[0] * (n + 1) for _ in range(n + 1)]
6+
7+
for startRow, startCol, endRow, endCol in queries:
8+
for r in range(startRow, endRow + 1):
9+
mat[r][startCol] += 1
10+
mat[r][endCol + 1] -= 1
11+
512

6-
# perform the queries
7-
for row1, col1, row2, col2 in queries:
8-
mat[row1][col1] += 1
9-
if row2+1 < n:
10-
mat[row2+1][col1] -= 1
11-
if col2+1 < n:
12-
mat[row1][col2+1] -= 1
13-
if row2+1 < n and col2+1 < n:
14-
mat[row2+1][col2+1] += 1
1513

16-
# update the matrix
1714
for i in range(n):
1815
for j in range(1, n):
19-
mat[i][j] += mat[i][j-1]
16+
mat[i][j] += mat[i][j - 1]
17+
2018

21-
for i in range(1, n):
19+
ans = [[0] * n for _ in range(n)]
20+
for i in range(n):
2221
for j in range(n):
23-
mat[i][j] += mat[i-1][j]
24-
25-
return mat
22+
ans[i][j] = mat[i][j]
23+
24+
return ans
25+
26+
27+
28+

0 commit comments

Comments
 (0)