Skip to content

Commit 0c41519

Browse files
authored
Added task 1774.
1 parent 8e2bca3 commit 0c41519

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g1701_1800.s1774_closest_dessert_cost;
2+
3+
// #Medium #Array #Dynamic_Programming #Backtracking
4+
// #2022_04_27_Time_5_ms_(82.32%)_Space_42.3_MB_(20.34%)
5+
6+
@SuppressWarnings("java:S1871")
7+
public class Solution {
8+
private int finalValue = Integer.MAX_VALUE;
9+
10+
public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {
11+
for (int baseCost : baseCosts) {
12+
closestCost(baseCost, toppingCosts, target, 0);
13+
}
14+
return finalValue;
15+
}
16+
17+
private void closestCost(int curCost, int[] toppingCosts, int target, int index) {
18+
if (index >= toppingCosts.length || curCost >= target) {
19+
if (Math.abs(target - curCost) < Math.abs(target - finalValue)) {
20+
finalValue = curCost;
21+
} else if (Math.abs(target - curCost) == Math.abs(target - finalValue)
22+
&& target < finalValue) {
23+
finalValue = curCost;
24+
}
25+
return;
26+
}
27+
closestCost(curCost, toppingCosts, target, index + 1);
28+
closestCost(curCost + toppingCosts[index], toppingCosts, target, index + 1);
29+
closestCost(curCost + toppingCosts[index] * 2, toppingCosts, target, index + 1);
30+
}
31+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
1774\. Closest Dessert Cost
2+
3+
Medium
4+
5+
You would like to make dessert and are preparing to buy the ingredients. You have `n` ice cream base flavors and `m` types of toppings to choose from. You must follow these rules when making your dessert:
6+
7+
* There must be **exactly one** ice cream base.
8+
* You can add **one or more** types of topping or have no toppings at all.
9+
* There are **at most two** of **each type** of topping.
10+
11+
You are given three inputs:
12+
13+
* `baseCosts`, an integer array of length `n`, where each `baseCosts[i]` represents the price of the <code>i<sup>th</sup></code> ice cream base flavor.
14+
* `toppingCosts`, an integer array of length `m`, where each `toppingCosts[i]` is the price of **one** of the <code>i<sup>th</sup></code> topping.
15+
* `target`, an integer representing your target price for dessert.
16+
17+
You want to make a dessert with a total cost as close to `target` as possible.
18+
19+
Return _the closest possible cost of the dessert to_ `target`. If there are multiple, return _the **lower** one._
20+
21+
**Example 1:**
22+
23+
**Input:** baseCosts = [1,7], toppingCosts = [3,4], target = 10
24+
25+
**Output:** 10
26+
27+
**Explanation:** Consider the following combination (all 0-indexed):
28+
29+
- Choose base 1: cost 7
30+
31+
- Take 1 of topping 0: cost 1 x 3 = 3
32+
33+
- Take 0 of topping 1: cost 0 x 4 = 0
34+
35+
Total: 7 + 3 + 0 = 10.
36+
37+
**Example 2:**
38+
39+
**Input:** baseCosts = [2,3], toppingCosts = [4,5,100], target = 18
40+
41+
**Output:** 17
42+
43+
**Explanation:** Consider the following combination (all 0-indexed): - Choose base 1: cost 3 - Take 1 of topping 0: cost 1 x 4 = 4 - Take 2 of topping 1: cost 2 x 5 = 10 - Take 0 of topping 2: cost 0 x 100 = 0 Total: 3 + 4 + 10 + 0 = 17. You cannot make a dessert with a total cost of 18.
44+
45+
**Example 3:**
46+
47+
**Input:** baseCosts = [3,10], toppingCosts = [2,5], target = 9
48+
49+
**Output:** 8
50+
51+
**Explanation:** It is possible to make desserts with cost 8 and 10. Return 8 as it is the lower cost.
52+
53+
**Constraints:**
54+
55+
* `n == baseCosts.length`
56+
* `m == toppingCosts.length`
57+
* `1 <= n, m <= 10`
58+
* <code>1 <= baseCosts[i], toppingCosts[i] <= 10<sup>4</sup></code>
59+
* <code>1 <= target <= 10<sup>4</sup></code>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g1701_1800.s1774_closest_dessert_cost;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void closestCost() {
11+
assertThat(new Solution().closestCost(new int[] {1, 7}, new int[] {3, 4}, 10), equalTo(10));
12+
}
13+
14+
@Test
15+
void closestCost2() {
16+
assertThat(
17+
new Solution().closestCost(new int[] {2, 3}, new int[] {4, 5, 100}, 18),
18+
equalTo(17));
19+
}
20+
21+
@Test
22+
void closestCost3() {
23+
assertThat(new Solution().closestCost(new int[] {3, 10}, new int[] {2, 5}, 9), equalTo(8));
24+
}
25+
}

0 commit comments

Comments
 (0)