From e8825168b02f7a5d15b3f9305dd4cfa1eb9b08ef Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 21 Apr 2026 04:22:52 -0600 Subject: [PATCH] adding algo --- .../ex_138_minimum_path_sum.py | 14 ++++++++++ .../ex_138_minimum_path_sum.ts | 12 +++++++++ .../test_138_minimum_path_sum_round_22.py | 27 +++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_138_minimum_path_sum.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_138_minimum_path_sum.ts create mode 100644 tests/test_150_questions_round_22/test_138_minimum_path_sum_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_138_minimum_path_sum.py b/src/my_project/interviews/top_150_questions_round_22/ex_138_minimum_path_sum.py new file mode 100644 index 00000000..e2d6cf92 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_138_minimum_path_sum.py @@ -0,0 +1,14 @@ +from typing import List + +class Solution: + def minPathSum(self, grid: List[List[int]]) -> int: + m, n = len(grid), len(grid[0]) + dp = [row[:] for row in grid] + for i in range(1, m): + dp[i][0] += dp[i - 1][0] + for j in range(1, n): + dp[0][j] += dp[0][j - 1] + for i in range(1, m): + for j in range(1, n): + dp[i][j] += min(dp[i - 1][j], dp[i][j - 1]) + return dp[m - 1][n - 1] diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_138_minimum_path_sum.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_138_minimum_path_sum.ts new file mode 100644 index 00000000..93c2a350 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_138_minimum_path_sum.ts @@ -0,0 +1,12 @@ +function minPathSum(grid: number[][]): number { + const m = grid.length, n = grid[0].length; + const dp: number[][] = grid.map(row => [...row]); + for (let i = 1; i < m; i++) dp[i][0] += dp[i - 1][0]; + for (let j = 1; j < n; j++) dp[0][j] += dp[0][j - 1]; + for (let i = 1; i < m; i++) { + for (let j = 1; j < n; j++) { + dp[i][j] += Math.min(dp[i - 1][j], dp[i][j - 1]); + } + } + return dp[m - 1][n - 1]; +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_138_minimum_path_sum_round_22.py b/tests/test_150_questions_round_22/test_138_minimum_path_sum_round_22.py new file mode 100644 index 00000000..99ae04b0 --- /dev/null +++ b/tests/test_150_questions_round_22/test_138_minimum_path_sum_round_22.py @@ -0,0 +1,27 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_138_minimum_path_sum import Solution + + +class MinimumPathSumTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1(self): + self.assertEqual(7, self.solution.minPathSum([[1,3,1],[1,5,1],[4,2,1]])) + + def test_example_2(self): + self.assertEqual(12, self.solution.minPathSum([[1,2,3],[4,5,6]])) + + def test_single_cell(self): + self.assertEqual(5, self.solution.minPathSum([[5]])) + + def test_single_row(self): + self.assertEqual(6, self.solution.minPathSum([[1,2,3]])) + + def test_single_column(self): + self.assertEqual(6, self.solution.minPathSum([[1],[2],[3]])) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file