Minimum Cost of Left and Right Propagation
Updated
Problem statement
You are given a positive integer array values. You may perform either propagation operation any number of times and in any order:
- Choose index
iand propagate left: replace every element beforeiwithvalues[i]. This costsi * values[i]. - Choose index
iand propagate right: replace every element afteriwithvalues[i]. This costs(n - 1 - i) * values[i].
Return the minimum total cost needed to make every array element equal. You may perform zero operations when the array is already uniform.
Function
minimumPropagationCost(values: int[]) → longExamples
Example 1
values = [5,2,4]return = 4Keep the middle value 2, propagate it left for cost 2, and propagate it right for another cost 2.
Example 2
values = [1,1,2,1]return = 2Keep the first run of two 1s and propagate right from index 1 for cost 2.
Example 3
values = [5,1,1,2,1,1]return = 4Keep the run of two 1s at indices 1 and 2. Propagate left from index 1 for cost 1, then right from index 2 for cost 3. The total is 4; no original equal-value run gives a lower cost.
Constraints
1 <= values.length <= 1000001 <= values[i] <= 10^9- The answer fits in a signed 64-bit integer.
More Amazon Online Assessment questions
- Lexicographically Largest IdentifierOA · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fill One Exact-Length Binary WindowOA · Seen Nov 2026
- Minimum Grid InconvenienceOA · Seen Nov 2026
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Get Smallest Base SegmentOA · Seen Nov 2026
- Maximum Equal-Bit Pairs in a SubsequenceOA · Seen Nov 2026
- First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Earn the Most 🐝Capital One · OA
- Minimum Cost to Merge Values into OnePayPal · OA
- Minimum Operations to Sort with Rotations Then SwapsBank of New York Mellon · OA
- Minimum Rounds to Complete All TasksInfosys · OA
- Reschedule Meetings for Maximum Free TimeFivetran · OA
- Maximum Easy Tasks After RearrangingIBM · OA
- Minimum Cost K-Capable ModelsMicrosoft · OA
- Maximum ScoreNVIDIA · OA
- Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA
- Minimize Adjacent Squared-Difference Cost with One InsertionMcKinsey & Company · OA