dp
with a size equal to the length of nums
and fill it with -1, representing uncomputed states.solve
that takes the current index i
and the array nums
.i
is greater than or equal to the length of nums
minus 2, return 0, as there are no more subarrays of size 3 to consider.dp[i]
is not -1, return dp[i]
as the already computed result.i
), the next element (i+1
), or the element after that (i+2
).dp[i]
and return it.nums
and replace each element with the maximum of k - nums[i]
and 0. This represents the minimum increments needed to make the single element beautiful.solve
function starting from index 0, 1, and 2, and return the minimum of these three calls.