From c13c1f86c73b613f098ef5aee3be27df02cd927f Mon Sep 17 00:00:00 2001 From: Evgenii Seliavka Date: Mon, 6 Apr 2026 13:43:00 -0400 Subject: [PATCH] Add LeetCode 3070 solution --- .../solutions_03000/solution_03070.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 practice/leetcode/solutions_03000/solution_03070.py diff --git a/practice/leetcode/solutions_03000/solution_03070.py b/practice/leetcode/solutions_03000/solution_03070.py new file mode 100644 index 0000000..0fef52d --- /dev/null +++ b/practice/leetcode/solutions_03000/solution_03070.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +"""LeetCode solution 03070.""" + +import unittest + + +class Solution(object): + def countSubmatrices(self, grid, k): + """ + :type grid: List[List[int]] + :type k: int + :rtype: int + """ + rows = len(grid) + cols = len(grid[0]) + prefix = [[0] * cols for _ in range(rows)] + ans = 0 + + for row in range(rows): + for col in range(cols): + prefix[row][col] = grid[row][col] + if row > 0: + prefix[row][col] += prefix[row - 1][col] + if col > 0: + prefix[row][col] += prefix[row][col - 1] + if row > 0 and col > 0: + prefix[row][col] -= prefix[row - 1][col - 1] + + if prefix[row][col] <= k: + ans += 1 + + return ans + + +class TestSolution(unittest.TestCase): + def test_countSubmatrices(self): + solution = Solution() + self.assertEqual(solution.countSubmatrices([[7, 6, 3], [6, 6, 1]], 18), 4) + self.assertEqual(solution.countSubmatrices([[1, 1], [1, 1]], 3), 3) + + +if __name__ == '__main__': + unittest.main()