As reported: LinkedIn Senior Software Engineer online assessment (Mar 2025), on HackerRank: two questions in 90 minutes. This was the first; a 2D DP solved it, and the second question needed close to
O(N)orO(N log N).
You are given an array arr and an integer K. Without changing the order, split arr into exactly K non-empty contiguous pieces. The cost of a split is the sum of each piece's maximum element. Return the minimum possible cost.
It is the same task as LeetCode 1335 — Minimum Difficulty of a Job Schedule, with K ≤ N always.
Example 1
arr = [1,5,3,4,2], K = 2
Output: 6 ([1] | [5,3,4,2] → 1 + 5)
Example 2
arr = [6,5,4,3,2,1], K = 2
Output: 7 ([6,5,4,3,2] | [1] → 6 + 1)
Example 3
arr = [7,1,7,1,7,1], K = 3
Output: 15 (for example [7] | [1,7] | [1,7,1] → 7 + 7 + 1)
Constraints: 1 ≤ K ≤ N < 300 where N = arr.length; 1 ≤ arr[i] < 10^5.
Code runner format: LeetCode's test-case format, one JSON value per line (one line per argument). Implement minSumOfMaxima(arr, k); the harness prints the result.