Find the minimum cost to make all array elements equal with prefix and suffix overwrites.
Unlock detailed company stats for this questionUpgrade
You are given an array of length n and can perform these operations:
- Select an index i such that 1 <= i <= n - 1, and set all elements from index 0 to i - 1 to arr[i]; the cost is i * arr[i].
- Select an index i such that 0 <= i <= n - 2, and set all elements from index i + 1 to n - 1 to arr[i]; the cost is (n - 1 - i) * arr[i]. Return the minimum cost to make all elements of the array the same.
Constraints: 1 <= N <= 10^3, -10^5 <= arr[i] <= 10^5.
Example: input [1, 1, 2, 1, 1], output 3. Selecting index 1 and overwriting the suffix costs (5 - 1 - 1) * 1 = 3, giving [1, 1, 1, 1, 1].
Format · Online assessment.
Community answers
No answers contributed by the community yet.
Related questions
Find the minimum cost to make all array elements equal by choosing one index.Find the minimum cost to make all array elements equal using left and right fill operations.Find the minimum total increment to make an array non-decreasing using suffix increases.Related courses

Course

Course
Course
