Spent the first few minutes just staring at the example trying to figure out if this was a DP problem or something greedy.
Clarify that you can only increase heights, so the final array must be element-wise >= the original. The problem reduces to finding the minimum non-decreasing (in terms of constraints) sequence that satisfies the adjacent difference constraint and dominates the original. Use a two-pass dynamic programming approach: first enforce the constraint from left to right, then from right to left, taking the maximum of the two passes at each position.
Pro tip: After computing the two passes, the final height at each index is the maximum of the left-to-right and right-to-left values. This ensures both constraints are satisfied while minimizing total added height. Mention that the total added height is the sum of differences between the final and original arrays.
Restate the problem: given an array, increase elements minimally so that adjacent differences are at most 1. Note that you can only increase, never decrease.
A single pass cannot satisfy both left and right constraints. Explain that you need to propagate constraints from left to right and then from right to left.
Create an array L where L[0] = original[0], and for i from 1 to n-1, L[i] = max(original[i], L[i-1] - 1). This ensures each element is at least the previous minus 1.
Create an array R where R[n-1] = original[n-1], and for i from n-2 down to 0, R[i] = max(original[i], R[i+1] - 1). This ensures each element is at least the next minus 1.
For each index i, final[i] = max(L[i], R[i]). The total added height is sum(final[i] - original[i]). Return this sum.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.