|  | 
|  | 1 | +/** | 
|  | 2 | + * [1774] Closest Dessert Cost | 
|  | 3 | + * | 
|  | 4 | + * 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: | 
|  | 5 | + * | 
|  | 6 | + * 	There must be exactly one ice cream base. | 
|  | 7 | + * 	You can add one or more types of topping or have no toppings at all. | 
|  | 8 | + * 	There are at most two of each type of topping. | 
|  | 9 | + * | 
|  | 10 | + * You are given three inputs: | 
|  | 11 | + * | 
|  | 12 | + * 	baseCosts, an integer array of length n, where each baseCosts[i] represents the price of the i^th ice cream base flavor. | 
|  | 13 | + * 	toppingCosts, an integer array of length m, where each toppingCosts[i] is the price of one of the i^th topping. | 
|  | 14 | + * 	target, an integer representing your target price for dessert. | 
|  | 15 | + * | 
|  | 16 | + * You want to make a dessert with a total cost as close to target as possible. | 
|  | 17 | + * Return the closest possible cost of the dessert to target. If there are multiple, return the lower one. | 
|  | 18 | + *   | 
|  | 19 | + * Example 1: | 
|  | 20 | + * | 
|  | 21 | + * Input: baseCosts = [1,7], toppingCosts = [3,4], target = 10 | 
|  | 22 | + * Output: 10 | 
|  | 23 | + * Explanation: Consider the following combination (all 0-indexed): | 
|  | 24 | + * - Choose base 1: cost 7 | 
|  | 25 | + * - Take 1 of topping 0: cost 1 x 3 = 3 | 
|  | 26 | + * - Take 0 of topping 1: cost 0 x 4 = 0 | 
|  | 27 | + * Total: 7 + 3 + 0 = 10. | 
|  | 28 | + * | 
|  | 29 | + * Example 2: | 
|  | 30 | + * | 
|  | 31 | + * Input: baseCosts = [2,3], toppingCosts = [4,5,100], target = 18 | 
|  | 32 | + * Output: 17 | 
|  | 33 | + * Explanation: Consider the following combination (all 0-indexed): | 
|  | 34 | + * - Choose base 1: cost 3 | 
|  | 35 | + * - Take 1 of topping 0: cost 1 x 4 = 4 | 
|  | 36 | + * - Take 2 of topping 1: cost 2 x 5 = 10 | 
|  | 37 | + * - Take 0 of topping 2: cost 0 x 100 = 0 | 
|  | 38 | + * Total: 3 + 4 + 10 + 0 = 17. You cannot make a dessert with a total cost of 18. | 
|  | 39 | + * | 
|  | 40 | + * Example 3: | 
|  | 41 | + * | 
|  | 42 | + * Input: baseCosts = [3,10], toppingCosts = [2,5], target = 9 | 
|  | 43 | + * Output: 8 | 
|  | 44 | + * Explanation: It is possible to make desserts with cost 8 and 10. Return 8 as it is the lower cost. | 
|  | 45 | + * | 
|  | 46 | + *   | 
|  | 47 | + * Constraints: | 
|  | 48 | + * | 
|  | 49 | + * 	n == baseCosts.length | 
|  | 50 | + * 	m == toppingCosts.length | 
|  | 51 | + * 	1 <= n, m <= 10 | 
|  | 52 | + * 	1 <= baseCosts[i], toppingCosts[i] <= 10^4 | 
|  | 53 | + * 	1 <= target <= 10^4 | 
|  | 54 | + * | 
|  | 55 | + */ | 
|  | 56 | +pub struct Solution {} | 
|  | 57 | + | 
|  | 58 | +// problem: https://leetcode.com/problems/closest-dessert-cost/ | 
|  | 59 | +// discuss: https://leetcode.com/problems/closest-dessert-cost/discuss/?currentPage=1&orderBy=most_votes&query= | 
|  | 60 | + | 
|  | 61 | +// submission codes start here | 
|  | 62 | + | 
|  | 63 | +impl Solution { | 
|  | 64 | +    pub fn closest_cost(base_costs: Vec<i32>, topping_costs: Vec<i32>, target: i32) -> i32 { | 
|  | 65 | +        let mut result = base_costs[0]; | 
|  | 66 | + | 
|  | 67 | +        for base_cost in base_costs { | 
|  | 68 | +            Self::dfs_helper(&mut result, &topping_costs, 0, target, base_cost); | 
|  | 69 | +        } | 
|  | 70 | + | 
|  | 71 | +        result | 
|  | 72 | +    } | 
|  | 73 | + | 
|  | 74 | +    fn dfs_helper(result: &mut i32, topping_costs: &Vec<i32>, i: usize, target: i32, sum: i32) { | 
|  | 75 | +        if i == topping_costs.len() { | 
|  | 76 | +            return; | 
|  | 77 | +        } | 
|  | 78 | +        let mut k = 0; | 
|  | 79 | +        loop { | 
|  | 80 | +            let temp = sum + k * topping_costs[i]; | 
|  | 81 | +            let delta = i32::abs(*result - target); | 
|  | 82 | +            if i32::abs(target - temp) < delta | 
|  | 83 | +                || (i32::abs(target - temp) == delta && temp < *result) | 
|  | 84 | +            { | 
|  | 85 | +                *result = temp; | 
|  | 86 | +            } | 
|  | 87 | + | 
|  | 88 | +            Self::dfs_helper(result, topping_costs, i + 1, target, temp); | 
|  | 89 | + | 
|  | 90 | +            if target <= temp || k == 2 { | 
|  | 91 | +                break; | 
|  | 92 | +            } | 
|  | 93 | +            k += 1; | 
|  | 94 | +        } | 
|  | 95 | +    } | 
|  | 96 | +} | 
|  | 97 | + | 
|  | 98 | +// submission codes end | 
|  | 99 | + | 
|  | 100 | +#[cfg(test)] | 
|  | 101 | +mod tests { | 
|  | 102 | +    use super::*; | 
|  | 103 | + | 
|  | 104 | +    #[test] | 
|  | 105 | +    fn test_1774_example_1() { | 
|  | 106 | +        let base_costs = vec![1, 7]; | 
|  | 107 | +        let topping_costs = vec![3, 4]; | 
|  | 108 | +        let target = 10; | 
|  | 109 | + | 
|  | 110 | +        let result = 10; | 
|  | 111 | + | 
|  | 112 | +        assert_eq!( | 
|  | 113 | +            Solution::closest_cost(base_costs, topping_costs, target), | 
|  | 114 | +            result | 
|  | 115 | +        ); | 
|  | 116 | +    } | 
|  | 117 | + | 
|  | 118 | +    #[test] | 
|  | 119 | +    fn test_1774_example_2() { | 
|  | 120 | +        let base_costs = vec![2, 3]; | 
|  | 121 | +        let topping_costs = vec![2, 5]; | 
|  | 122 | +        let target = 18; | 
|  | 123 | + | 
|  | 124 | +        let result = 17; | 
|  | 125 | + | 
|  | 126 | +        assert_eq!( | 
|  | 127 | +            Solution::closest_cost(base_costs, topping_costs, target), | 
|  | 128 | +            result | 
|  | 129 | +        ); | 
|  | 130 | +    } | 
|  | 131 | + | 
|  | 132 | +    #[test] | 
|  | 133 | +    fn test_1774_example_3() { | 
|  | 134 | +        let base_costs = vec![3, 10]; | 
|  | 135 | +        let topping_costs = vec![4, 5, 100]; | 
|  | 136 | +        let target = 9; | 
|  | 137 | + | 
|  | 138 | +        let result = 8; | 
|  | 139 | + | 
|  | 140 | +        assert_eq!( | 
|  | 141 | +            Solution::closest_cost(base_costs, topping_costs, target), | 
|  | 142 | +            result | 
|  | 143 | +        ); | 
|  | 144 | +    } | 
|  | 145 | +} | 
0 commit comments