Skip to main content
added 104 characters in body
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

The GetMaxSum() function would be clearer if it were not so monolithic. The general outline of the program is: update the numbers for each input row based on the previous intermediate results, and fetch the maximum at the end. The program structure should reflect that. Furthermore, you can avoid storing the entire triangle in memory by working as you encounter each row.

The firstCandidate / secondCandidate comparison would be better written using Math.Max().

Your ParseLine() can be simplified using Array.ConvertAll(). Since it's a one-liner, perhaps you don't need to write it as its own method at all.

The epilogue can be simplified using LINQ.

using System;
using System.IO;
using System.Linq;

public class Triangle
{
    private int[] Row = new int[0];

    public void AppendRow(string numbers)
    {
        int[] oldRow = Row;

        // http://stackoverflow.com/a/1297250
        Row = Array.ConvertAll(numbers.Trim().Split(' '), int.Parse);

        for (int j = 0; j < Row.Length; j++)
        {
            Row[j] += Math.Max
            (
                (j > 0)             ? oldRow[j - 1] : 0,
                (j < oldRow.Length) ? oldRow[j]     : 0
            );
        }
    }

    public int GetMaxSum()
    {
        return Row.Max();
    }

    public static void Main(string[] args)
    {
        Triangle triangle = new Triangle();
        using (StreamReader reader = File.OpenText(args[0]))
        {
            while (!reader.EndOfStream)
            {
                triangle.AppendRow(reader.ReadLine());
            }
        }
        Console.WriteLine(triangle.GetMaxSum());
    }
}

The GetMaxSum() function would be clearer if it were not so monolithic. The general outline of the program is: update the numbers for each input row based on the previous intermediate results, and fetch the maximum at the end. The program structure should reflect that.

The firstCandidate / secondCandidate comparison would be better written using Math.Max().

Your ParseLine() can be simplified using Array.ConvertAll(). Since it's a one-liner, perhaps you don't need to write it as its own method at all.

The epilogue can be simplified using LINQ.

using System;
using System.IO;
using System.Linq;

public class Triangle
{
    private int[] Row = new int[0];

    public void AppendRow(string numbers)
    {
        int[] oldRow = Row;

        // http://stackoverflow.com/a/1297250
        Row = Array.ConvertAll(numbers.Trim().Split(' '), int.Parse);

        for (int j = 0; j < Row.Length; j++)
        {
            Row[j] += Math.Max
            (
                (j > 0)             ? oldRow[j - 1] : 0,
                (j < oldRow.Length) ? oldRow[j]     : 0
            );
        }
    }

    public int GetMaxSum()
    {
        return Row.Max();
    }

    public static void Main(string[] args)
    {
        Triangle triangle = new Triangle();
        using (StreamReader reader = File.OpenText(args[0]))
        {
            while (!reader.EndOfStream)
            {
                triangle.AppendRow(reader.ReadLine());
            }
        }
        Console.WriteLine(triangle.GetMaxSum());
    }
}

The GetMaxSum() function would be clearer if it were not so monolithic. The general outline of the program is: update the numbers for each input row based on the previous intermediate results, and fetch the maximum at the end. The program structure should reflect that. Furthermore, you can avoid storing the entire triangle in memory by working as you encounter each row.

The firstCandidate / secondCandidate comparison would be better written using Math.Max().

Your ParseLine() can be simplified using Array.ConvertAll(). Since it's a one-liner, perhaps you don't need to write it as its own method at all.

The epilogue can be simplified using LINQ.

using System;
using System.IO;
using System.Linq;

public class Triangle
{
    private int[] Row = new int[0];

    public void AppendRow(string numbers)
    {
        int[] oldRow = Row;

        // http://stackoverflow.com/a/1297250
        Row = Array.ConvertAll(numbers.Trim().Split(' '), int.Parse);

        for (int j = 0; j < Row.Length; j++)
        {
            Row[j] += Math.Max
            (
                (j > 0)             ? oldRow[j - 1] : 0,
                (j < oldRow.Length) ? oldRow[j]     : 0
            );
        }
    }

    public int GetMaxSum()
    {
        return Row.Max();
    }

    public static void Main(string[] args)
    {
        Triangle triangle = new Triangle();
        using (StreamReader reader = File.OpenText(args[0]))
        {
            while (!reader.EndOfStream)
            {
                triangle.AppendRow(reader.ReadLine());
            }
        }
        Console.WriteLine(triangle.GetMaxSum());
    }
}
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

The GetMaxSum() function would be clearer if it were not so monolithic. The general outline of the program is: update the numbers for each input row based on the previous intermediate results, and fetch the maximum at the end. The program structure should reflect that.

The firstCandidate / secondCandidate comparison would be better written using Math.Max().

Your ParseLine() can be simplified using Array.ConvertAll(). Since it's a one-liner, perhaps you don't need to write it as its own method at all.

The epilogue can be simplified using LINQ.

using System;
using System.IO;
using System.Linq;

public class Triangle
{
    private int[] Row = new int[0];

    public void AppendRow(string numbers)
    {
        int[] oldRow = Row;

        // http://stackoverflow.com/a/1297250
        Row = Array.ConvertAll(numbers.Trim().Split(' '), int.Parse);

        for (int j = 0; j < Row.Length; j++)
        {
            Row[j] += Math.Max
            (
                (j > 0)             ? oldRow[j - 1] : 0,
                (j < oldRow.Length) ? oldRow[j]     : 0
            );
        }
    }

    public int GetMaxSum()
    {
        return Row.Max();
    }

    public static void Main(string[] args)
    {
        Triangle triangle = new Triangle();
        using (StreamReader reader = File.OpenText(args[0]))
        {
            while (!reader.EndOfStream)
            {
                triangle.AppendRow(reader.ReadLine());
            }
        }
        Console.WriteLine(triangle.GetMaxSum());
    }
}