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.length
n == mat[i].length
1 <= m, n <= 70
1 <= mat[i][j] <= 70
1 <= target <= 800
This problem asks to find the minimum absolute difference between a target value and the sum of elements chosen from each row of a matrix. A dynamic programming approach proves efficient, leveraging the concept of a grouped knapsack problem.
The core idea is to iteratively build a set of all possible sums achievable by selecting one element from each row. We then find the sum within this set that's closest to the target.
Initialization: Start with a set containing only 0 (representing the sum before choosing any elements).
Iteration: For each row in the matrix:
s
) and each element (x
) in the current row:
s + x
) to the new set. This represents the new sum after including x
from this row.Finding Minimum Difference: Iterate through the final set of sums, calculating the absolute difference between each sum and the target
. Return the minimum difference found.
Time and Space Complexity Analysis:
Time Complexity: O(m * n * S), where 'm' is the number of rows, 'n' is the number of columns, and 'S' is the maximum possible sum of elements (approximately m * max(mat[i][j])). In the worst case, the size of the set of possible sums grows exponentially with the number of rows. However, the constraints of the problem (m, n <= 70, mat[i][j] <=70) keep the problem computationally manageable.
Space Complexity: O(S), where 'S' is again the maximum possible sum of elements. The space is dominated by the set containing possible sums.
This code uses sets for efficient management of possible sums, avoiding duplicate values.
class Solution:
def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:
sums = {0} # Initialize with sum 0
for row in mat:
new_sums = set()
for s in sums:
for x in row:
new_sums.add(s + x)
sums = new_sums # Update sums for the next iteration
min_diff = float('inf') # Initialize with a large value
for s in sums:
min_diff = min(min_diff, abs(s - target))
return min_diff
Java's HashSet
provides similar functionality to Python's set
.
import java.util.*;
class Solution {
public int minimizeTheDifference(int[][] mat, int target) {
Set<Integer> sums = new HashSet<>();
sums.add(0);
for (int[] row : mat) {
Set<Integer> newSums = new HashSet<>();
for (int s : sums) {
for (int x : row) {
newSums.add(s + x);
}
}
sums = newSums;
}
int minDiff = Integer.MAX_VALUE;
for (int s : sums) {
minDiff = Math.min(minDiff, Math.abs(s - target));
}
return minDiff;
}
}
The C++ and Go implementations would follow a similar structure, using their respective set data structures (e.g., std::unordered_set
in C++ and map[int]bool
in Go, where the bool acts as a flag to mark the presence of a sum). The fundamental algorithmic approach remains the same.