-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
36 lines (33 loc) · 838 Bytes
/
Solution.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
#include <bitset>
#include <cstddef>
#include <cstdlib>
#include <vector>
class Solution {
public:
int maxCount(std::vector<int>& banned, int n, int maxSum) {
// Seems like Greedy?
// Greedily pick the smallest non-banned integer until <= maxSum.
// Can we prove the Greedy Choice?
// i.e., Proof by Cut-and-Paste:
// 1. Suppose there exist x* such that x* > x.
// 2. If x* + sum <= maxSum, then we can replace x* with x to form a
// smaller sum <= maxSum.
std::bitset<10000 + 1> isBanned{};
for (int num : banned) {
isBanned.set(num);
}
int chosen = 0;
int sum = 0;
for (int i = 1; i <= n; ++i) {
if (isBanned.test(i)) {
continue;
}
if (sum + i > maxSum) {
break;
}
++chosen;
sum += i;
}
return chosen;
}
};