Skip to content

Commit 429405e

Browse files
authored
Create 2557-maximum-number-of-integers-to-choose-from-a-range-ii.js
1 parent 1619cab commit 429405e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {number[]} banned
3+
* @param {number} n
4+
* @param {number} maxSum
5+
* @return {number}
6+
*/
7+
var maxCount = function(banned, n, maxSum) {
8+
const set = new Set(banned);
9+
10+
let low = 0;
11+
let high = n;
12+
let possibleVal = 0;
13+
while (low <= high) {
14+
const mid = Math.floor((low + high) / 2);
15+
let totalSum = (mid * (mid + 1)) / 2;
16+
for (const val of set) {
17+
if (val <= mid) totalSum -= val;
18+
}
19+
20+
if (totalSum <= maxSum) {
21+
possibleVal = mid;
22+
low = mid + 1;
23+
} else {
24+
high = mid - 1;
25+
}
26+
}
27+
28+
let ans = possibleVal;
29+
for (const val of set) {
30+
if (val <= possibleVal) ans--;
31+
}
32+
33+
return ans;
34+
};

0 commit comments

Comments
 (0)