All problems
0553HardArrayDynamic ProgrammingHeap (Priority Queue)

Auditor Discards from the Reading Ledger

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2163Minimum Difference in Sums After Removal of Elements

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A meter has written down 3 * n readings in the order they were taken, given as nums.

An auditor strikes out exactly n of the readings, chosen freely. The 2 * n readings still standing keep their original order: the earlier half of them, meaning the first n that survive, is the opening block, and the later half, the last n that survive, is the closing block.

The score of a set of strike-outs is the total of the opening block minus the total of the closing block. This score may be negative. Return the smallest score the auditor can reach.

Examples

Example 1

Input
nums = [4, 9, 2]
Output
-5

Strike out the last reading, 2. The opening block is 4 and the closing block is 9, so the score is 4 - 9 = -5.

Example 2

Input
nums = [6, 2, 8, 3, 9, 4]
Output
-9

Strike out the 3 at position 3 and the 4 at position 5. The survivors in order are 6, 2, 8, 9, so the opening block totals 8 and the closing block totals 17, giving a score of -9.

Example 3

Input
nums = [5, 5, 5, 5, 5, 5]
Output
0

Every reading is 5, so whichever two are struck out both blocks total 10 and the score is 0.

Constraints

  • nums.length == 3 * n
  • 1 <= n <= 10^5
  • 1 <= nums[i] <= 10^5
  • The score therefore lies between -10^10 and 10^10.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def minimum_difference(nums: list[int]) -> int:
Java
public long minimumDifference(int[] nums)
September 7
Apply