← Capital One Interview Insights
The key insight I kept second-guessing was that you can only increment, never decrement.
Recognize that the final array must be either an arithmetic progression with common difference +1 or -1. For each possible starting height, compute the cost to transform the given array into each of these two patterns, then take the minimum. Since only increments are allowed, the starting height must be at least the maximum of (height[i] - i) for increasing pattern and (height[i] + i) for decreasing pattern, so we can compute the minimal cost directly without iterating over all possible starts.
Pro tip: Clarify that only increments are allowed, so the target heights must be at least the original heights. This constraint simplifies the problem: for each pattern, the optimal starting height is determined by the maximum required offset, and the cost is the sum of differences.
The beautified array must be strictly increasing by 1 (i.e., h[i] = start + i) or strictly decreasing by 1 (i.e., h[i] = start - i). Identify these two possible patterns.
Since only increments are allowed, for the increasing pattern, start must satisfy start + i >= h[i] for all i, so start >= max(h[i] - i). For the decreasing pattern, start - i >= h[i], so start >= max(h[i] + i).
For each pattern, set start to the minimal feasible value (the maximum of the required lower bounds). Then compute the total operations as the sum over i of (target[i] - h[i]).
Compare the costs for the increasing and decreasing patterns and return the smaller one as the answer.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.