Split Array Largest Sum
HardThis is an extra practice problem. It doesn't have walkthrough slides. You can solve it with the pattern template you already know. Open the hints if you get stuck.
Question
You're planning a road trip along a route that's broken into legs. You know the distance of each leg. You have to drive the legs in order. Split them into m driving days. Each day covers one or more legs in a row. You can't skip or reorder any leg.
A day's total distance is the sum of the legs you drive that day. Look at every way to split the legs into m days. Return the smallest possible distance for the longest day.
Input: nums = [7, 2, 5, 10, 8], m = 2
Output: 18
Split the legs into [7, 2, 5] (14 total) and [10, 8] (18 total). No other split into 2 days gives a smaller longest day.
Input: nums = [1, 2, 3, 4, 5], m = 2
Output: 9
Split the legs into [1, 2, 3] (6 total) and [4, 5] (9 total). This keeps the longest day as small as possible.
Input: nums = [10], m = 1
Output: 10
There's only one day, so that day has to cover every leg.
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
Remember to understand the problem and plan your approach before you start coding.