Loading...
Loading...
You are given a list of positive integers nums and an integer k.
The cost of a list is the sum of abs(b[i + 1] - b[i]) over every pair of adjacent elements b[i] and b[i + 1]. A list with fewer than two elements has cost 0.
Consider an operation where you remove exactly k consecutive elements from nums. The remaining elements keep their order and close the gap, so the element just before the removed block and the element just after it become adjacent.
Return the smallest possible cost of the list after the operation.
Input: nums = [3, 9, 4, 2, 16], k = 3
Output: 6
Explanation: Removing [3, 9, 4] leaves [2, 16] with cost 14. Removing [9, 4, 2] leaves [3, 16] with cost 13. Removing [4, 2, 16] leaves [3, 9] with cost 6, the smallest.
Input: nums = [1, 10, 1, 1], k = 1
Output: 0
Explanation: Removing the 10 leaves [1, 1, 1]. The two 1s around the removed element are now adjacent, so every difference is 0.
Input: nums = [5, 1, 5, 1, 5], k = 1
Output: 8
Explanation: Removing the first or the last element leaves a list with cost 12. Removing the element at index 1 leaves [5, 5, 1, 5] with cost 0 + 4 + 4 = 8. The middle removals also give 8.
k ≤ nums.length ≤105nums[i] ≤104