Ctrl K

Split With a Repeat Penalty

Hard
You are given an integer array values and an integer fee. Cut the array into one or more non-empty contiguous pieces (every element goes into exactly one piece, and the order stays the same). Each piece costs fee plus a repeat penalty: the number of its elements whose value appears at least twice within that piece. Elements whose value is unique in the piece add nothing. For example, the piece [5, 1, 5, 5, 2] has a penalty of 3 (the three 5s), so it costs fee + 3. Return the minimum total cost of a split.
Example 1
values=[4, 0, 4, 1, 1, 0], fee=3 → 8

The best cut is [4, 0, 4, 1] and [1, 0]: the first piece costs 3 + 2 (two 4s), the second costs 3, for a total of 8. Keeping everything in one piece would cost 3 + 6 = 9.

Example 2
values=[2, 2, 2, 2], fee=1 → 4

Four single-element groups have no repeats, so the total is 4 · 1 = 4.

Example 3
values=[0, 1, 2], fee=7 → 7
Constraints
  • 1 ≤ len(values) ≤ 1000
  • 0 ≤ values[i] < len(values)
  • 1 ≤ fee ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSpells Without Neighbors MediumEvery Way to Space the Words HardStretches With Exactly k Kinds HardSquare-Sum Chains HardMost Repeated Colour on a Path Hard
loading Python…
Input
values=[4, 0, 4, 1, 1, 0], fee=3
Expected
8

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