Watch 7 video solutions for Minimize the Difference Between Target and Chosen Elements, a medium level problem involving Array, Dynamic Programming, Matrix. This walkthrough by Happy Coding has 2,560 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an m x n integer matrix mat and an integer target.
Choose one integer from each row in the matrix such that the absolute difference between target and the sum of the chosen elements is minimized.
Return the minimum absolute difference.
The absolute difference between two numbers a and b is the absolute value of a - b.
Example 1:
Input: mat = [[1,2,3],[4,5,6],[7,8,9]], target = 13 Output: 0 Explanation: One possible choice is to: - Choose 1 from the first row. - Choose 5 from the second row. - Choose 7 from the third row. The sum of the chosen elements is 13, which equals the target, so the absolute difference is 0.
Example 2:
Input: mat = [[1],[2],[3]], target = 100 Output: 94 Explanation: The best possible choice is to: - Choose 1 from the first row. - Choose 2 from the second row. - Choose 3 from the third row. The sum of the chosen elements is 6, and the absolute difference is 94.
Example 3:
Input: mat = [[1,2,9,8,7]], target = 6 Output: 1 Explanation: The best choice is to choose 7 from the first row. The absolute difference is 1.
Constraints:
m == mat.lengthn == mat[i].length1 <= m, n <= 701 <= mat[i][j] <= 701 <= target <= 800Problem Overview: You are given an m x n matrix and a target value. You must choose exactly one element from each row. The goal is to minimize |sum - target|, where sum is the total of the chosen elements.
Approach 1: Backtracking with Pruning (Exponential, worst-case O(n^m) time, O(m) space)
This approach tries every possible combination by selecting one element from each row using recursion. At each step, you add the chosen value to the current sum and move to the next row. A pruning rule cuts off branches when the current sum already exceeds a threshold where improvement is impossible. Sorting row values and stopping early once sums grow too large significantly reduces the search space in practice. The method is straightforward and works well for smaller matrices but can still degrade toward exponential complexity in the worst case.
Approach 2: Dynamic Programming with Closure Check (O(m * n * S) time, O(S) space)
The optimal solution uses dynamic programming to track reachable sums after processing each row. Start with a set containing 0. For every row, iterate through the current sums and add each element in that row, generating the next set of possible sums. To prevent the state space from exploding, apply a closure check: cap the sums at a reasonable bound around the target (for example target + 70, since matrix values are small). This pruning keeps the DP state compact while still preserving all sums that could produce the optimal difference.
The key insight: the exact sum doesn't matter once it grows far beyond the target. Only sums within a limited range can produce the minimum absolute difference. Using a boolean array or hash set to represent reachable sums keeps transitions fast. Each row performs a simple nested iteration over current sums and row elements, making the implementation clean and predictable.
This technique is a classic pattern for subset-style problems where the number of combinations is large but the sum range is bounded. It appears frequently in array and matrix problems that involve selecting elements under constraints.
Recommended for interviews: Dynamic programming with closure checking. Interviewers expect you to recognize that brute-force combinations are too large and convert the problem into a bounded sum DP. Showing the backtracking approach first demonstrates problem understanding, but the DP optimization shows stronger algorithmic thinking.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Backtracking with Pruning | O(n^m) worst case | O(m) | Useful for understanding the brute-force search and when matrix size is small |
| Dynamic Programming with Closure Check | O(m * n * S) | O(S) | Best general solution when sum range is bounded around the target |