-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 272 ms (21.41%), Space: 57.9 MB (84.18%) - LeetHub
- Loading branch information
1 parent
b6b90d8
commit dbdffaa
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
1552-magnetic-force-between-two-balls/1552-magnetic-force-between-two-balls.cpp
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,38 @@ | ||
class Solution { | ||
public: | ||
bool isChk(vector<int> &P, int mid, int M) | ||
{ | ||
int prev = 0; | ||
int count = 1; | ||
for(int i=1;i<P.size();i++) | ||
{ | ||
if(abs(P[prev]-P[i]) >= mid) | ||
{ | ||
prev = i; | ||
count++; | ||
} | ||
if(count >= M) | ||
return true; | ||
} | ||
return count >= M; | ||
} | ||
int maxDistance(vector<int>& nums, int m) { | ||
sort(nums.begin(),nums.end()); | ||
int s=1; | ||
int e=nums[nums.size()-1]; | ||
int ans=0; | ||
while(s<e) | ||
{ | ||
int mid=(s+e)/2; | ||
if(isChk(nums,mid,m)) | ||
{ | ||
ans=mid; | ||
s=mid+1; | ||
} | ||
else | ||
e=mid; | ||
} | ||
return ans; | ||
|
||
} | ||
}; |