-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
abe7706
commit b9a906d
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <string> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
int solution(int InDistance, vector<int> InRocks, int NumRemove) | ||
{ | ||
InRocks.emplace_back(InDistance); | ||
sort(InRocks.begin(), InRocks.end()); | ||
|
||
int LowerDistance = 1, UpperDistance = InDistance; | ||
int Answer = 0; | ||
|
||
auto IsValid = [&](int EstimateDistance) | ||
{ | ||
int Count = 0, CurrentPos = 0; | ||
for(int RockPos : InRocks) | ||
{ | ||
int Distance = RockPos - CurrentPos; | ||
if(Distance >= EstimateDistance) | ||
{ | ||
CurrentPos = RockPos; | ||
} | ||
else | ||
{ | ||
Count++; | ||
} | ||
} | ||
|
||
return Count <= NumRemove; | ||
}; | ||
|
||
while(LowerDistance <= UpperDistance) | ||
{ | ||
int EstimateDistance = (LowerDistance + UpperDistance) / 2; | ||
if(IsValid(EstimateDistance)) | ||
{ | ||
Answer = EstimateDistance; | ||
LowerDistance = EstimateDistance + 1; | ||
} | ||
else | ||
{ | ||
UpperDistance = EstimateDistance - 1; | ||
} | ||
} | ||
|
||
return Answer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters