
Input: mat =[[1,2,3],[4,5,6],[7,8,9]], target =13Output: 0Explanation: 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 is13, which equals the target, so the absolute difference is0.

Input: mat =[[1],[2],[3]], target =100Output: 94Explanation: 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 is6, and the absolute difference is94.

Input: mat =[[1,2,9,8,7]], target =6Output: 1Explanation: The best choice is to choose 7 from the first row.The absolute difference is1.
We need to pick one element from each row so that the sum is as close as possible to the target. By tracking all possible sums we can form by picking one element per row, we can efficiently find the minimum absolute difference to the target.
classSolution {
public:int minimizeTheDifference(vector<vector<int>>& mat, int target) {
int m = mat.size(), n = mat[0].size();
bitset<5001> dp;
dp[0] =1;
for (int i =0; i < m; ++i) {
bitset<5001> ndp;
for (int s =0; s <=5000; ++s) {
if (dp[s]) {
for (int x : mat[i]) {
if (s + x <=5000) ndp[s + x] =1;
}
}
}
dp = ndp;
}
int ans = INT_MAX;
for (int s =0; s <=5000; ++s) {
if (dp[s]) ans = min(ans, abs(s - target));
}
return ans;
}
};
funcminimizeTheDifference(mat [][]int, targetint) int {
m, n:= len(mat), len(mat[0])
dp:=map[int]struct{}{0: {}}
fori:=0; i < m; i++ {
ndp:=map[int]struct{}{}
fors:=rangedp {
for_, x:=rangemat[i] {
ndp[s+x] = struct{}{}
}
}
dp = ndp }
ans:=1<<30fors:=rangedp {
ifabs(s-target) < ans {
ans = abs(s-target)
}
}
returnans}
funcabs(xint) int { ifx < 0 { return-x }; returnx }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
publicintminimizeTheDifference(int[][] mat, int target) {
Set<Integer> dp =new HashSet<>();
dp.add(0);
for (int[] row : mat) {
Set<Integer> ndp =new HashSet<>();
for (int s : dp) {
for (int x : row) ndp.add(s + x);
}
dp = ndp;
}
int ans = Integer.MAX_VALUE;
for (int s : dp) ans = Math.min(ans, Math.abs(s - target));
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
funminimizeTheDifference(mat: Array<IntArray>, target: Int): Int {
var dp = setOf(0)
for (row in mat) {
val ndp = mutableSetOf<Int>()
for (s in dp) for (x in row) ndp.add(s + x)
dp = ndp
}
return dp.minOf { kotlin.math.abs(it - target) }
}
}
1
2
3
4
5
6
7
8
9
defminimize_the_difference(mat: list[list[int]], target: int) -> int:
dp: set[int] = {0}
for row in mat:
ndp = set()
for s in dp:
for x in row:
ndp.add(s + x)
dp = ndp
return min(abs(s - target) for s in dp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
impl Solution {
pubfnminimize_the_difference(mat: Vec<Vec<i32>>, target: i32) -> i32 {
letmut dp = std::collections::HashSet::new();
dp.insert(0);
for row in mat.iter() {
letmut ndp = std::collections::HashSet::new();
for&s in dp.iter() {
for&x in row.iter() {
ndp.insert(s + x);
}
}
dp = ndp;
}
dp.into_iter().map(|s| (s - target).abs()).min().unwrap()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
minimizeTheDifference(mat: number[][], target: number):number {
letdp=newSet<number>([0]);
for (constrowofmat) {
constndp=newSet<number>();
for (constsofdp) for (constxofrow) ndp.add(s+x);
dp=ndp;
}
letans= Number.MAX_SAFE_INTEGER;
for (constsofdp) ans= Math.min(ans, Math.abs(s-target));
returnans;
}
}