当前位置:网站首页>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;
}
}边栏推荐
- skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
- Opportunity interview experience summary
- 【Seaborn】组合图表:PairPlot和JointPlot
- 二叉搜索树(特性篇)
- 数据中台落地实施之法
- Arduino 控制的双足机器人
- 整理几个重要的Android知识,高级Android开发面试题
- Cesium (4): the reason why gltf model is very dark after loading
- Deep listening array deep listening watch
- The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
猜你喜欢

"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."

最新2022年Android大厂面试经验,安卓View+Handler+Binder

爬虫(17) - 面试(2) | 爬虫面试题库

The latest interview experience of Android manufacturers in 2022, Android view+handler+binder

模块六

node:504报错

Three. JS series (1): API structure diagram-1

预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖

Module VI

QT 图片背景色像素处理法
随机推荐
skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色
"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
字节跳动Android金三银四解析,android面试题app
LeetCode 213. Home raiding II daily question
最新阿里P7技术体系,妈妈再也不用担心我找工作了
谎牛计数(春季每日一题 53)
LeetCode 1186. 删除一次得到子数组最大和 每日一题
AutoLISP series (1): function function 1
[medical segmentation] attention Unet
整理几个重要的Android知识,高级Android开发面试题
Imitate the choice of enterprise wechat conference room
LeetCode 213. 打家劫舍 II 每日一题
Record the migration process of a project
数据中台落地实施之法
node:504报错
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
Opencv personal notes
最新高频Android面试题目分享,带你一起探究Android事件分发机制
Cesium (4): the reason why gltf model is very dark after loading
Opportunity interview experience summary