Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
There needs to be at least one value among <code>3</code> consecutive values in the array that is greater than or equal to <code>k</code>.
The problem can be solved using dynamic programming.
Let <code>dp[i]</code> be the minimum number of increment operations required to make the subarray consisting of the first <code>i</code> values beautiful, while also having the value at <code>nums[i] >= k</code>.
<code>dp[0] = max(0, k - nums[0])</code>, <code>dp[1] = max(0, k - nums[1])</code>, and <code>dp[2] = max(0, k - nums[2])</code>.
<code>dp[i] = max(0, k - nums[i]) + min(dp[i - 1], dp[i - 2], dp[i - 3])</code> for <code>i</code> in the range <code>[3, n - 1]</code>.
The answer to the problem is <code>min(dp[n - 1], dp[n - 2], dp[n - 3])</code>.