Project Euler #67: Maximum path sum II

Sort by

recency

|

56 Discussions

|

  • + 0 comments

    Python

    def helper(rows, triangle):
        for row in range(rows - 2, -1, -1):
            for col in range(len(triangle[row])):
                triangle[row][col] += max(triangle[row + 1][col], 
                                          triangle[row + 1][col + 1])
        
        return triangle[0][0]
    
    for _ in range(int(input())):
            rows = int(input())
            triangle = []
    
            for _ in range(rows):
                row = list(map(int, input().split()))
                triangle.append(row)
            
            result = helper(rows, triangle)
            print(result)
    
  • + 0 comments

    100 points. Python 3

    def max_path(n,triangle):
        temp=[[] for _ in range(n)]
        for k in triangle[-1]:
            temp[-1].append(k)
        for i in range(n-2,-1,-1):
            for j in range(i+1):
                temp[i].append(triangle[i][j]+max(temp[i+1][j],temp[i+1][j+1]))
        return temp[0][0]
    t=int(input().strip())
    for _ in range(t):
        n=int(input().strip())
        triangle=[]
        for i in range(n):
            triangle.append(list(map(int,input().strip().split())))
        print(max_path(n,triangle))
    
  • + 0 comments

    Here is my Python 3 100/- Points solution

    def find_maximum_total(triangle):
        n = len(triangle)
    
        for i in range(n - 2, -1, -1):
            for j in range(len(triangle[i])):
                triangle[i][j] += max(triangle[i+1][j], triangle[i+1][j+1])
    
        return triangle[0][0]
    
    # Read the number of testcases
    t = int(input().strip())
    
    for _ in range(t):
        n = int(input().strip())
        triangle = []
        for _ in range(n):
            row = list(map(int, input().strip().split()))
            triangle.append(row)
    
        result = find_maximum_total(triangle)
        print(result)
    
  • + 0 comments

    In PHP

    Since I'll be using a bottom-up approach, I've assembled the triangle in reverse before calling the solution function

    <?php
    
    function solution($triangle) {
        $len = count($triangle);
        foreach ($triangle as $i => &$curr) {
            if ($i == $len - 1) {
                return $curr[0];
            }
            
            $next = &$triangle[$i + 1];
            foreach ($next as $j => $val) {
                $next[$j] += max($curr[$j], $curr[$j + 1]);
            }
        }
    }
    
    $_fp = fopen("php://stdin", "r");
    $q = intval(trim(fgets($_fp)));
    
    while ($q--) {
        $j = intval(trim(fgets($_fp)));
        $triangle = array_fill(0, $j, null);
        while ($j--) {
            $triangle[$j] = array_map('intval', explode(' ', trim(fgets($_fp))));
        }
        echo solution($triangle), PHP_EOL;
    }
    
    fclose($_fp);
    
  • + 0 comments

    Solution in c#

    static void Main(String[] args) 
    {
        var tt = Convert.ToInt32(Console.ReadLine());
        for(int i=0; i<tt; i++)
        {
            var list = new List<List<int>>();
            var loop = Convert.ToInt32(Console.ReadLine());
            
            for(int j=0; j<loop;j++)
            {
                var current = Console
                                .ReadLine()
                                .Trim()
                                .ToString()
                                .Split(' ')
                                .Select(x=>Convert.ToInt32(x))
                                .ToList();
                list.Add(current);
            }
            for (var m = list.Count - 2; m >= 0; m--)
            {
                for (var n = 0; n <= m; n++)
                {
                    list[m][n] += 
                    Math.Max(list[m + 1][n], list[m + 1][n + 1]);
                }
            }
            Console.WriteLine(list[0][0]);
        }
    }