memo
with default values indicating uncomputed states.dp(i, k)
which returns the minimum space wasted starting from index i
with k
resizes left.i
is equal to the length of nums
, return 0 as there are no more elements to consider.k
is less than 0, return an infinite value to indicate that we cannot resize more than allowed.memo[i][k]
is already computed, return it.i
to the end, updating the maximum number and total sum.dp(j + 1, k - 1)
.memo[i][k]
and return it.dp(0, k)
to get the minimum total space wasted.