Make Pixel Columns Strictly Increasing
Updated
Problem statement
An image is stored as a grid pixelIntensity with n rows and m columns. Each cell holds the brightness of one pixel.
The image is valid when, in every column, each pixel is strictly brighter than every pixel above it in that column. In other words, reading any column from top to bottom, the values strictly increase.
You may only make pixels brighter. Raising a pixel by 1 costs 1, so raising it by d costs d. Return the minimum total cost to make the image valid. The answer can exceed the 32-bit range, so return it as a 64-bit integer.
Function
getMinimumIncreaseCost(pixelIntensity: int[][]) → longExamples
Example 1
pixelIntensity = [[2,5],[7,4],[3,5]]return = 9Column 0 is 2, 7, 3. The first two values already increase, but 3 is not greater than 7, so it rises to 8 at a cost of 5. Column 1 is 5, 4, 5. The 4 must rise to 6 (cost 2), and then the last 5 must rise to 7 (cost 2), because it has to be greater than the new 6. The total cost is 5 + 2 + 2 = 9.
Example 2
pixelIntensity = [[5],[5],[5]]return = 3The only column is 5, 5, 5. The top value stays, the middle one rises to 6 (cost 1) and the bottom one rises to 7 (cost 2). The total cost is 3.
Example 3
pixelIntensity = [[4,9,1]]return = 0There is only one row, so every column has one pixel and nothing needs to change. The cost is 0.
Constraints
1 <= n, m <= 100, wherenis the number of rows andmis the number of columns.0 <= pixelIntensity[i][j] <= 10^9- All rows have the same length
m.
More Microsoft Online Assessment questions
- Minimum Cost to Process Priority TasksOA · Seen Nov 2026
- Most Visited Sprint MarkerOA · Seen Nov 2026
- Maximum Escape Game ScoreOA · Seen Nov 2026
- Count Subarrays with a Perfect-Square ProductOA · Seen Nov 2026
- Maximum Interactive Team SizeOA · Seen Nov 2026
- Maximize Pipeline Throughput Under a Scaling BudgetOA · Seen Nov 2026
- Minimum Cost K-Capable ModelsOA · Seen Nov 2026
- Find Available Meeting RoomsOA · Seen Oct 2026
- Maximum Sum of a K-StarOA · Seen Oct 2026
- Capacity to Ship Packages Within DaysOA · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Minimum Row Deletions to Prevent a Target ORInMobi · ONSITE INTERVIEWInMobi interview questions
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Server InvestmentRippling · OARippling interview questions
- Book Reading EveningsGoogle · OAGoogle interview questions
- Wind-Blown Leaves Remaining in a GridAbridge · OAAbridge interview questions
- Maximum Apples That Fit in a BoxVirtu Financial · OAVirtu Financial interview questions
- Query a Header-Encoded MatrixNVIDIA · OANVIDIA interview questions
- Minimum AmountBelvedere Trading · OA
- Maximum Income from Asset TradingJefferies · OA
- Maximum Three-Server Difficulty ScoreVisa · OAVisa interview questions