package leetcode;

/**
 * https://leetcode.com/problems/closest-dessert-cost/
 */
public class Problem1774 {
    public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1774 prob = new Problem1774();
        System.out.println(prob.closestCost(new int[]{1,7}, new int[]{3,4}, 10)); // 10
        System.out.println(prob.closestCost(new int[]{2,3}, new int[]{4,5,100}, 18)); // 17
        System.out.println(prob.closestCost(new int[]{3,10}, new int[]{2,5}, 9)); // 8
        System.out.println(prob.closestCost(new int[]{10}, new int[]{1}, 1)); // 10
    }
}

@@ -192,15 +192,274 @@
     
-// TODO
+int i = 0;%0A        MinCost minCost = new MinCost();%0A        for (int baseCost : baseCosts) %7B%0A            if (minCost.value == -1) %7B%0A                minCost.value = baseCost;%0A            %7D%0A            closestCost(toppingCosts, target, 0, baseCost, minCost);%0A        %7D
 %0A   
@@ -470,17 +470,29 @@
  return 
-0
+minCost.value
 ;%0A    %7D%0A
@@ -501,444 +501,1194 @@
    p
-ublic static void main(String%5B%5D args) %7B%0A        Problem1774 prob = new Problem1774();%0A        System.out.println(prob.closestCost(new int%5B%5D%7B1,7%7D, new int%5B%5D%7B3,4%7D, 10)); // 10%0A        System.out.println(prob.closestCost(new int%5B%5D%7B2,3%7D, new int%5B%5D%7B4,5,100%7D, 18)); // 17%0A        System.out.println(prob.closestCost(new int%5B%5D%7B3,10%7D, new int%5B%5D%7B2,5%7D, 9)); // 8%0A        System.out.println(prob.closestCost(new int%5B%5D%7B10%7D, new int%5B%5D%7B1%7D, 1)); // 10
+rivate static class MinCost %7B%0A        private int value = -1;%0A    %7D%0A%0A    private static void closestCost(int%5B%5D toppingCosts, int target, int index, int cost,%0A                                    MinCost minCost) %7B%0A        if (index == toppingCosts.length %7C%7C cost %3E target) %7B%0A            if (Math.abs(cost - target) %3C Math.abs(minCost.value - target)) %7B%0A                minCost.value = cost;%0A            %7D else if (Math.abs(cost - target) == Math.abs(minCost.value - target)) %7B%0A                minCost.value = Math.min(minCost.value, cost);%0A            %7D%0A            return;%0A        %7D%0A        if (Math.abs(cost - target) %3C Math.abs(minCost.value - target)) %7B%0A            minCost.value = cost;%0A        %7D else if (Math.abs(cost - target) == Math.abs(minCost.value - target)) %7B%0A            minCost.value = Math.min(minCost.value, cost);%0A        %7D%0A        // Skip topping.%0A        closestCost(toppingCosts, target, index + 1, cost, minCost);%0A        // Add topping.%0A        closestCost(toppingCosts, target, index + 1, cost + toppingCosts%5Bindex%5D, minCost);%0A        // Add the same topping twice.%0A        closestCost(toppingCosts, target, index + 1, cost + (toppingCosts%5Bindex%5D * 2), minCost);
 %0A   

Solve problem 1774