forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-number-of-alloys.cpp
81 lines (75 loc) · 2.68 KB
/
maximum-number-of-alloys.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Time: O(k * nlogn)
// Space: O(n)
// sort, math
class Solution {
public:
int maxNumberOfAlloys(int n, int k, int budget, vector<vector<int>>& composition, vector<int>& stock, vector<int>& cost) {
static const int INF = numeric_limits<int>::max();
const auto& count = [&](const vector<int>& machine, int budget) {
const auto& cnt = [&](int x) {
return stock[x] / machine[x];
};
vector<int> idxs(n);
iota(begin(idxs), end(idxs), 0);
sort(begin(idxs), end(idxs), [&](const auto& a, const auto& b) {
return cnt(a) < cnt(b);
});
int result = cnt(idxs[0]);
for (int i = 0, prefix = 0, curr = 0, discount = 0; i < n; ++i) {
curr += cost[idxs[i]] * machine[idxs[i]];
discount += cost[idxs[i]] * (stock[idxs[i]] % machine[idxs[i]]);
if (i + 1 != n && cnt(idxs[i + 1]) - cnt(idxs[i]) == 0) {
continue;
}
prefix += curr;
budget += discount;
curr = discount = 0;
const auto mn = min(i + 1 < n ? (cnt(idxs[i + 1]) - cnt(idxs[i])) : INF, budget / prefix);
if (mn == 0) {
break;
}
budget -= prefix * mn;
result += mn;
}
return result;
};
int result = 0;
for (const auto& machine : composition) {
result = max(result, count(machine, budget));
}
return result;
}
};
// Time: O(k * n * logr), r = min(stock)+budget
// Space: O(1)
// binary search
class Solution2 {
public:
int maxNumberOfAlloys(int n, int k, int budget, vector<vector<int>>& composition, vector<int>& stock, vector<int>& cost) {
const auto& check = [&](int64_t x) {
for (const auto& machine : composition) {
int64_t curr = 0;
for (int i = 0; i < n; ++i) {
curr += (max(x * machine[i] - stock[i], static_cast<int64_t>(0)) * cost[i]);
if (curr > budget) {
break;
}
}
if (curr <= budget) {
return true;
}
}
return false;
};
int left = 1, right = *min_element(cbegin(stock), cend(stock)) + budget;
while (left <= right) {
const int mid = left + (right - left) / 2;
if (!check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right;
}
};