forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1774.java
27 lines (24 loc) · 1.08 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
package com.fishercoder.solutions;
public class _1774 {
public static class Solution1 {
int result = 0;
public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {
result = baseCosts[0];
for (int i = 0; i < baseCosts.length; i++) {
recursion(baseCosts[i], toppingCosts, 0, target);
}
return result;
}
private void recursion(int currentCost, int[] toppingCosts, int index, int target) {
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);
}
}
}