Ctrl K

Reshuffle and Adjust

Medium
You are given two arrays of the same length, current and wanted. Your job is to turn current into exactly wanted. Two actions are available, as many times as you like and in any order: - **Reshuffle.** Cut current into any number of contiguous pieces and put the pieces back together in any order. Each such operation costs shuffle_fee. - **Adjust.** Add a positive integer x to any element or subtract it. This costs x. Return the minimum total cost.
Example 1
current=[5, -1, 8], wanted=[-2, 9, 4], shuffle_fee=3 → 6

Without reordering it costs 7 + 10 + 4 = 21. Reorder the pieces into [-1, 8, 5] for a fee of 3, then adjust by 1 + 1 + 1 = 3. Total 6.

Example 2
current=[1, 2], wanted=[2, 1], shuffle_fee=10 → 2

Reordering costs 10, while simply nudging both numbers costs 1 + 1 = 2.

Example 3
current=[4, 4, 4], wanted=[4, 4, 4], shuffle_fee=0 → 0
Constraints
  • 1 ≤ len(current) = len(wanted) ≤ 10⁵
  • 0 ≤ shuffle_fee ≤ 2·10¹⁰
  • -10⁵ ≤ current[i], wanted[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumStudios for Recording Sessions MediumLow, High, Low MediumStrictly Low, High, Low MediumFewest Meetings to Cancel Medium
loading Python…
Input
current=[5, -1, 8], wanted=[-2, 9, 4], shuffle_fee=3
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 128 tests on the server, hidden ones included.