-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximum-Icecream.cpp
More file actions
72 lines (58 loc) · 1.38 KB
/
Copy pathMaximum-Icecream.cpp
File metadata and controls
72 lines (58 loc) · 1.38 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* https://leetcode.com/problems/maximum-ice-cream-bars/ */
/* greedy */
class Solution {
public:
int maxIceCream(vector<int>& costs, int coins) {
sort(costs.begin(),costs.end());
int count=0;
for(int i=0;i<costs.size();i++)
{
if(costs[i]<=coins)
{
coins=coins-costs[i];
count++;
}
else
{
break;
}
}
return count;
}
};
/* Recursion */
int func(int ind,int target,vector<int>&arr)
{
if(ind==0)
{
if(target>=arr[0]) return 1;
return 0;
}
// if(ind==0) return arr[0]==target;
if(ind<0) return 0;
int nottake=func(ind-1,target,arr);
int take=0;
if(target>=arr[ind])
{
take=1+func(ind-1,target-arr[ind],arr);
} return max(take,nottake);
}
int maxIceCream(vector<int>& costs, int coins) {
return func(costs.size()-1,coins,costs);
}
/* Greedy */
sort(costs.begin(),costs.end());
int count=0;
for(int i=0;i<costs.size();i++ )
{
coins=coins-costs[i];
if(coins>=0)
{
count++;
}
else
{
return count;
}
}
return count;