Triangle Max Total (Top to Bottom) via Adjacent Elements -- Coding Exercise
By starting at the top of the triangle and moving to adjacent numbers on the row below, the maximum total from top to bottom is 27.
5
9 6
4 6 8
0 7 1 5
I.e. 5 + 9 + 6 + 7 = 27
Write a program in a language of your choice to find the maximum total from top to bottom in triangle.txt, a text file containing a triangle with 100 rows.
Test Cases —
Input : 3 7 4 2 4 6 8 5 9 3
Output : 23
Explanation : 3 + 7 + 4 + 9 = 23
Input : 8 -4 4 2 2 6 1 1 1 1
Output : 19
Explanation : 8 + 4 + 6 + 1 = 19
- Review problem and identify / re-read Daniela’s framing of the purpose of the exercise.
- Deduce what exactly the overarching problem is.
- Narrow down the “problem” into more tangible, quantifiable, sub-problems to solve the overall problem/task at hand.
- Identify / brainstorm / whiteboard possible approaches, first steps, and partial solutions to the problem.
- Write pseudocode, based on the above, in an effort
- Utilize example/test/simple case to start with and
- Add further test cases to account for
- Start coding
- Review and reflect on what is working, what isn’t, and why.
- Define what exactly “adjacent” means in the context of this specific exercise/program
- Place numbers in some sort of organized, sortable(?) data structure for parsing through data, looping, comparing of values, etc.
- Figure out how to codify the adjacency of one number to the next, especially considering the triangle structure of the data.
- Identify pattern(s) in how the max values on each line compare with one another
- See if this can/should be into first-version and/or subsequent versions of the algorithm — or is it specific to this data set / an outlier / irrelevant? Does it scale?
- If yes to most/all of the above, incorporate into algorithm or make note to incorporate later for greater efficiency, reusability, etc.
- Compare current value with previous value (previous line’s max?) to determine if they are “adjacent” to one another
- Find max on each line of values or within each set of values, determine if this is your current value.
- If both steps 7 and 8 hold true — ADD TO runningMaxTotal variable
- Figure out how to determine if you have reached the end of a line / end of a set of values
- If true — then move to next line / set of numbers
- Repeat steps 6 through 11
- Determine if you are at the end of the final line / at the final value of your data set
- If so, terminate loop, terminate program, etc.
- Return finalMaxTotal variable value (e.g. 27, in this instance)
- Initialize chosen data structure for storing, organizing, sorting(?) data-set.
- Start with initial max sum equal to the first value in your data set / data structure
- Set up a loop to iterate, line by line / item by item
- Possible multiple levels of loops / multiple levels of array-dimensions needed for items within each line
- Run through steps 4-11 or 6-11 above
- Account for “0” values
- Account for negatives
- Account for “bad” data / “bad” user input
- Identify invalid elements of data — Add helper function to validate data-set (numbers only, no strings, no objects, etc.)
- “Clean” / scrub data — Add helper function to remove invalid elements/data and re-arrange elements to keep original/triangle structure in place, and original order of elements — both line by line and within each line
1. Array (sorted / unsorted / key-value pairs)
2. Object
3. Hash Table
4. Tree — Binary-tree
- Brute-force — iterate through all possible paths and keep running total based on adding of elements, keep max total as well, compare the two, until end of the data set / text file, and finally — return (what should be) the max value of the data set.
- Binary Search — for each line to find max of each line/set of numbers within overall data set (?)
- Recursion — given the triangle’s tree-like structure, this may help to assemble this as a binary-tree/binary-search-tree to parse
- Dynamic Programming — “Sliding-window” type of approach of determining max of adjacent elements and repeating the process as you make your way through the data-set / text-file.
- Dynamic Programming — slice/delete/remove values from data-set as they are accounted for / added to the running and/or max total, in conjunction with or to facilitate the “sliding window” approach.
- Initialize array to give the data some structure
- Initialize maxTotalSum variable
- Set maxTotalSum var to first number of triangle / array — maxSumTotal = array[0] (e.g. 5, in the example provided)
- Run through steps 4-11 or 6-11 above