Skip to content

The solution done by me in C# on the challenge "Pyramid Slide Down" over at www.codewars.com

Notifications You must be signed in to change notification settings

IvanovArtyom/Pyramid-Slide-Down

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Description:

Lyrics...

Pyramids are amazing! Both in architectural and mathematical sense. If you have a computer, you can mess with pyramids even if you are not in Egypt at the time. For example, let's consider the following problem. Imagine that you have a pyramid built of numbers, like this one here:

   /3/
  \7\ 4 
 2 \4\ 6 
8 5 \9\ 3

Here comes the task...

Let's say that the 'slide down' is the maximum sum of consecutive numbers from the top to the bottom of the pyramid. As you can see, the longest 'slide down' is 3 + 7 + 4 + 9 = 23

Your task is to write a function that takes a pyramid representation as an argument and returns its largest 'slide down'. For example:

* With the input `[[3], [7, 4], [2, 4, 6], [8, 5, 9, 3]]`
* Your function should return `23`.

By the way...

My tests include some extraordinarily high pyramids so as you can guess, brute-force method is a bad idea unless you have a few centuries to waste. You must come up with something more clever than that.

(c) This task is a lyrical version of Problem 18 and/or Problem 67 on ProjectEuler.

My solution

using System;

public class PyramidSlideDown
{
    public static int LongestSlideDown(int[][] pyramid)
    {
        for (int i = pyramid.Length - 1; i > 0; i--)
        {
            for (int j = 0; j < pyramid[i].Length - 1; j++)
                pyramid[i - 1][j] += Math.Max(pyramid[i][j], pyramid[i][j + 1]);
        }

        return pyramid[0][0];
    }
}

About

The solution done by me in C# on the challenge "Pyramid Slide Down" over at www.codewars.com

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages