cswork / OA打开练习工作台

AmazonOA #291

Minimize Array Sum Difference

估计中等评测数据准备中,可先阅读题面

考点(AI 标注)原地数组贪心调度

Given an array 5 4 0 3 3 1, take sum of absolute differences between adjacent pairs i.e |5-4|+|4-0|+|0-3|+|3-3|+|3-1| = 10 The task is to remove as many elements from the array such that the sum remains same.

Example 1

Input:

arr = [5, 4, 0, 3, 3, 1]

Output:

[5, 0, 3, 1]

Explanation: Original sum of absolute differences: |5-4|+|4-0|+|0-3|+|3-3|+|3-1| = 10. After removing elements to minimize the array while keeping the sum the same: Solution 1: [5, 4, 0, 3, 1] => Sum = 10 Solution 2: [5, 0, 3, 1] => Sum = 10 Solution 2 is the acceptable answer as it has the minimum number of elements.

Example 2

Input:

arr = [6, 4, 4, 3, 3, 2]

Output:

[6, 2]

Explanation: :) IMPORTANT NOTE — The expected output from the original source is [6, 4, 3, 2]. However, a pro from the server pointed out that [6, 2] would actually make more sense as the correct answer for that example.