1981. Minimize the Difference Between Target and Chosen Elements Medium

@problem@discussion
#Array#Dynamic Programming#Matrix



1/**
2 * [1981] Minimize the Difference Between Target and Chosen Elements
3 *
4 * You are given an m x n integer matrix mat and an integer target.
5 * 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.
6 * Return the minimum absolute difference.
7 * The absolute difference between two numbers a and b is the absolute value of a - b.
8 *  
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2021/08/03/matrix1.png" style="width: 181px; height: 181px;" />
11 * Input: mat = [[1,2,3],[4,5,6],[7,8,9]], target = 13
12 * Output: 0
13 * Explanation: One possible choice is to:
14 * - Choose 1 from the first row.
15 * - Choose 5 from the second row.
16 * - Choose 7 from the third row.
17 * The sum of the chosen elements is 13, which equals the target, so the absolute difference is 0.
18 * 
19 * Example 2:
20 * <img alt="" src="https://assets.leetcode.com/uploads/2021/08/03/matrix1-1.png" style="width: 61px; height: 181px;" />
21 * Input: mat = [[1],[2],[3]], target = 100
22 * Output: 94
23 * Explanation: The best possible choice is to:
24 * - Choose 1 from the first row.
25 * - Choose 2 from the second row.
26 * - Choose 3 from the third row.
27 * The sum of the chosen elements is 6, and the absolute difference is 94.
28 * 
29 * Example 3:
30 * <img alt="" src="https://assets.leetcode.com/uploads/2021/08/03/matrix1-3.png" style="width: 301px; height: 61px;" />
31 * Input: mat = [[1,2,9,8,7]], target = 6
32 * Output: 1
33 * Explanation: The best choice is to choose 7 from the first row.
34 * The absolute difference is 1.
35 * 
36 *  
37 * Constraints:
38 * 
39 * 	m == mat.length
40 * 	n == mat[i].length
41 * 	1 <= m, n <= 70
42 * 	1 <= mat[i][j] <= 70
43 * 	1 <= target <= 800
44 * 
45 */
46pub struct Solution {}
47
48// problem: https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/
49// discuss: https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/discuss/?currentPage=1&orderBy=most_votes&query=
50
51// submission codes start here
52
53impl Solution {
54    pub fn minimize_the_difference(mat: Vec<Vec<i32>>, target: i32) -> i32 {
55        0
56    }
57}
58
59// submission codes end
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_1981() {
67    }
68}
69


Back
© 2025 bowen.ge All Rights Reserved.