trimmed
to store the trimmed length for all subarrays starting from index i
to j
.trimmed
array by iterating over all possible subarrays and counting the frequency of each element. If an element's frequency becomes 2, increase the trimmed length by 2; if it's more than 2, increase by 1.dp
to store the minimum cost to split the array starting from index i
.i
from 0 to n-1
, calculate the minimum cost by trying all possible next split positions j
and using the precomputed trimmed
lengths.i
is the minimum of dp[j] + k + trimmed[i][j-1]
for all j
from i+1
to n
.dp[0]
as the minimum cost to split the entire array.