Skip to content

Commit 1d77174

Browse files
add 1981
1 parent f9a106f commit 1d77174

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1981|[Minimize the Difference Between Target and Chosen Elements](https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1981.java) ||Medium|DP|
1112
|1980|[Find Unique Binary String](https://leetcode.com/problems/find-unique-binary-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1980.java) ||Medium||
1213
|1979|[Find Greatest Common Divisor of Array](https://leetcode.com/problems/find-greatest-common-divisor-of-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1979.java) ||Easy||
1314
|1974|[Minimum Time to Type Word Using Special Typewriter](https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1974.java) ||Easy||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1981 {
4+
public static class Solution1 {
5+
/**
6+
* creidt: https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/discuss/1418614/Java-dp-code-with-proper-comments-and-explanation
7+
*/
8+
int ans = Integer.MAX_VALUE;
9+
boolean[][] dp;
10+
11+
public int minimizeTheDifference(int[][] mat, int target) {
12+
dp = new boolean[mat.length][4900];//we use 4900 due to the contraints in this problem: 70 * 70 = 4900
13+
memo(mat, 0, 0, target);
14+
return ans;
15+
}
16+
17+
private void memo(int[][] mat, int row, int sum, int target) {
18+
if (dp[row][sum]) {
19+
return;
20+
}
21+
if (row == mat.length - 1) {
22+
for (int i = 0; i < mat[0].length; i++) {
23+
ans = Math.min(ans, Math.abs(sum + mat[row][i] - target));
24+
}
25+
dp[row][sum] = true;
26+
return;
27+
}
28+
for (int i = 0; i < mat[0].length; i++) {
29+
memo(mat, row + 1, sum + mat[row][i], target);
30+
}
31+
dp[row][sum] = true;
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1981;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1981Test {
10+
private static _1981.Solution1 solution1;
11+
12+
@BeforeEach
13+
public static void setup() {
14+
solution1 = new _1981.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(0, solution1.minimizeTheDifference(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,2,3],[4,5,6],[7,8,9]"), 13));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(94, solution1.minimizeTheDifference(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1],[2],[3]"), 100));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(1, solution1.minimizeTheDifference(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,2,9,8,7]"), 6));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)