FunctionRacerProblemsRace

Best Stretch Sum

MediumDynamic programmingPython, JavaScript, TypeScript, Java, C++

Find the stretch of one or more consecutive nodes with the largest total, and return that total. The stretch must contain at least one node, so if every value is negative the answer is the largest single value. The list is passed in as head.

Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.

Example 1

head [-2,1,-3,4,-1,2,1,-5,4]
returns 6

The stretch 4, -1, 2, 1 adds up to 6.

Example 2

head [-3,-1,-2]
returns -1

All negative: take just -1.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Dynamic programming problems