Best Stretch Sum
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
1 <= number of nodes <= 100-100 <= Node.val <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Dynamic programming problems
- Cheapest DescentMedium
- Cheapest Pool PassesMedium
- Coins From Either EndMedium
- Count DerangementsMedium
- Count the Free PlotsMedium
- Dice Sum WaysMedium
- Fairest Stone SplitMedium
- Fewest Perfect SquaresMedium