Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find minimum adjustment cost of an array in Python
The minimum adjustment cost problem involves modifying array elements to ensure adjacent differences don't exceed a target value while minimizing the total cost of changes. We use dynamic programming to find the optimal solution.
Problem Statement
Given an array of positive numbers, we need to replace elements so that the difference between any two adjacent elements is at most equal to a given target. The goal is to minimize the adjustment cost, which is the sum of absolute differences between original and new values: ?|A[i] - Anew[i]|.
Example
If input array is [56, 78, 53, 62, 40, 7, 26, 61, 50, 48] and target = 20, we need to find the minimum cost to adjust elements.
Algorithm Approach
We use dynamic programming where dp[i][j] represents the minimum cost to adjust the first i+1 elements such that the i-th element becomes j ?
Create a 2D table where rows represent array indices and columns represent possible values (0 to M)
Initialize first row with costs of changing first element to each possible value
For each subsequent element, consider all valid previous values within target difference
Find minimum cost among all possible values for the last element
Implementation
def get_min_adjustment_cost(arr, target):
"""
Find minimum cost to adjust array elements so adjacent
differences don't exceed target.
"""
n = len(arr)
M = 100 # Maximum possible value in adjusted array
# dp[i][j] = min cost to adjust first i+1 elements
# such that element i becomes j
dp = [[float('inf')] * (M + 1) for _ in range(n)]
# Initialize first element - cost to change arr[0] to j
for j in range(M + 1):
dp[0][j] = abs(j - arr[0])
# Fill dp table for remaining elements
for i in range(1, n):
for j in range(M + 1):
# Check all valid previous values within target difference
for k in range(max(0, j - target), min(M + 1, j + target + 1)):
cost = dp[i-1][k] + abs(arr[i] - j)
dp[i][j] = min(dp[i][j], cost)
# Return minimum cost among all possible values for last element
return min(dp[n-1])
# Test the function
arr = [56, 78, 53, 62, 40, 7, 26, 61, 50, 48]
target = 20
result = get_min_adjustment_cost(arr, target)
print(f"Minimum adjustment cost: {result}")
Minimum adjustment cost: 35
How It Works
The algorithm works by building a solution incrementally ?
Base Case: For the first element, calculate cost of changing it to each value from 0 to M
Transition: For each subsequent element at position i, consider changing it to value j
Constraint: Only consider previous values k where |j-k| ? target to maintain adjacent difference constraint
Optimization: Choose the minimum cost transition from all valid previous states
Time Complexity
The time complexity is O(n × M × target) where n is array length, M is the maximum value range, and target is the maximum allowed difference. Space complexity is O(n × M) for the DP table.
Conclusion
This dynamic programming approach efficiently finds the minimum adjustment cost by considering all possible value assignments while respecting the adjacent difference constraint. The solution builds optimal substructure by making locally optimal choices at each step.
