Skip to content

Commit ecdb282

Browse files
authored
Create Minimize the Difference Between Target and Chosen Elements [19… (qiyuangong#47)
* Create Minimize the Difference Between Target and Chosen Elements [1981].java Contributed by @parascoding
1 parent 998b2da commit ecdb282

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int minimizeTheDifference(int[][] a, int k) {
3+
n=a.length;
4+
m=a[0].length;
5+
min=Integer.MAX_VALUE;
6+
dp=new boolean[n][5000];
7+
solve(a,k,0,0,0);
8+
return min;
9+
}
10+
private void solve(int a[][],int k,int sum,int row,int col)
11+
{
12+
if(dp[row][sum]) return;
13+
if(n-1==row)
14+
{
15+
for(int i=0;i<m;i++)
16+
min=Math.min(min,Math.abs(k-sum-a[row][i]));
17+
dp[row][sum]=true;
18+
return;
19+
}
20+
21+
for(int i=0;i<m;i++)
22+
solve(a,k,sum+a[row][i],row+1,col);
23+
dp[row][sum]=true;
24+
}
25+
private int min;
26+
private int dy[],n,m;
27+
private boolean dp[][];
28+
}

0 commit comments

Comments
 (0)