Skip to content

Commit e8015ed

Browse files
committed
Runtime: 12 ms (Top 88.73%) | Memory: 9.9 MB (Top 42.44%)
1 parent bdbf0ba commit e8015ed

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed
Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,77 @@
1+
// Runtime: 12 ms (Top 88.73%) | Memory: 9.9 MB (Top 42.44%)
12
class Solution {
23
public:
3-
4+
45
//ans-> store final result
56
//mn -> Current minimum difference b/w target and curr_price
67
int ans=INT_MAX,mn=INT_MAX;
7-
8+
89
/*
910
i-> index of tc(toppingCosts)
1011
curr -> current cost taken
1112
tar -> Target cost
12-
*/
13-
13+
*/
14+
1415
void find(int i,vector<int>& tc,int curr, int tar)
15-
{
16+
{
1617
/*
17-
if difference b/w current cost and target cost is
18-
less than min difference , so we have to update ans
18+
if difference b/w current cost and target cost is
19+
less than min difference , so we have to update ans
1920
and min difference because we got closest to target
2021
*/
2122
if(abs(curr-tar)<mn)
22-
{
23+
{
2324
mn=abs(curr-tar);
2425
ans=curr;
2526
}
26-
27+
2728
/*
28-
if difference b/w current cost and target cost is
29-
equal to min difference , so we have to update ans
29+
if difference b/w current cost and target cost is
30+
equal to min difference , so we have to update ans
3031
and takin minimum one as condition given in problem
3132
*/
32-
if(abs(curr-tar)==mn) ans=min(ans,curr);
33-
33+
if(abs(curr-tar)==mn) ans=min(ans,curr);
34+
3435
/*
35-
If we are going out of bound just return
36+
If we are going out of bound just return
3637
because we will nothing get after here
3738
*/
3839
if(i>=tc.size() || curr-tar>mn) return;
39-
40-
41-
/* Main interesting thing -> How to recurse */
40+
41+
/* Main interesting thing -> How to recurse */
4242
/*
4343
Just put all the required conditions
44-
44+
4545
NOTE: We have already taken 1 baseCosts, so left that
4646
We have to select from tc(toppingCosts)
47-
47+
4848
In our question we have 3 conditon
4949
1. Take ith toppingCosts and go for next (i+1)th toppingCost
5050
2. Take ith toppingCosts 2 times and go for next (i+1)th toppingCost
5151
3. We will not take ith toppingCosts and will select next ones
5252
*/
53-
53+
5454
//taking ith toppingCosts and moving to (i+1)th toppingCosts
55-
find(i+1,tc,curr+tc[i],tar);
56-
55+
find(i+1,tc,curr+tc[i],tar);
56+
5757
//taking ith toppingCosts 2 times and moving to (i+1)th toppingCosts
58-
find(i+1,tc,curr+2*tc[i],tar);
59-
58+
find(i+1,tc,curr+2*tc[i],tar);
59+
6060
//Without taking ith toppingCosts and move to (i+1)th toppingCosts for next one
61-
find(i+1,tc,curr,tar);
62-
63-
61+
find(i+1,tc,curr,tar);
62+
6463
}
6564
int closestCost(vector<int>& baseCosts, vector<int>& toppingCosts, int target) {
66-
65+
6766
//take each baseCost every time
6867
for(auto x:baseCosts)
6968
{
7069
//taking ith baseCost and select toppingCosts
71-
find(0,toppingCosts,x,target);
70+
find(0,toppingCosts,x,target);
7271
}
73-
72+
7473
//return closest to target answer
7574
return ans;
76-
75+
7776
}
78-
};
77+
};

0 commit comments

Comments
 (0)