Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON 3 PLEASE FOLLOW INSTRUCTIONS #COMMENT STEPS :) Problem Given a triangle of integers, we want to find the path that has the largest sum

PYTHON 3

PLEASE FOLLOW INSTRUCTIONS

#COMMENT STEPS :)image text in transcribed

Problem

Given a triangle of integers, we want to find the path that has the largest sum going from the top to the bottom of the triangle. The way we go down the triangle is by moving down level by level, at each level having the choice to either go straight down to the integer directly below, or the integer below and to the right.

Consider the following triangle:

 
 

10

 

25 13

 

19 78 65

 

17 22 54 89

We start moving down the triangle from the top, with the node 10, where we have two options to choose from:

image text in transcribed

For the triangle given above, the maximum path from the top to the bottom is shown below:

image text in transcribed

Notice how the maximum path isn't necessarily always given by picking the element with the highest number at each step (see how we pick 15 over 19 when we are at 10 and trying to go from the first level to the second level).

Hint: consider solving this problem recursively:

Think about the base case. What's the simplest form of this problem that we can solve?

Think about how to articulate the two choices you have at each step

Think about how you're going to pick the best choice at each step

Compute this by completing the function maxPath(), which takes in a row R and a column C and a 2d-array triangle, and returns the maximum path from (R, C) to the bottom of the triangle. The top row in the triangle will have index 0, and the leftmost column will also have index 0.

1 triangLe-L 2 10], 3 C15,19], 4 [65,12,78], 5 [11,99,54,29 6 7 8 def maxPath(R, C, triangle)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions