A build tool must concatenate several output chunks into one file. It joins two chunks at a time, and joining chunks of sizes x and y costs x + y (it reads and writes every byte). The joined chunk can be joined again later, in any order.
Write cheapestConcat(sizes) that returns the smallest total cost to end with a single chunk. With zero or one chunk there's nothing to join, so the cost is 0.
Input: sizes = [2,4,3] Output: 14
Join 2 and 3 (cost 5), then 5 and 4 (cost 9): 5 + 9 = 14.
Input: sizes = [1,8,3,5] Output: 30
Join 1 + 3 = 4, then 4 + 5 = 9, then 9 + 8 = 17: 4 + 9 + 17 = 30.
0 <= sizes.length <= 100001 <= sizes[i] <= 1000Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #601 Cheapest Way to Concatenate Chunks. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.