示例#1
0
        public static int MaxPath(PyramidDefinition pyramid)
        {
            for (int i = pyramid.Rows.Length - 1; i > 0; i--)
            {
                // for each row, take the highest from each pair and add to the row above

                int[] row = pyramid.Rows[i];
                int[] previousRow = pyramid.Rows[i-1];

                for (int j = 0; j < row.Length-1; j++)
                {
                    previousRow[j] += Math.Max(row[j], row[j+1]);
                }
            }

            return pyramid.Rows[0][0];
        }
示例#2
0
        public void PyramidMaxSumTests()
        {
            PyramidDefinition pyramid = new PyramidDefinition(new int[][]
            {
                new [] {55},
                new [] {94, 48},
                new [] {95, 30, 96},
                new [] {77, 71, 26, 67},
            });

            Assert.AreEqual(321, PyramidMaxSumPath.MaxPath(pyramid));

            PyramidDefinition pyramid2 = new PyramidDefinition(new int[][]
            {
                new [] {1},
                new [] {1, 1},
                new [] {1, 100, 1},
                new [] {1, 1, 1, 1},
            });

            Assert.AreEqual(103, PyramidMaxSumPath.MaxPath(pyramid2));
        }