A build tool has a row of JavaScript chunks with sizes sizes, in load order. It can merge two neighboring chunks into one, and a merge costs the combined size of the two chunks. It keeps merging neighbors until a single bundle is left.
Write cheapestMerge(sizes) that returns the smallest total cost of all the merges. A single chunk needs no merging and costs 0.
Input: sizes = [3,1,2] Output: 9
Merge 1 and 2 (cost 3), then 3 and 3 (cost 6). Merging 3 and 1 first costs 4 + 6 = 10.
Input: sizes = [5,5] Output: 10
One merge of both chunks.
1 <= sizes.length <= 1001 <= 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 #542 Cheapest Bundle Merge. 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.