Skip to content

Commit 71c056e

Browse files
add 1774
1 parent 15cf658 commit 71c056e

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-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+
|1774|[Closest Dessert Cost](https://leetcode.com/problems/closest-dessert-cost/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1774.java) ||Medium|Greedy|
1112
|1773|[Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1773.java) ||Easy|Array, String|
1213
|1769|[Minimum Number of Operations to Move All Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1769.java) ||Medium|Array, Greedy|
1314
|1768|[Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1768.java) ||Easy|String|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1774 {
4+
public static class Solution1 {
5+
int result = 0;
6+
7+
public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {
8+
result = baseCosts[0];
9+
for (int i = 0; i < baseCosts.length; i++) {
10+
recursion(baseCosts[i], toppingCosts, 0, target);
11+
}
12+
return result;
13+
}
14+
15+
private void recursion(int currentCost, int[] toppingCosts, int index, int target) {
16+
if (Math.abs(currentCost - target) < Math.abs(result - target) || (Math.abs(currentCost - target) < Math.abs(result - target) && currentCost == result)) {
17+
result = currentCost;
18+
}
19+
if (index == toppingCosts.length || currentCost == target) {
20+
return;
21+
}
22+
recursion(currentCost, toppingCosts, index + 1, target);
23+
recursion(currentCost + toppingCosts[index], toppingCosts, index + 1, target);
24+
recursion(currentCost + toppingCosts[index] * 2, toppingCosts, index + 1, target);
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1774;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1774Test {
10+
private static _1774.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1774.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(10, solution1.closestCost(new int[]{1, 7}, new int[]{3, 4}, 10));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(17, solution1.closestCost(new int[]{2, 3}, new int[]{4, 5, 100}, 18));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(8, solution1.closestCost(new int[]{3, 10}, new int[]{2, 5}, 9));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)