Skip to content

Commit 3cddae0

Browse files
authored
Create 2560. House Robber IV (#741)
2 parents 20e3bb8 + b7c2d2f commit 3cddae0

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

2560. House Robber IV

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
bool ispossible(vector<int>& nums, int mid, int k){
4+
int count_of_houses = 0;
5+
int i = 0;
6+
while(i < nums.size()){
7+
if(nums[i] <= mid){
8+
count_of_houses ++;
9+
i += 2;
10+
}
11+
else{
12+
i++;
13+
}
14+
}
15+
return count_of_houses >= k;
16+
}
17+
18+
int minCapability(vector<int>& nums, int k) {
19+
int left = *min_element(nums.begin(), nums.end());
20+
int right = *max_element(nums.begin(), nums.end());
21+
int result = right;
22+
while(left <= right){
23+
int mid = left + (right - left)/2;
24+
if(ispossible(nums, mid, k) == true){
25+
result = mid;
26+
right = mid-1;
27+
}
28+
else{
29+
left = mid+1;
30+
}
31+
}
32+
return result;
33+
}
34+
};

0 commit comments

Comments
 (0)