Problem · Array
Minimum Operations for Stepwise Structures
Learn this problemProblem statement
You are given an integer array structures, where structures[i] is the height of the structure at position i.
In one operation, you may increase the height of any one structure by exactly 1. You may perform as many operations as needed, but you may not decrease any height.
Transform the array into either of these stepwise patterns:
- Ascending: every structure is exactly
1unit taller than the structure immediately before it. - Descending: every structure is exactly
1unit shorter than the structure immediately before it.
Return the minimum number of operations needed to obtain either pattern.
Function
minimumStepwiseOperations(structures: int[]) → longExamples
Example 1
structures = [1,4,3,2]return = 4Add 4 units to the first structure. The final heights are [5,4,3,2], which form a descending stepwise pattern.
Example 2
structures = [5,7,9,4,11]return = 9Add 2 units to the first structure, 1 unit to the second, and 6 units to the fourth. The final heights are [7,8,9,10,11], which form an ascending stepwise pattern, using 9 operations.
Constraints
structuresis an array of integers representing structure heights.- An operation increases exactly one structure height by
1. - The final adjacent heights must differ by exactly
1throughout the array. - The source shows a Java execution time limit of
3seconds and a memory limit of1 GB.
More Tiktok problems
- Concatenate Digit-wise SumsOA · Seen Aug 2026
- Count 2x2 Submatrices by Black CellsOA · Seen Aug 2026
- Count House Segments After DestructionOA · Seen Aug 2026
- Debugger Breakpoint ActionsOA · Seen Aug 2026
- Distribution Center Package AllocationOA · Seen Aug 2026
- Find All Local PeaksOA · Seen Aug 2026
- Minimum Height Difference Between Distant PeaksOA · Seen Aug 2026
- Rating Level After ChangesOA · Seen Aug 2026