forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sell-diminishing-valued-colored-balls.cpp
48 lines (43 loc) · 1.47 KB
/
sell-diminishing-valued-colored-balls.cpp
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
// Time: O(nlogm), m is the max of inventory, n is the size of inventory
// Space: O(1)
class Solution {
public:
int maxProfit(vector<int>& inventory, int orders) {
static const int MOD = 1e9 + 7;
int left = 1, right = *max_element(cbegin(inventory), cend(inventory));
while (left <= right) {
const auto mid = left + (right - left) / 2;
if (!check(inventory, orders, mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
uint64_t result = 0;
for (const auto& cnt : inventory) {
if (cnt >= left) {
result = (result + (uint64_t(left + cnt) * (cnt - left + 1) / 2) % MOD) % MOD;
}
}
// assert(orders - count(inventory, left) >= 0);
return (result + uint64_t(left - 1) * (orders - count(inventory, left)) % MOD) % MOD;
}
private:
int count(const vector<int>& inventory, int x) {
static const int MAX_ORDERS = 1e9;
int result = 0;
for (const auto& cnt : inventory) {
if (cnt >= x) {
if (result >= (MAX_ORDERS + 1) - (cnt - x + 1)) {
result = MAX_ORDERS + 1;
break;
}
result += cnt - x + 1;
}
}
return result;
}
bool check(const vector<int>& inventory, int orders, int x) {
return count(inventory, x) > orders;
}
};