KatInterview

Greedy Task Merging and Segment Removal Cost

Heaps, Sorting & SearchAuto-graded · 18 test cases
MediumAuto-gradedHeaps, Sorting & Searchgreedyheapprefix-sums

Last updated

Tests greedy heap-based merge cost minimization and prefix-sum sliding-window removal to minimize adjacent absolute differences.

Greedy Task Merging and Segment Removal Cost

You will implement two independent cost-minimization routines, packaged together in a single function.


Part 1 — Optimal Task Consolidation

You have a list of tasks, each with a positive integer size. In one operation you pick any two tasks of sizes a and b, combine them into a single task of size a + b, and pay a cost of a + b. Repeat until only one task remains.

Return the minimum total cost to fully consolidate all tasks.

  • If the list is empty or contains exactly one task, no operations are needed and the cost is 0.

Part 2 — Best Segment Removal

You have an array of integer values and a positive integer k. Remove exactly k consecutive elements (a contiguous block) from the array. For the elements that remain — kept in their original relative order — compute the roughness: the sum of |value[i] − value[i+1]| over every adjacent pair in the remaining sequence.

Find the removal that produces the smallest roughness. Return that minimum roughness together with the starting index (0-based, in the original array) of the removed block.

  • If multiple starting positions yield the same minimum roughness, return the smallest such starting index.
  • It is guaranteed that 1 ≤ k < len(values), so at least one element always remains.

Return value

Return a single list in this exact format:

[merge_cost, [min_roughness, best_start_index]]

where merge_cost is the answer to Part 1 and [min_roughness, best_start_index] is the answer to Part 2.


Constraints

  • 0 ≤ len(tasks) ≤ 10^4; each task size is a positive integer ≤ 10^5.
  • 2 ≤ len(values) ≤ 10^5; each value is an integer in [−10^9, 10^9].
  • 1 ≤ k < len(values).
  • Aim for O(m log m) on Part 1 (where m = len(tasks)) and O(n) on Part 2 (where n = len(values)).

Examples

Example 1

Input: compute_array_costs([1, 2, 3, 4], [1, 3, 2, 5, 4], 2)

Output: [19, [3, 2]]

Example 2

Input: compute_array_costs([5], [10, 1, 8, 3], 1)

Output: [0, [7, 1]]

Example 3

Input: compute_array_costs([4, 2, 7], [9, 2, 6, 1, 8, 3], 3)

Output: [19, [6, 1]]

Hints

Hint 1

For Part 1, think about what determines how many times each original task size contributes to the total cost. That framing points directly to a well-known greedy tree-building algorithm that uses a min-heap.

Hint 2

For Part 2, instead of recomputing the full roughness from scratch for each window, precompute a prefix array of the adjacent absolute differences. Then express each candidate removal's roughness as three pieces: edges entirely on the left, edges entirely on the right, and at most one new bridge edge where the two surviving segments meet.

Hint 3

Watch the index arithmetic on the right-side prefix sum. The edge between positions `end` and `end+1` involves a removed element, so it must be excluded. Also remember that when the removed block starts at index 0 or ends at the last index, there is no bridge edge to add.