Skip to content

Commit 8ebedca

Browse files
committed
chore(algorithms, triangle): minor fixes
1 parent 6aab5b8 commit 8ebedca

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

algorithms/dynamic_programming/min_path_sum/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ grows quadratically.
5252

5353
### Space Complexity
5454

55-
The space complexity is O(n) since we only maintain a single working array `dp` with on entry per row.
55+
The space complexity is O(n) since we only maintain a single working array `dp` with one entry per row.

algorithms/dynamic_programming/min_path_sum/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def min_path_sum_2(triangle: List[List[int]]) -> int:
4141
Complexity:
4242
Time Complexity results in O(n^2), since we visit every number in the triangle exactly once. For n rows, there are
4343
roughtl n^2/2 elements.
44-
Space Complexity is O(1) since the triangle input array is updated in place
44+
Space Complexity is O(n) since the triangle input array's last row is copied over
4545
4646
Args:
4747
triangle(list): A list of lists of integers representing the triangle

algorithms/dynamic_programming/min_path_sum/test_min_path_sum.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import copy
23
from typing import List
34
from parameterized import parameterized
45
from algorithms.dynamic_programming.min_path_sum import min_path_sum, min_path_sum_2
@@ -15,12 +16,14 @@
1516
class MinPathSumInTriangleTestCase(unittest.TestCase):
1617
@parameterized.expand(TEST_CASES)
1718
def test_min_path_sum_in_triangle(self, triangle: List[List[int]], expected: int):
18-
actual = min_path_sum(triangle)
19+
input_triangle = copy.deepcopy(triangle)
20+
actual = min_path_sum(input_triangle)
1921
self.assertEqual(expected, actual)
2022

2123
@parameterized.expand(TEST_CASES)
2224
def test_min_path_sum_2_in_triangle(self, triangle: List[List[int]], expected: int):
23-
actual = min_path_sum_2(triangle)
25+
input_triangle = copy.deepcopy(triangle)
26+
actual = min_path_sum_2(input_triangle)
2427
self.assertEqual(expected, actual)
2528

2629

0 commit comments

Comments
 (0)