diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_137_triangle.py b/src/my_project/interviews/top_150_questions_round_22/ex_137_triangle.py new file mode 100644 index 00000000..6eb32f2a --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_137_triangle.py @@ -0,0 +1,9 @@ +from typing import List + +class Solution: + def minimumTotal(self, triangle: List[List[int]]) -> int: + dp = triangle[-1][:] + for row in range(len(triangle) - 2, -1, -1): + for col in range(len(triangle[row])): + dp[col] = triangle[row][col] + min(dp[col], dp[col + 1]) + return dp[0] diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_137_triangle.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_137_triangle.ts new file mode 100644 index 00000000..5fb18e16 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_137_triangle.ts @@ -0,0 +1,9 @@ +function minimumTotal(triangle: number[][]): number { + const dp: number[] = [...triangle[triangle.length - 1]]; + for (let row = triangle.length - 2; row >= 0; row--) { + for (let col = 0; col < triangle[row].length; col++) { + dp[col] = triangle[row][col] + Math.min(dp[col], dp[col + 1]); + } + } + return dp[0]; +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_137_triangle_round_22.py b/tests/test_150_questions_round_22/test_137_triangle_round_22.py new file mode 100644 index 00000000..6e5aab97 --- /dev/null +++ b/tests/test_150_questions_round_22/test_137_triangle_round_22.py @@ -0,0 +1,24 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_137_triangle import Solution + + +class TriangleTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1(self): + self.assertEqual(11, self.solution.minimumTotal([[2],[3,4],[6,5,7],[4,1,8,3]])) + + def test_example_2(self): + self.assertEqual(-10, self.solution.minimumTotal([[-10]])) + + def test_single_row(self): + self.assertEqual(5, self.solution.minimumTotal([[5]])) + + def test_two_rows(self): + self.assertEqual(3, self.solution.minimumTotal([[1],[2,3]])) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file