Class Solution
java.lang.Object
g1901_2000.s1981_minimize_the_difference_between_target_and_chosen_elements.Solution
1981 - Minimize the Difference Between Target and Chosen Elements.<p>Medium</p>
<p>You are given an <code>m x n</code> integer matrix <code>mat</code> and an integer <code>target</code>.</p>
<p>Choose one integer from <strong>each row</strong> in the matrix such that the <strong>absolute difference</strong> between <code>target</code> and the <strong>sum</strong> of the chosen elements is <strong>minimized</strong>.</p>
<p>Return <em>the <strong>minimum absolute difference</strong></em>.</p>
<p>The <strong>absolute difference</strong> between two numbers <code>a</code> and <code>b</code> is the absolute value of <code>a - b</code>.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/08/03/matrix1.png" alt="" /></p>
<p><strong>Input:</strong> mat = [[1,2,3],[4,5,6],[7,8,9]], target = 13</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong> One possible choice is to:</p>
<ul>
<li>
<p>Choose 1 from the first row.</p>
</li>
<li>
<p>Choose 5 from the second row.</p>
</li>
<li>
<p>Choose 7 from the third row.</p>
</li>
</ul>
<p>The sum of the chosen elements is 13, which equals the target, so the absolute difference is 0.</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/08/03/matrix1-1.png" alt="" /></p>
<p><strong>Input:</strong> mat = [[1],[2],[3]], target = 100</p>
<p><strong>Output:</strong> 94</p>
<p><strong>Explanation:</strong> The best possible choice is to:</p>
<ul>
<li>
<p>Choose 1 from the first row.</p>
</li>
<li>
<p>Choose 2 from the second row.</p>
</li>
<li>
<p>Choose 3 from the third row.</p>
</li>
</ul>
<p>The sum of the chosen elements is 6, and the absolute difference is 94.</p>
<p><strong>Example 3:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/08/03/matrix1-3.png" alt="" /></p>
<p><strong>Input:</strong> mat = [[1,2,9,8,7]], target = 6</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong> The best choice is to choose 7 from the first row. The absolute difference is 1.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == mat.length</code></li>
<li><code>n == mat[i].length</code></li>
<li><code>1 <= m, n <= 70</code></li>
<li><code>1 <= mat[i][j] <= 70</code></li>
<li><code>1 <= target <= 800</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
minimizeTheDifference
public int minimizeTheDifference(int[][] mat, int target)
-