aboutsummaryrefslogtreecommitdiff
path: root/challenge-064/lubos-kolouch/python/ch-1.py
blob: 1d513712a38b37cfe53a74d6509b4d067a8cd32d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import List


def min_sum_path(matrix: List[List[int]]) -> int:
    rows = len(matrix)
    columns = len(matrix[0])

    for i in range(1, rows):
        matrix[i][0] += matrix[i - 1][0]

    for j in range(1, columns):
        matrix[0][j] += matrix[0][j - 1]

    for i in range(1, rows):
        for j in range(1, columns):
            matrix[i][j] += min(matrix[i - 1][j], matrix[i][j - 1])

    return matrix[-1][-1]


matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

print(f"Minimum sum path: {min_sum_path(matrix)}")  # Output: 21