Project Euler #81: Path sum: two ways

Sort by

recency

|

28 Discussions

|

  • + 0 comments
    Python
    def find_min_path_sum(M):
        N = len(M)
        for i in range(1, N):
            M[i][0] += M[i-1][0]
            M[0][i] += M[0][i-1]
        for i in range(1, N):
            for j in range(1, N):
                M[i][j] += min(M[i-1][j], M[i][j-1])
        return M[-1][-1]
    
    print(find_min_path_sum([list(map(int, input().split())) for _ in range(int(input()))]))
    
  • + 0 comments

    Easy solution in Python 3

    def find_min_path_sum(grid):
        rows=len(grid)
        columns=len(grid)
        res=[[0]*len(grid) for i in range(len(grid))]
        res[0][0]=grid[0][0]
        for i in range(1,rows):
            res[i][0]=res[i-1][0]+grid[i][0]
        for j in range(1,columns):
            res[0][j]=res[0][j-1]+grid[0][j]
        for i in range(1,rows):
            for j in range(1,columns):
                res[i][j]=grid[i][j]+min(res[i-1][j],res[i][j-1])
        return res[rows-1][columns-1]   
    n=int(input().strip())
    grid=[]
    for _ in range(n):
        grid.append(list(map(int,input().strip().split())))
    print(find_min_path_sum(grid))
    
  • + 0 comments

    What is capital N here mean?

  • + 0 comments

    Keyword: Min cost path dynamic programming!

  • + 0 comments

    My compiled HackerRank solutions (Python 3): https://github.com/JNYH/hackerrank