From 3b56b7ec0ae0be404fbe9807d3e68490dc3822fc Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 28 Apr 2026 04:45:41 -0600 Subject: [PATCH] adding updates --- .../ex_145_maximal_square.py | 19 +++++++++++ .../ex_145_maximal_square.ts | 21 ++++++++++++ .../test_145_maximal_square_round_22.py | 33 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_145_maximal_square.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_145_maximal_square.ts create mode 100644 tests/test_150_questions_round_22/test_145_maximal_square_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_145_maximal_square.py b/src/my_project/interviews/top_150_questions_round_22/ex_145_maximal_square.py new file mode 100644 index 00000000..e49d7bbc --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_145_maximal_square.py @@ -0,0 +1,19 @@ +from typing import List + + +class Solution: + def maximalSquare(self, matrix: List[List[str]]) -> int: + m, n = len(matrix), len(matrix[0]) + dp = [[0] * n for _ in range(m)] + max_side = 0 + + for i in range(m): + for j in range(n): + if matrix[i][j] == '1': + if i == 0 or j == 0: + dp[i][j] = 1 + else: + dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1 + max_side = max(max_side, dp[i][j]) + + return max_side * max_side diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_145_maximal_square.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_145_maximal_square.ts new file mode 100644 index 00000000..b7708d2f --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_145_maximal_square.ts @@ -0,0 +1,21 @@ +function maximalSquare(matrix: string[][]): number { + const m = matrix.length; + const n = matrix[0].length; + const dp: number[][] = Array.from({ length: m }, () => new Array(n).fill(0)); + let maxSide = 0; + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (matrix[i][j] === '1') { + if (i === 0 || j === 0) { + dp[i][j] = 1; + } else { + dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1; + } + maxSide = Math.max(maxSide, dp[i][j]); + } + } + } + + return maxSide * maxSide; +}; diff --git a/tests/test_150_questions_round_22/test_145_maximal_square_round_22.py b/tests/test_150_questions_round_22/test_145_maximal_square_round_22.py new file mode 100644 index 00000000..6c551462 --- /dev/null +++ b/tests/test_150_questions_round_22/test_145_maximal_square_round_22.py @@ -0,0 +1,33 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_145_maximal_square import Solution + + +class MaximumSquareTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1(self): + matrix = [ + ["1", "0", "1", "0", "0"], + ["1", "0", "1", "1", "1"], + ["1", "1", "1", "1", "1"], + ["1", "0", "0", "1", "0"], + ] + self.assertEqual(self.solution.maximalSquare(matrix), 4) + + def test_example_2(self): + matrix = [["0", "1"], ["1", "0"]] + self.assertEqual(self.solution.maximalSquare(matrix), 1) + + def test_example_3(self): + matrix = [["0"]] + self.assertEqual(self.solution.maximalSquare(matrix), 0) + + def test_all_ones(self): + matrix = [["1", "1"], ["1", "1"]] + self.assertEqual(self.solution.maximalSquare(matrix), 4) + + +if __name__ == '__main__': + unittest.main()