Question
A = [ [3], [7, 4], [2, 4, 6], [8, 5, 9, 3] ] function findLargestPath(triangle, row, col) { // Check to see if the
A = [
[3],
[7, 4],
[2, 4, 6],
[8, 5, 9, 3]
]
function findLargestPath(triangle, row, col) {
// Check to see if the row and column are actually in the triangle
if (col == -1 || col >= triangle[row].length) {
return -1; //If they are not. ensure that this path wont be taken.
}
if (row == triangle.length - 1) {
return triangle[row][col]; //Return the value.
} else {
//return this value plus the maximum path below
return triangle[row][col] + Math.max(findLargestPath(triangle, row + 1, col), findLargestPath(triangle, row + 1, col + 1));
}
}
console.log(findLargestPath(A, 0, 0));
Why recurrence of this algorithm is T(n)=2T(n-1)+O(1)
Please explain with sufficient details
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started