- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1774.java
29 lines (26 loc) · 1.14 KB
/
_1774.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
packagecom.fishercoder.solutions.secondthousand;
publicclass_1774 {
publicstaticclassSolution1 {
intresult = 0;
publicintclosestCost(int[] baseCosts, int[] toppingCosts, inttarget) {
result = baseCosts[0];
for (inti = 0; i < baseCosts.length; i++) {
recursion(baseCosts[i], toppingCosts, 0, target);
}
returnresult;
}
privatevoidrecursion(intcurrentCost, int[] toppingCosts, intindex, inttarget) {
if (Math.abs(currentCost - target) < Math.abs(result - target)
|| (Math.abs(currentCost - target) < Math.abs(result - target)
&& currentCost == result)) {
result = currentCost;
}
if (index == toppingCosts.length || currentCost == target) {
return;
}
recursion(currentCost, toppingCosts, index + 1, target);
recursion(currentCost + toppingCosts[index], toppingCosts, index + 1, target);
recursion(currentCost + toppingCosts[index] * 2, toppingCosts, index + 1, target);
}
}
}