forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_sub_squares.py
More file actions
43 lines (34 loc) · 1.17 KB
/
sum_sub_squares.py
File metadata and controls
43 lines (34 loc) · 1.17 KB
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
37
38
39
40
41
42
43
"""
Sum of Sub-Squares
Given a square matrix of size n x n and an integer k, compute the sum
of all k x k sub-squares and return the results as a matrix.
Reference: https://www.geeksforgeeks.org/given-n-x-n-square-matrix-find-sum-sub-squares-size-k-x-k/
Complexity:
Time: O(n^2 * k^2)
Space: O((n - k + 1)^2)
"""
from __future__ import annotations
def sum_sub_squares(matrix: list[list[int]], k: int) -> list[list[int]] | None:
"""Compute sums of all k x k sub-squares in the matrix.
Args:
matrix: Square matrix of size n x n.
k: Side length of the sub-squares.
Returns:
Matrix of sub-square sums, or None if k > n.
Examples:
>>> sum_sub_squares([[1, 1, 1], [2, 2, 2], [3, 3, 3]], 2)
[[6, 6], [9, 9]]
"""
size = len(matrix)
if k > size:
return None
result_size = size - k + 1
result = [[0] * result_size for _ in range(result_size)]
for i in range(result_size):
for j in range(result_size):
total = 0
for p in range(i, k + i):
for q in range(j, k + j):
total += matrix[p][q]
result[i][j] = total
return result