当前位置:网站首页>LeetCode 1981. Minimize the difference between the target value and the selected element one question per day
LeetCode 1981. Minimize the difference between the target value and the selected element one question per day
2022-07-07 16:59:00 【@Little safflower】
Problem description
Give you a size of m x n The integer matrix of mat And an integer target .
From matrix's Every line Select an integer from the , Your goal is To minimize the Of all selected elements and And target value target Of Absolute difference .
return The smallest absolute difference .
a and b Two digit Absolute difference yes a - b The absolute value of .
Example 1:
Input :mat = [[1,2,3],[4,5,6],[7,8,9]], target = 13
Output :0
explain : One possible optimal option is :
- On the first line, choose 1
- On the second line, choose 5
- On the third line, choose 7
The sum of the selected elements is 13 , Equal to the target value , So the absolute difference is 0 .
Example 2:Input :mat = [[1],[2],[3]], target = 100
Output :94
explain : The only option is :
- On the first line, choose 1
- On the second line, choose 2
- On the third line, choose 3
The sum of the selected elements is 6 , The absolute difference is 94 .
Example 3:Input :mat = [[1,2,9,8,7]], target = 6
Output :1
explain : The best choice is to choose the first line 7 .
The absolute difference is 1 .
Tips :
m == mat.length
n == mat[i].length
1 <= m, n <= 70
1 <= mat[i][j] <= 70
1 <= target <= 800source : Power button (LeetCode)
link :https://leetcode.cn/problems/minimize-the-difference-between-target-and-chosen-elements
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Java
class Solution {
public int minimizeTheDifference(int[][] mat, int target) {
int n = mat.length;
// 70 * 70
boolean[][] dp = new boolean[n][5000];
// Deal with the first line
for(int num : mat[0]) dp[0][num] = true;
for(int i = 1;i < n;i++){
// The elements of each line
for(int val : mat[i]){
for(int j = val;j < 5000;j++){
// The previous line is optional j-val value , The current line can be selected j
dp[i][j] = dp[i][j] || dp[i - 1][j - val];
}
}
}
int ans = Integer.MAX_VALUE;
for(int j = 0;j < 5000;j++){
if(dp[n - 1][j]){
ans = Math.min(ans,Math.abs(j - target));
}
}
return ans;
}
}边栏推荐
- SqlServer2014+: 创建表的同时创建索引
- Pisa-Proxy SQL 解析之 Lex & Yacc
- 字节跳动Android面试,知识点总结+面试题解析
- Three. JS series (3): porting shaders in shadertoy
- Have fun | latest progress of "spacecraft program" activities
- 全网“追杀”钟薛高
- [PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
- 运算符
- 应用在温度检测仪中的温度传感芯片
- LeetCode 1049. 最后一块石头的重量 II 每日一题
猜你喜欢
![[designmode] facade patterns](/img/79/cde2c18e2ec8b08697662ac352ff90.png)
[designmode] facade patterns

Introduction and use of gateway

Vs2019 configuration matrix library eigen
最新Android高级面试题汇总,Android面试题及答案

Prediction - Grey Prediction

Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions

Sort out several important Android knowledge and advanced Android development interview questions

全网“追杀”钟薛高

Horizontal and vertical centering method and compatibility

Pisa-Proxy SQL 解析之 Lex & Yacc
随机推荐
【Seaborn】组合图表:PairPlot和JointPlot
Have fun | latest progress of "spacecraft program" activities
编程模式-表驱动编程
LeetCode 1981. 最小化目标值与所选元素的差 每日一题
LeetCode 1696. 跳跃游戏 VI 每日一题
Opencv personal notes
QT picture background color pixel processing method
Pycharm IDE下载
Cesium(3):ThirdParty/zip. js
LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
time标准库
Personal notes of graphics (1)
[designmode] flyweight pattern
预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖
The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars
Opencv configuration 2019vs
记录Servlet学习时的一次乱码
【Seaborn】组合图表、多子图的实现
Pisa-Proxy SQL 解析之 Lex & Yacc
QT中自定义控件的创建到封装到工具栏过程(一):自定义控件的创建