-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximumIceCreamBars.cpp
More file actions
40 lines (37 loc) · 944 Bytes
/
Copy pathmaximumIceCreamBars.cpp
File metadata and controls
40 lines (37 loc) · 944 Bytes
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
#include <bits/stdc++.h>
using namespace std;
// Maximum Ice Cream Bars
class Solution
{
public:
int lowerBound(vector<long long> costs, int target)
{
int low = 0, high = costs.size() - 1, mid = 0, ans = -1;
while (low < high)
{
mid = low + (high - low) / 2;
if (costs[mid] <= target)
{
ans = mid;
low = mid + 1;
}
else
high = mid;
}
return ans;
}
int maxIceCream(vector<int> &costs, int coins)
{
sort(costs.begin(), costs.end());
vector<long long> pref(costs.size(), 0);
pref[0] = costs[0];
for (int i = 1; i < costs.size(); i++)
{
pref[i] = pref[i - 1] + costs[i];
}
if (pref.back() <= coins)
return costs.size();
int ans = lowerBound(pref, coins);
return ans + 1;
}
};