Skip to content

Commit 0f3804b

Browse files
BarklimBarklim
authored andcommitted
Create 2560.js
1 parent b131fee commit 0f3804b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

example/Dayly/2560.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const minCapability = (nums, k) => {
7+
const canStealKHouses = (capability) => {
8+
let count = 0;
9+
let i = 0;
10+
while (i < nums.length) {
11+
if (nums[i] <= capability) {
12+
count++;
13+
i += 2;
14+
} else {
15+
i++;
16+
}
17+
}
18+
return count >= k;
19+
};
20+
21+
let left = Math.min(...nums);
22+
let right = Math.max(...nums);
23+
24+
while (left < right) {
25+
let mid = Math.floor((left + right) / 2);
26+
if (canStealKHouses(mid)) {
27+
right = mid;
28+
} else {
29+
left = mid + 1;
30+
}
31+
}
32+
33+
return left;
34+
};
35+
36+
const example1 = minCapability([2,3,5,9], 2); // 5
37+
const example2 = minCapability([2,7,9,3,1], 2); // 2
38+
39+
console.log(example1);
40+
console.log(example2);

0 commit comments

Comments
 (0)