C++ Simple and intuitive solution, with Diagram
We will first generate array containing all the possible values that toppingCosts can generate.\nWe know that there a three ways in which a topping can be selec
We will first generate array containing all the possible values that toppingCosts can generate.\nWe know that there a three ways in which a topping can be selected \n1. Not select , move on to the next topping.\n2. Select once, move to next\n3. Select twice, move to next\n\nHere is the digram how it will look, say for toppingCosts = [3, 4] then our recursion tree would like below at each level each topping having 3 choices.\n\n![image](https://assets.leetcode.com/users/images/aeaf5274-d18f-4587-a5bd-244368ae6601_1614486168.0933335.png)\n\nso we now have all possible values toppingCost can give.\n**Following constraints its size can go max upto 3^10 .**\n\nWe can now add each baseCost and chech which gives is closest to target.\n\n```\nclass Solution {\npublic:\n    void solve(int i, vector<int> toppingCosts, int cost, vector<int> &ans){\n    if(i == toppingCosts.size()){\n        ans.push_back(cost);\n        return;\n    }\n    solve(i + 1, toppingCosts, cost, ans);\n    solve(i + 1, toppingCosts, cost + toppingCosts[i], ans);\n    solve(i + 1, toppingCosts, cost + 2*toppingCosts[i], ans);\n\n}\n    int closestCost(vector<int>& baseCosts, vector<int>& toppingCosts, int target) {\n        vector<int> possible_toppings;\n        solve(0, toppingCosts, 0, possible_toppings);\n        \n        int ans, diff = INT_MAX;\n        \n        //iterating for each baseCost and possible toppingCost\n        for(int i = 0; i < baseCosts.size(); i++){\n            for(int j = 0; j < possible_toppings.size(); j++){\n                \n                int temp = baseCosts[i] + possible_toppings[j];\n                // if closest to target found\n                if(diff > abs(target - temp)){\n                    diff = abs(target - temp);\n                    ans = temp;\n                }\n            }\n        }\n       return ans;\n    }\n};\n```\n\n
[]