Minimum Increments for Stepwise Towers
Updated
Problem statement
You are given an integer array towers, where towers[i] is the height of the ith tower.
In one move, add exactly one block to any tower. Blocks cannot be removed.
Make the entire sequence stepwise in either direction:
- increasing: every height is exactly one greater than the previous height; or
- decreasing: every height is exactly one less than the previous height.
Return the minimum number of moves required.
Function
minimumStepwiseIncrements(towers: int[]) → longExamples
Example 1
towers = [1,4,3,2]return = 4Raise the first tower from 1 to 5. The result [5,4,3,2] is stepwise decreasing and costs four moves.
Example 2
towers = [5,7,9,4,11]return = 9The cheapest target is [7,8,9,10,11], requiring 2 + 1 + 0 + 6 + 0 = 9 added blocks.
Constraints
1 <= towers.length <= 1000000 <= towers[i] <= 1000000000- The answer fits in a signed 64-bit integer.
More ZipRecruiter Online Assessment questions
- Four-Level Banking System with Scheduled PaymentsOA · Seen Oct 2026
- Count Three-Digit Numbers with Distinct DigitsOA · Seen Oct 2024
- Longest Frequency-Bounded SubarrayOA · Seen Oct 2024
- Minimum Changes to Draw a YOA · Seen Oct 2024
- Apply Binary State OperationsOA · Seen Sep 2024
- Simulate Orchard Rot SpreadOA · Seen Sep 2024
- Compare Odd- and Even-Index SumsOA · Seen Sep 2024
- Coordinate Removals with Column GravityOA · Seen Sep 2024
- Lexicographically Smallest End ReversalOA · Seen Sep 2024
- Recursively Expanded Shell Command CountsOA · Seen Sep 2024
Similar questions at other companies
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Minimize Adjacent Squared-Difference Cost with One InsertionMcKinsey & Company · OAMcKinsey & Company interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimum Values with Even Powers of TwoVisa · OAVisa interview questions
- Minimize Adjacent Squared-Difference Cost with One InsertionMicrosoft · OAMicrosoft interview questions
- Minimum Halvings for Alternating ParityIBM · OAIBM interview questions
- Resource AllocationMaven Securities · OAMaven Securities interview questions
- Rock JumpingIMC Trading · OAIMC Trading interview questions
- Minimum Total Batch ExpenseAmazon · OA
- Coprime Pick with Change CostCitadel · OACitadel interview questions